-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproto_client.py
205 lines (164 loc) · 6.55 KB
/
proto_client.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
#
# Copyright(c) 2023 Swisscom (Schweiz) AG
# Authors: Marco Tollini, Leonardo Rodoni
# Distributed under the MIT License (http://opensource.org/licenses/MIT)
#
# External Libraries
import ipaddress
import logging
import socket
import threading
import select
from scapy.all import TCP, UDP, raw
# Internal Libraries
from packet_manager import PacketWithMetadata
from proto import Proto
from report import report
class GenericPClient:
def __init__(
self,
collector,
client):
self.proto = "unknown"
self.collector = collector
self.socket_init = False
self.client = client
self.receiving_thread = None
self.should_run = True
def get_payload(self, packetwm: PacketWithMetadata):
raise Exception("Method not implemented")
def get_repro_ip(self):
return ipaddress.ip_address(self.client.repro_ip)
def receiver(self):
while self.should_run:
ready = select.select([self.socket], [], [], 1)
if ready[0]:
buff = self.socket.recv(4096)
if len(buff) > 0:
logging.info(f"Received {len(buff)} bytes")
def _init_socket(self):
self.socket_init = True
# socket has already a socket initialized by one the child classes with the right proto (UDP or TCP)
s = self.socket
# set additional socket options
if self.client.interface is not None:
s.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, str(self.client.interface + '\0').encode('utf-8'))
if self.client.so_sndbuf is not None:
s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, self.client.so_sndbuf)
if self.client.so_rcvbuf is not None:
s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, self.client.so_rcvbuf)
# some logs
logging.info(f'[{self.client.repro_ip}][{self.proto}] Opening socket with collector (simulating {self.client.src_ip})')
# bind and connect to socket
if type(ipaddress.ip_address(self.client.repro_ip)) is ipaddress.IPv4Address:
s.bind((self.client.repro_ip, 0))
elif type(ipaddress.ip_address(self.client.repro_ip)) is ipaddress.IPv6Address:
s.bind((self.client.repro_ip, 0, 0, 0))
s.connect((self.collector['ip'], self.collector['port']))
self.socket = s
self.receiving_thread = threading.Thread(target=self.receiver)
self.receiving_thread.start()
def send(self, packet, proto):
# open socket if it's the first packet
if not self.socket_init:
self._init_socket()
report.pkt_proto_sent_countup(proto)
return self.socket.send(packet)
class GenericTCPPClient(GenericPClient):
def __init__(
self,
collector,
client):
super().__init__(
collector,
client)
self.__init_socket() # initialize TCP socket
self.proto = Proto.tcp_generic.value
def __init_socket(self):
if type(ipaddress.ip_address(self.client.repro_ip)) is ipaddress.IPv4Address:
self.socket = socket.socket(socket.AF_INET)
elif type(ipaddress.ip_address(self.client.repro_ip)) is ipaddress.IPv6Address:
self.socket = socket.socket(socket.AF_INET6)
def get_payload(self, packetwm: PacketWithMetadata):
packet = packetwm.packet
tcp_payload = raw(packet[TCP].payload)
return tcp_payload
class GenericUDPPClient(GenericPClient):
def __init__(
self,
collector,
client):
super().__init__(
collector,
client)
self.__init_socket() # initialize UDP socket
self.proto = Proto.udp_generic.value
def __init_socket(self):
if type(ipaddress.ip_address(self.client.repro_ip)) is ipaddress.IPv4Address:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
elif type(ipaddress.ip_address(self.client.repro_ip)) is ipaddress.IPv6Address:
self.socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
def get_payload(self, packetwm: PacketWithMetadata):
packet = packetwm.packet
ipfix_packet = raw(packet[UDP].payload)
return ipfix_packet
class BGPPClient(GenericPClient):
def __init__(
self,
collector,
client):
super().__init__(
collector,
client)
self.__init_socket() # initialize TCP socket
self.proto = Proto.bgp.value
def __init_socket(self):
if type(ipaddress.ip_address(self.client.repro_ip)) is ipaddress.IPv4Address:
self.socket = socket.socket(socket.AF_INET)
elif type(ipaddress.ip_address(self.client.repro_ip)) is ipaddress.IPv6Address:
self.socket = socket.socket(socket.AF_INET6)
def get_payload(self, packetwm: PacketWithMetadata):
packet = packetwm.packet
bgp_packet = raw(packet[TCP].payload)
return bgp_packet
def send(self, packet, proto):
r = super().send(packet, proto)
return r
class BMPPClient(GenericPClient):
def __init__(
self,
collector,
client):
super().__init__(
collector,
client)
self.__init_socket() # initialize TCP socket
self.proto = Proto.bmp.value
def __init_socket(self):
if type(ipaddress.ip_address(self.client.repro_ip)) is ipaddress.IPv4Address:
self.socket = socket.socket(socket.AF_INET)
elif type(ipaddress.ip_address(self.client.repro_ip)) is ipaddress.IPv6Address:
self.socket = socket.socket(socket.AF_INET6)
def get_payload(self, packetwm: PacketWithMetadata):
packet = packetwm.packet
bmp_packet = raw(packet[TCP].payload)
return bmp_packet
class IPFIXPClient(GenericPClient):
def __init__(
self,
collector,
client):
super().__init__(
collector,
client)
self.__init_socket() # initialize UDP socket
self.proto = Proto.ipfix.value
def __init_socket(self):
if type(ipaddress.ip_address(self.client.repro_ip)) is ipaddress.IPv4Address:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
elif type(ipaddress.ip_address(self.client.repro_ip)) is ipaddress.IPv6Address:
self.socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
def get_payload(self, packetwm: PacketWithMetadata):
packet = packetwm.packet
ipfix_packet = raw(packet[UDP].payload)
return ipfix_packet