프로젝트

Network webserver

da1seun9 2020. 6. 27. 04:31

Packet Analysis

네트워크 패킷을 wireshark를 이용해서 Naver을 열었을 때 상황이다.

Ethernet in packet

Ethernet의 header를 보여준다.

IP in packet

IP의 header를 보여준다.

TCP in packet

TCP header를 보여준다.

HTTP in packet (Request)

HTTP request Message structure

HTTP 요청 메시지의 구조를 보여준다

Request line : Method + URI + Version (POST + / + HTTP/1.1)

HTTP의 요청 메소드가 아주 많이 있는데 그중에 우리는 자주 사용하는 것이 GET과 POST가 있다

GET : GET메서드는 특정 리소스의 표시를 요청합니다.GET을 사용하는 요청은 오직 데이터를 받기만 합니다.
POST : POST 메서드는 특정 리소스에 엔티티를 제출할 때 쓰입니다.

GET은 클라이언트의 데이터를 URL뒤에 붙여서 보낸다.

Ex) www.dal-seung.tistory.com?id=dalseung&pass=1234

POST는 URL이 아닌 BODY에 데이터를 넣어서 보낸다

Tip) HTTP response Message structure

Status line : Version + Code + Pharse

Training

code1과 code2는 같은 동작을 하는 프로그램이다.

Index.html

<!DOCTYPE html>
<html>
<head>
    <title>hi</title>
</head>
<body>
    hello
</body>
</html>

Code 1

import socket

HOST = '192.168.0.112'
PORT = 5455
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print("binding...")
s.bind((HOST,PORT))
print("success to bind")

s.listen()
print("Listen...")

c, addr = s.accept()
print("Connected by", addr)

with open("/root/Desktop/docs/index.html",'r') as f:
    line = f.read()
    data = c.recv(65535)
    print(data)
    if data[:3] == b'GET' or data[:4] == b'POST':
        res = "HTTP/1.1 200 OK\n\n" # Http version, 200 is http state message
        res += line
    c.sendall(res.encode())
f.close()
c.close()
s.close()

res에 HTTP/1.1 200 OK를 넣는 이유는 HTTP 응답 메세지 구조의 Status line에 들어가야 하기 때문이다.

Version : HTTP/1.1

Code : 200

Pharse : OK

Code 2

import socket

HOST = 'localhost'
PORT = 5455
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print("binding...")
s.bind((HOST,PORT))
print("success to bind")

s.listen()
print("Listen...")

c, addr = s.accept()
print("Connected by", addr)

with open("/root/Desktop/docs/index.html",'r') as f:
    line = f.read()
    data = c.recv(65535)
    print(data)
    if b'GET' in data or b'POST' in data: # GET attach data which send to webserver encoded to url, POST hide sending data in packet
        res = "HTTP/1.1 200 OK\n\n"
        res += line
    c.sendall(res.encode())
f.close()
c.close()
s.close()

이 코드는 Method를 찾을 때 파이썬의 in 연산자를 써서 했다.

webserver.py를 실행시키면 binding이 되면서 listen상태가 된다.

이때 해당 ip와 해당 Port를 써서 접속하면

print(data)가 실행되면서 Index.html의 코드가 구현된 웹페이지가 뜬다.

'프로젝트' 카테고리의 다른 글

Netteok 프로젝트  (0) 2020.08.31
Network basic training  (0) 2020.06.25
Windows Software Zer0-day hunting  (0) 2020.02.21
1-day Exploit 실습  (1) 2020.02.21
SEH overwrite  (0) 2020.02.17