-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pfcp_packet_generator.py
313 lines (275 loc) · 12 KB
/
pfcp_packet_generator.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# PFCP Packet Generator with Request and Response Messages
# Author : [email protected]
import random
import time
from scapy.all import IP, UDP, wrpcap
from scapy.contrib.pfcp import *
class RobustPFCPPacketGenerator:
def __init__(self, src_ip, dst_ip):
"""
Initialize the PFCP Packet Generator.
:param src_ip: Source IP address for PFCP packets
:param dst_ip: Destination IP address for PFCP packets
"""
self.src_ip = src_ip
self.dst_ip = dst_ip
self.seq_num = 0
def get_next_seq_num(self):
"""
Generate and return the next sequence number for PFCP messages.
:return: Integer representing the next sequence number
"""
self.seq_num += 1
return self.seq_num
def create_pfcp_packet(self, pfcp_layer, src_ip, dst_ip):
"""
Create a complete PFCP packet with IP and UDP headers.
:param pfcp_layer: The PFCP layer to be encapsulated
:param src_ip: Source IP address
:param dst_ip: Destination IP address
:return: Complete PFCP packet (IP/UDP/PFCP)
"""
return IP(src=src_ip, dst=dst_ip) / UDP(sport=8805, dport=8805) / pfcp_layer
def create_node_id_ie(self, ip_address):
"""
Create a Node ID Information Element.
:param ip_address: IP address to be used as Node ID
:return: IE_NodeId object
"""
return IE_NodeId(ipv4=ip_address)
def create_gate_status_ie(self):
"""
Create a Gate Status Information Element with safe default values.
:return: IE_GateStatus object
"""
try:
# Changed the parameters to match the expected format
return IE_GateStatus(ul=0, dl=0)
except Exception as e:
print(f"Error creating Gate Status IE: {e}")
return None
def create_enhanced_qer(self, qer_id):
"""
Create an enhanced Quality of Service Enforcement Rule (QER) with detailed parameters.
:param qer_id: QER ID
:return: IE_CreateQER object with enhanced QoS parameters
"""
try:
gate_status = self.create_gate_status_ie()
if gate_status is None:
return None
return IE_CreateQER(
IE_list=[
IE_QER_Id(id=qer_id),
gate_status,
IE_MBR(ul=random.randint(1000000, 1000000000), dl=random.randint(1000000, 1000000000)),
IE_GBR(ul=random.randint(500000, 500000000), dl=random.randint(500000, 500000000)),
IE_QFI(QFI=random.randint(1, 63))
]
)
except Exception as e:
print(f"Error creating enhanced QER: {e}")
return None
def generate_association_setup_request_response(self):
"""
Generate a PFCP Association Setup Request and Response pair.
:return: Tuple of (request, response) PFCP packets
"""
try:
seq = self.get_next_seq_num()
request = PFCP(version=1, S=0, seq=seq) / PFCPAssociationSetupRequest(
IE_list=[
self.create_node_id_ie(self.src_ip),
IE_RecoveryTimeStamp(timestamp=int(time.time()))
]
)
response = PFCP(version=1, S=0, seq=seq) / PFCPAssociationSetupResponse(
IE_list=[
self.create_node_id_ie(self.dst_ip),
IE_Cause(cause=1), # Request accepted
IE_RecoveryTimeStamp(timestamp=int(time.time()))
]
)
return (self.create_pfcp_packet(request, self.src_ip, self.dst_ip),
self.create_pfcp_packet(response, self.dst_ip, self.src_ip))
except Exception as e:
print(f"Error generating association setup request/response: {e}")
return None, None
def generate_session_establishment_request_response(self):
"""
Generate a PFCP Session Establishment Request and Response pair.
:return: Tuple of (request, response) PFCP packets
"""
try:
seq = self.get_next_seq_num()
cp_seid = random.randint(1, 1000000)
up_seid = random.randint(1, 1000000)
pdr_id = random.randint(1, 1000)
far_id = random.randint(1, 1000)
qer_id = random.randint(1, 1000)
urr_id = random.randint(1, 1000)
qer = self.create_enhanced_qer(qer_id)
if qer is None:
return None, None
request = PFCP(version=1, S=1, seq=seq, seid=0) / PFCPSessionEstablishmentRequest(
IE_list=[
self.create_node_id_ie(self.src_ip),
IE_FSEID(v4=1, seid=cp_seid, ipv4=self.src_ip),
IE_CreatePDR(
IE_list=[
IE_PDR_Id(id=pdr_id),
IE_Precedence(precedence=random.randint(1, 255))
]
),
IE_CreateFAR(
IE_list=[
IE_FAR_Id(id=far_id),
IE_ApplyAction(FORW=1)
]
),
qer,
IE_CreateURR(
IE_list=[
IE_URR_Id(id=urr_id),
IE_MeasurementMethod(VOLUM=1, DURAT=1)
]
)
]
)
response = PFCP(version=1, S=1, seq=seq, seid=cp_seid) / PFCPSessionEstablishmentResponse(
IE_list=[
IE_Cause(cause=1), # Request accepted
IE_FSEID(v4=1, seid=up_seid, ipv4=self.dst_ip)
]
)
return (self.create_pfcp_packet(request, self.src_ip, self.dst_ip),
self.create_pfcp_packet(response, self.dst_ip, self.src_ip))
except Exception as e:
print(f"Error generating session establishment request/response: {e}")
return None, None
def generate_session_modification_request_response(self):
"""
Generate a PFCP Session Modification Request and Response pair.
:return: Tuple of (request, response) PFCP packets
"""
try:
seq = self.get_next_seq_num()
seid = random.randint(1, 1000000)
pdr_id = random.randint(1, 1000)
far_id = random.randint(1, 1000)
qer_id = random.randint(1, 1000)
urr_id = random.randint(1, 1000)
gate_status = self.create_gate_status_ie()
if gate_status is None:
return None, None
request = PFCP(version=1, S=1, seq=seq, seid=seid) / PFCPSessionModificationRequest(
IE_list=[
IE_UpdatePDR(
IE_list=[
IE_PDR_Id(id=pdr_id),
IE_Precedence(precedence=random.randint(1, 255))
]
),
IE_UpdateFAR(
IE_list=[
IE_FAR_Id(id=far_id),
IE_ApplyAction(FORW=1)
]
),
IE_UpdateQER(
IE_list=[
IE_QER_Id(id=qer_id),
gate_status,
IE_MBR(ul=random.randint(1000000, 1000000000), dl=random.randint(1000000, 1000000000)),
IE_GBR(ul=random.randint(500000, 500000000), dl=random.randint(500000, 500000000)),
IE_QFI(QFI=random.randint(1, 63))
]
),
IE_UpdateURR(
IE_list=[
IE_URR_Id(id=urr_id),
IE_MeasurementMethod(VOLUM=1, DURAT=1)
]
)
]
)
response = PFCP(version=1, S=1, seq=seq, seid=seid) / PFCPSessionModificationResponse(
IE_list=[IE_Cause(cause=1)] # Request accepted
)
return (self.create_pfcp_packet(request, self.src_ip, self.dst_ip),
self.create_pfcp_packet(response, self.dst_ip, self.src_ip))
except Exception as e:
print(f"Error generating session modification request/response: {e}")
return None, None
def generate_session_deletion_request_response(self):
"""
Generate a PFCP Session Deletion Request and Response pair.
:return: Tuple of (request, response) PFCP packets
"""
try:
seq = self.get_next_seq_num()
seid = random.randint(1, 1000000)
request = PFCP(version=1, S=1, seq=seq, seid=seid) / PFCPSessionDeletionRequest()
response = PFCP(version=1, S=1, seq=seq, seid=seid) / PFCPSessionDeletionResponse(
IE_list=[IE_Cause(cause=1)] # Request accepted
)
return (self.create_pfcp_packet(request, self.src_ip, self.dst_ip),
self.create_pfcp_packet(response, self.dst_ip, self.src_ip))
except Exception as e:
print(f"Error generating session deletion request/response: {e}")
return None, None
def generate_heartbeat_request_response(self):
"""
Generate a PFCP Heartbeat Request and Response pair.
:return: Tuple of (request, response) PFCP packets
"""
try:
seq = self.get_next_seq_num()
request = PFCP(version=1, S=0, seq=seq) / PFCPHeartbeatRequest(
IE_list=[IE_RecoveryTimeStamp(timestamp=int(time.time()))]
)
response = PFCP(version=1, S=0, seq=seq) / PFCPHeartbeatResponse(
IE_list=[IE_RecoveryTimeStamp(timestamp=int(time.time()))]
)
return (self.create_pfcp_packet(request, self.src_ip, self.dst_ip),
self.create_pfcp_packet(response, self.dst_ip, self.src_ip))
except Exception as e:
print(f"Error generating heartbeat request/response: {e}")
return None, None
def generate_random_packet_pair(self):
"""
Generate a random PFCP packet pair from the available types.
:return: Tuple of (request, response) PFCP packets
"""
generators = [
self.generate_association_setup_request_response,
self.generate_session_establishment_request_response,
self.generate_session_modification_request_response,
self.generate_session_deletion_request_response,
self.generate_heartbeat_request_response
]
chosen_generator = random.choice(generators)
print(f"Generating packet pair using: {chosen_generator.__name__}")
return chosen_generator()
def generate_pcap(self, num_pairs, filename):
"""
Generate a PCAP file containing multiple PFCP packet pairs.
:param num_pairs: Number of PFCP packet pairs to generate
:param filename: Name of the output PCAP file
"""
packets = []
for i in range(num_pairs):
print(f"Generating packet pair {i+1}/{num_pairs}")
request, response = self.generate_random_packet_pair()
if request and response:
packets.extend([request, response])
else:
print(f"Failed to generate packet pair {i+1}")
if packets:
wrpcap(filename, packets)
print(f"Generated {len(packets)} packets ({len(packets)//2} pairs) and saved to {filename}")
else:
print("No packets were generated. PCAP file was not created.")
if __name__ == "__main__":
generator = RobustPFCPPacketGenerator("192.0.2.1", "192.0.2.2")
generator.generate_pcap(10, "robust_pfcp_traffic_v12.pcap")