-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sniffer.py
108 lines (84 loc) · 3.48 KB
/
Sniffer.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
from scapy.all import *
import pandas as pd
from sklearn.cluster import AgglomerativeClustering
from scipy.cluster import hierarchy
import pandas as pd
BIT_SPLIT = 4
MAX_PAYLOAD = 10
# Define a function to capture TCP packets until the connection is closed
def capture_tcp_packets(ip_address, port):
# Create an empty list to store packet information
packets = []
# Set the flag variable to True initially
connection_open = True
# Define a function to append packet information to the packets list
def store_packet(packet):
# print("Sniffing")
nonlocal connection_open
print(packet[TCP].flags)
if 'PA' == str(packet[TCP].flags):
# print(packet[TCP].flags)
# Extract TCP sequence number and packet arrival time
seq_num = packet[TCP].seq
arrival_time = packet.time
# Append packet information to the packets list
packets.append({'Sequence Number': seq_num,
'Arrival Time': arrival_time})
print("len:", len(packets))
# Check for the TCP FIN flag to detect when the connection is closed
elif 'FA' == str(packet[TCP].flags):
# Set the flag variable to False to stop capturing packets
print("FIN")
connection_open = False
filt = "tcp and dst port "+str(port)
# Use Scapy's sniff() function to capture packets until the connection is closed
sniff(prn=store_packet,
filter=filt, stop_filter=lambda p: not connection_open)
# Create a Pandas data frame from the packets list
df = pd.DataFrame(packets)
X = df['Arrival Time'].values.reshape(-1, 1)
# Initialize the AgglomerativeClustering model
agg_clustering = AgglomerativeClustering(
n_clusters=None, linkage='ward', distance_threshold=6) # increase threshold if bitsplit increases
# Fit the model to the data
agg_clustering.fit(X)
# Add the cluster labels as a new column in the dataframe
df['cluster_label'] = agg_clustering.labels_
index_l = 0
index_u = 0
message = ""
while index_u <= len(df)-1:
while index_u < len(df)-1 and df['cluster_label'][index_l] == df['cluster_label'][index_u + 1]:
index_u += 1
data = int(((df['Sequence Number'][index_u] -
df['Sequence Number'][index_l])/MAX_PAYLOAD)+1)-1
# print(data)
binary_string = bin(data)[2:]
# print(binary_string)
if len(binary_string) < BIT_SPLIT:
binary_string = "0"*(BIT_SPLIT-len(binary_string) %
BIT_SPLIT) + binary_string
# print(binary_string+"\n")
message = message+binary_string
index_l = index_u+1
index_u = index_u+1
n = len(message)
print("message len:", n)
while n > 8 and n % 8 != 0:
print("inloop", n)
print(message)
message = message[1:]
n -= 1
# calculate the number of bytes needed to represent the integer
num = int(message, 2)
# calculate the number of bytes needed to represent the integer
num_bytes = (len(message) + 7) // 8
# convert the integer to bytes
bytes_data = num.to_bytes(num_bytes, byteorder='big')
# Making bitstring valid in case it is not a multiple of 8
print("Final bits", message)
# Writing to file
f = open("/home/gb/SteganographySoftware/uploads/msg.txt", "w")
f.write(bytes_data.decode('utf-8'))
print("Message Received")
return 'msg.txt'