본문 바로가기

Python/Forensic

Python Forensics (2)

- Concept of Network Programming

 네트워크 프로그래밍은 3가지의 definition을 갖는다.

(1) Client : 개인용 컴퓨터 및 워크 스테이션에서 실행

(2) Server : 동일 컴퓨터 혹은 다른 컴퓨터의 프로그램

(3) WebSockets : client와 server 사이의 TCP연결을 통해 프로토콜을 제공. 양방향 메시지 전송

위와 같은 그림은 다른 사용자가 보내거나 받은 정보의 유효성을 검사하는데 사용된다. 암호화는 메시지를 보호하는데 사용되는 방법 중 하나로 메시지가 이용하는 채널을 보호하는 것도 중요하다. 클라이언트가 handshaking을 사용하는 python 프로그램을 살펴보자.

# client.py
import socket

# create a socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# get local machine name
host = socket.gethostname()
port = 10001

# connection to hostname on the port.
s.connect((host, port))

# Receive no more than 1024 bytes
tm = s.recv(1024)
print("The client is waiting for connection")
s.close()

# server.py
import socket
import time

# create a socket object
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# get local machine name 
host = socket.gethostname()
port = 10001

# bind to the port
serversocket.bind((host, port))

# queue up to 5 requests 
serversocket.listen(5)

while True:
   # establish a connection 
   clientsocket,addr = serversocket.accept()
   print("Got a connection from %s" % str(addr))
   currentTime = time.ctime(time.time()) + "\r\n"
   clientsocket.send(currentTime.encode('ascii'))
   clientsocket.close()

먼저 client.py를 실행시키면 이런 메세지가 나타난다.

>client.py
The client is waiting for connection

waiting for you인 상태이다. 확인 후 server.py를 실행해보자.

>server.py
Traceback (most recent call last):
  File "server.py", line 13, in <module>
    serversocket.bind((host, port))
OSError: [WinError 10013] 액세스 권한에 의해 숨겨진 소켓에 액세스를 시도했습니다

만약 이런 error가 나온다면 현재 올라와 있는 포트에 바인드해서 포트 충돌이 일어난 것이니 얌전하게 다른 포트로 바꿔준다. 정상적인 실행이 되면 괜히 두렵게 아무 창도 안뜨는데 다른 창을 열어서 client를 다시 실행해준다.

>client.py
The client is waiting for connection

>server.py
Got a connection from ('1.0.0.1', 56448)

서버에서 connection이 올라오는 것을 확인할 수 있다.

'Python > Forensic' 카테고리의 다른 글

Python Forensics (1)  (0) 2019.05.10