본문 바로가기
Study/Python

[Python3] Tcp Socket Proxy tool

by Becoming a Hacker 2022. 3. 10.
반응형

Windows에서 TCP Socket 통신을 하는 프로그램을 진단해야 했는데 NCIS의 safeij.dll이 DLL Injection 및 Debugging을 막아버려서 기존에 사용하던 Echo Mirage와 같은 도구를 사용할 수 없었습니다.

 

이를 진단하기 위해 서버의 IP를 내 IP로 변경한 뒤, MITM을 걸어 A <-> Proxy <-> B로 통신 구간을 확인했습니다.

 

통신 과정만 확인할 수 있도록 대충 개발하다보니 손을 봐야할 내용이 너무 많습니다. 참고용으로만 확인해주세요.

import socket
from threading import Thread

HOST = "127.0.0.1" # Client IP
HOST2 = "1.1.1.1" # Server IP
PORT = 13010

def c_c():
        client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        client_socket.connect((HOST2, PORT))
        return client_socket

def thread_work1(cs, cs2):
        try:
                data = cs.recv(1024000)
                print(f"proxy -> {HOST2}")
                print(data)
                cs2.send(data)
        except Exception as e:
                global server_socket
                global client_socket2, addr
                server_socket.listen()
                client_socket2, addr = server_socket.accept()
                print("New Connect", addr)

def thread_work2(cs, cs2):
        try:
                data = cs2.recv(1024000)
                print(f"{HOST} -> proxy")
                print(data)
                cs.send(data)
        except Exception as e:
                global client_socket
                client_socket = c_c()
                print("New Connect2",(HOST2, PORT))

# server socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((HOST,PORT))
server_socket.listen()

client_socket2, addr = server_socket.accept()
print('Connected by', addr)

# client_socket
client_socket = c_c()

th1 = None
th2 = None

while True:
        if(th1==None):
                th1 = Thread(target=thread_work1,args=(client_socket, client_socket2,))
                th1.start()
        else:
                th1.join()
                th1 = None
        if(th2==None):
                th2 = Thread(target=thread_work2,args=(client_socket, client_socket2,))
                th2.start()
        else:
                th2.join()
                th2 = None

댓글