-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpcap_generator_from_csv.py
executable file
·1233 lines (1058 loc) · 52.8 KB
/
pcap_generator_from_csv.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
import sys
import binascii
import random
import argparse
#for timestamping
import time
import re
import os #for playing around with set input path
# ----- ===== Configurable parameteres ==== ----
# DO NOT TOUCH OTHER VARIABLES
# default necessary values if there is nothing provided
# default_src_mac = "00:00:00:00:00:01"
# default_dst_mac = "00:00:00:00:00:02"
# default_src_ip = "10.0.0.1"
# default_dst_ip = "192.168.88.8"
# default_src_port = 1234
# default_dst_port = 808
# default_vlan = None
# default_ttl = 64
# default_ether_type = 'ipv4'
# default_src_ipv6 = '2603:c022:1:52dd:dead:beef:abba:edda'
# default_dst_ipv6 = '2405:0800:9030:1bd2:dead:beef:dead:beef'
# default_protocol = 'udp'
#DEFINE HERE THE DIFFERENT PACKETS SIZE YOU WANT - ALL HEADER INFORMATION WILL BE THE SAME FOR ALL PACKET SIZES
#This needs to be list object: if you want to use one packet size must be a list with two elements, where the latter is
#empty, i.e., packet_size=(64,)
# packet_sizes = (64,) #, # PCAP file will be generated for these
# 128, # packet sizes - we always generate all packets with these packet sizes
# 256,
# 512,
# 1024,
# 1280,
# 1500)
# COLORIZING
none = '\033[0m'
bold = '\033[01m'
disable = '\033[02m'
underline = '\033[04m'
reverse = '\033[07m'
strikethrough = '\033[09m'
invisible = '\033[08m'
black = '\033[30m'
red = '\033[31m'
green = '\033[32m'
orange = '\033[33m'
blue = '\033[34m'
purple = '\033[35m'
cyan = '\033[36m'
lightgrey = '\033[37m'
darkgrey = '\033[90m'
lightred = '\033[91m'
lightgreen = '\033[92m'
yellow = '\033[93m'
lightblue = '\033[94m'
pink = '\033[95m'
lightcyan = '\033[96m'
CBLINK = '\33[5m'
CBLINK2 = '\33[6m'
# ------ =================================== -----
### CONSTANT LETTERS USED TO BE REPLACED ###
# to ease adding new ones without messing up already used ones
# |--- pcap packet header
# T1 T1 T1 T1 - time in seconds
# T2 T2 T2 T2 - time in microseconds
# XX XX XX XX - Frame size
# YY YY YY YY - Frame size
#
# |--- eth header
# TY PE - Ethernet type #TODO
#
# |--- ip_header
# XX XX - Length
# TT - ttl
# PP - protocol (udp/tcp ~ 11/6) #TODO
# YY YY - checksum
# SS SS SS SS - soure IP (ipv4)
# DD DD DD DD - dest IP (ipv4)
#
# |--- ipv6_header
# XX XX - length
# SS SS SS ... - source IP
# DD DD DD ... - dest IP
#
# |--- udp_header
# ZZ ZZ - source port
# XX XX - dest port
# YY YY - length
#
# |-- gtp_header
# FF - type
# LL LL - length
# TE ID TE ID - TEID
#
# |--- tcp_syn_header
# ZZ ZZ - source port
# XX XX - destination port
# NN NN NN NN - SEQ number
# CC CC - checksum
# TT TT TT TT - timestamp
#sec and microsec
TIME_FORMAT = '4D 3C B2 A1'
#sec and nanosec
# TIME_FORMAT= 'D4 C3 B2 A1'
# Global header for pcap 2.4
pcap_global_header = ( f'{TIME_FORMAT}' #this magic number dictates if time is second+micro, or seconds+nano
'02 00' # File format major revision (i.e. pcap <2>.4)
'04 00' # File format minor revision (i.e. pcap 2.<4>)
'00 00 00 00'
'00 00 00 00'
'FF FF 00 00'
'01 00 00 00')
# pcap packet header that must preface every packet
pcap_packet_header = ('T1 T1 T1 T1' # time in seconds (little endian)
'T2 T2 T2 T2' # time in microseconds (little endian)
'XX XX XX XX' # Frame Size (little endian)
'YY YY YY YY') # Frame Size (little endian)
eth_header = ('00 E0 4C 00 00 01' # Dest Mac
'00 04 0B 00 00 02' # Src Mac
'TY PE') # Protocol (0x0800 = IP)
ip_header = ('45' # IP version and header length (multiples of 4 bytes) (4+4 bits)
'00' #DSCP + ECN (6 + 2 bits)
'XX XX' # Length (16 bits) - will be calculated and replaced later
'00 00' # Identification (16 bits)
'40 00' # Flags + frag_Offset (3 + 13 bits)
'TT PP' # TTL + Protocol (11-UDP, 6-TCP) (8 + 8 bits)
'YY YY' # Checksum - will be calculated and replaced later (16 bits)
'SS SS SS SS' # Source IP (Default: 10.1.0.1) (32 bits)
'DD DD DD DD') # Dest IP (Default: 10.0.0.1) (32 bits)
ipv6_header = ( '6' # IP version
'00' # Traffic class, DSCP, ECN
'3F B7 7' # Flow label <- randomly set to this 3fb77, it does not have any specific meaning now
'XX XX' # Length (16 bits) in bytes including extension headers if there is any + PDU- will be calucalted and replaced later
'PP' # Next header (protocol set to TCP here as only TCP SYNs are supported for now)
'FF' # Hop limit - set to max 255
'SS SS SS SS SS SS SS SS SS SS SS SS SS SS SS SS' # Source IP (128 bits) - will be replaced later
'DD DD DD DD DD DD DD DD DD DD DD DD DD DD DD DD' # Dest IP (128 bits) - will be replaced later
)
tcp_syn_header= ('ZZ ZZ' # Source port - will be replaced later
'XX XX' # Destination port - will be replaced later
'NN NN NN NN' # SEQ number - will be replaced later
'00 00 00 00' # ACK number - set to 0 as being SYN packet
'L' # header length - calculated later (in 32-bit words)
'00' # reserved (3bit), nonce (1bit), flags (CWR,ECE,URG,ACK) (4bit)
'2' # flags (4bit) (ACK,PSH,SYN,FIN -> hex(0b0010) -> 2) - it's set to 2 to indicate SYN packet
'20 00' # window - set randomly
'CC CC' # checksum - will be replaced later
'00 00' # urgent pointer - 00 00 by default
'02 04 05 78' #TCP option - Max Segment Size - set to 1400 bytes
### IF YOU WANT MORE TCP OPTIONS HERE, DO IT BELOW ####
'04 02' # TCP option - SACK permitted
'08 0A TT TT TT TT 00 00 00 00' # TCP option timestamp - 08 timestamp, 0a (length - 10), TT... timestamp, 00... timestamp echo reply=0 by default
'01' #TCP option - No-Operation
'03 03 07' #TCP window scale (03), length (03), set multiplier to 7 (multiply by 128)'
)
udp_header = ('ZZ ZZ' # Source port - will be replaced later
'XX XX' # Destination Port - will be replaced later
'YY YY' # Length - will be calculated and replaced later
'CC CC') # UDP checksum - it is optional in IPv4 but MANDATORY in IPv6 - so we calculate it to be sure
gtp_header = ('30' # Version(3), Proto type(1) and other zero fields
'FF' # Type: T-PDU
'LL LL' # Length - will be calculated later
'TE ID TE ID') # TEID - will be added later
ETHER_TYPES_ALLOWED = ['ipv4', 'ipv6']
PROTOS_ALLOWED = ['udp', 'tcp_syn'] #currently, we do not support more protos than pure UDP or TCP_SYN
def _reverseEndian(hexstring):
#create a list of 2-characters of the input
big_endian = re.findall('..', hexstring)
little_endian=""
for i in reversed(big_endian):
little_endian+=i
return little_endian
def createTimestamp(**kwargs):
# this is a timestamp in seconds.microseconds, e.g., 1570435931.7557144
_time = kwargs.get('time', time.time())
reverse = kwargs.get('reverse', False)
#check for float type
if isinstance(_time,float):
_time="%.8f" % _time # str(time) is not working well below python3 as floats become reduced to two decimals only
#split it to seconds and microseconds
_time=_time.split('.')
# time is a list now
sec = format(int(_time[0]), '08x')
usec = format(int(_time[1]), '08x')
if(reverse):
sec = _reverseEndian(sec)
usec = _reverseEndian(usec)
return (sec,usec)
def getByteLength(str1):
return int(len(''.join(str1.split())) / 2)
first_byte_to_write = True
def writeByteStringToFile(bytestring, filename):
bytelist = bytestring.split()
bytes = binascii.a2b_hex(''.join(bytelist))
bitout = open(filename, 'ab')
bitout.write(bytes)
def backspace(n):
# print((b'\x08' * n).decode(), end='') # use \x08 char to go back
sys.stdout.write('\r' * n) # use '\r' to go back
def rawcount(filename):
'''
Ultrafast way to count number of lines in a file. Comes pretty handy when parsing the csv file and we want to show its progress.
comes from here: https://stackoverflow.com/questions/845058/how-to-get-line-count-of-a-large-file-cheaply-in-python
Works with python3! python2 might not be sufficient for the raw interface
'''
f = open(filename, 'rb')
lines = 0
buf_size = 1024 * 1024
read_f = f.raw.read
buf = read_f(buf_size)
while buf:
lines += buf.count(b'\n')
buf = read_f(buf_size)
return lines
def calculateRemainingPercentage(message, current, n):
percent = str(message + ": %d%%" % (int((current / float(n)) * 100)))
if(current < n):
#coloring - does not seem to work, though
percent.replace(": ", str(": {}".format(orange)) )
print(percent, end="\r")
else:
#coloring - does not seem to work, though
percent.replace(": ", str(": {}{}".format(bold,green)) )
print(percent, end="")
print("\t{}{}[DONE]{}".format(bold,green,none))
# sys.stdout.write(percent)
# backspace(len(percent)) # back for n chars
def readFile(input):
#get the number of lines in the file
num_lines = rawcount(input)
headers = list() # list of dictionaries
print("\n### PROCESSING INPUT FILE ###")
with open(input, 'r') as lines:
line_num = 1
for line in lines:
#progress status
calculateRemainingPercentage(f"|-- Parsing input file: {input}", line_num, num_lines)
#remove blank spaces
line = line.strip()
#removed blank lines
if line:
#omit commented lines
packet_counter=1
if not (line.startswith("#", 0, 1)):
#assume that the desctiption file is a CSV file and look like this:
##timestamp=123123124.123123, src_mac=<SRC_MAC>,dst_mac=<DST_MAC>, src_ip=<SRC_IP>, dst_ip<DST_IP>, src_port=<SRC_PORT>,dst_port=<DST_PORT>,gtp=<GTP_TEID>, ?? - unimplemented
#let us further assume that order is not important
one_line = line.split(',')
# this dictionary will store eventually one complete header
header = {
'timestamp':"",
'src_mac':"",
'dst_mac':"",
'src_ip':"",
'dst_ip':"",
'src_port':"",
'dst_port':"",
'gtp':"",
'ext_src_ip':"",
'ext_dst_ip':"",
'vlan':"",
'ttl':"",
'ether_type':"",
'src_ipv6':"",
'dst_ipv6':"",
'protocol':"",
'payload_needed':""
# NOTE: add more header fields here
}
for i in one_line:
#remove white spaces
i=i.strip()
#check whether there is a remaining comma at the end (strip above already makes it a zero-length
#white space, so we only need to check that
if i != "":
#OK, everything is prepared, let's start to parse the relevant data
header_row=i.split('=')
#now, we only will have key=value pairs, let's see whether they are meaningful
#note we need to iterate the whole line first, as it should not be ordered.
for h in header.keys():
if header_row[0] == h:
if h.endswith("mac"):
header[h] = parseMAC(header_row[1])
elif h.endswith('ip'):
header[h] = parseIP(header_row[1])
elif h.endswith('ipv6'):
header[h] = parseIPv6(header_row[1])
elif h.endswith('payload_needed'):
if (header_row[1].lower() == "false"): #we only have to handle false, rest are true by default
header[h] = False
elif (header_row[1].lower() == "true"): #any string converted to bool is considered as True
header[h] = True
else:
print("payload_needed cannot be parsed properly -> reverting to default True")
header[h] = True
#TODO: below could be OR-ed together, but easier to follow this way
#we basically do some quick conversion here, whether we need values as Int or String
elif h.endswith('timestamp'):
header[h] = header_row[1] #it is a string, but it can remain a string
elif h.endswith('ether_type'):
header[h] = header_row[1] #it is a string, but it can remain a string
elif h.endswith('protocol'):
header[h] = header_row[1] #it is a string, but it can remain a string
else: #all other header fields that are represented as INTEGER
#e.g., ***port,***vlan, ***gtp, ***ttl
header[h] = int(header_row[1])
# NOTE: handle here futher header fields that are different from the above
# or update the above ones to parse the new header fields accordingly
headers.append(header)
#update line_num to update progress bar
line_num+=1
#Set necessary header fields (e.g., source MAC) data to default values if csv file did contain them
for h in headers:
#inside the list
for hh in h:
#inside one header
if hh == 'timestamp' and h[hh]=="":
h[hh] = default_timestamp
if hh == 'src_mac' and h[hh]=="":
h[hh]=parseMAC(default_src_mac)
if hh == 'dst_mac' and h[hh]=="":
h[hh] = parseMAC(default_dst_mac)
if hh == 'src_ip' and h[hh] =="":
h[hh]=parseIP(default_src_ip)
if hh == 'dst_ip' and h[hh] == "":
h[hh] = parseIP(default_dst_ip)
if hh == 'src_port' and h[hh] == "":
h[hh] = default_src_port
if hh == 'dst_port' and h[hh] == "":
h[hh] = default_dst_port
if hh == 'vlan' and h[hh] == "":
h[hh] = default_vlan
if hh == 'gtp' and h[hh] == "":
h[hh] = None
if hh == "ttl" and h[hh] == "":
h[hh] = default_ttl
if hh == "protocol" and h[hh] == "":
h[hh] = default_protocol
if hh == 'src_ipv6' and h[hh] =="":
h[hh]=parseIPv6(default_src_ipv6)
if hh == 'dst_ipv6' and h[hh] =="":
h[hh]=parseIPv6(default_dst_ipv6)
if hh == 'payload_needed' and h[hh] =="":
h[hh]=True
if hh == 'ether_type' and h[hh]=="":
h[hh] = default_ether_type
#NOTE: Add here new header type
return headers
def setDefaults(**kwargs):
'''
This function will set default headers.
:param kwargs:
packet_sizes = list of packetsizes required
payload_needed = default payload_needed
src_mac = default src_mac
dst_mac = default dst_mac
src_ip = default src_ip
dst_ip = default dst_ip
ttl = default_ttl
src_port = default src_port
dst_port = default dst_port
vlan = default vlan
gtp_teid = default gtp_teid
timestamp = default timestamp
ether_type = default ether_type
src_ipv6 = default src_ipv6
dst_ipv6 = default dst_ipv6
protocol = default protocol
:return: None
'''
global default_src_mac, default_dst_mac
global default_src_ip, default_dst_ip
global default_src_port, default_dst_port
global default_vlan
global default_ttl
global packet_sizes
global verbose
global default_timestamp
global default_ether_type
global default_src_ipv6
global default_dst_ipv6
global default_protocol
#NOTE: add here your new header type
packet_sizes = []
default_src_mac = kwargs.get('src_mac')
default_dst_mac = kwargs.get('dst_mac')
default_src_ip = kwargs.get('src_ip')
default_dst_ip = kwargs.get('dst_ip')
default_src_port = int(kwargs.get('src_port')) #CONVERT TO INT
default_dst_port = int(kwargs.get('dst_port')) #CONVERT TO INT
default_vlan = kwargs.get('vlan') #IS NOT CONVERTED TO INT as default is None
gtp_teid = kwargs.get('gtp_teid') #IS NOT CONVERTED TO INT as default is None
verbose = kwargs.get('verbose')
default_timestamp = kwargs.get('timestamp')
default_ttl = int(kwargs.get('ttl')) #CONVERT TO INT
default_ether_type = kwargs.get('ether_type')
default_src_ipv6 = kwargs.get('src_ipv6')
default_dst_ipv6 = kwargs.get('dst_ipv6')
default_protocol = kwargs.get('protocol')
#NOTE: add here your new header type
if default_vlan is not None:
default_vlan = int(default_vlan)
ps = kwargs.get('packet_sizes')
for i in ps:
packet_sizes.append(int(i))
def generateRandomHeaders(num_packets):
'''
This function generates random packets, as an alternative to reading from a csv file.
:param num_packets: number of packets to generate
:return: the generated headers in a list
'''
headers = []
for i in range(num_packets):
calculateRemainingPercentage("|-- Generating random headers", i, num_packets-1)
header = {
'timestamp': default_timestamp,
'src_mac': getRandomMAC(),
'dst_mac': getRandomMAC(),
'src_ip': getRandomIP(),
'dst_ip': getRandomIP(),
'src_port': default_src_port,
'dst_port': default_dst_port,
'gtp': None,
'ext_src_ip':"",
'ext_dst_ip':"",
'vlan': default_vlan,
'ttl': default_ttl,
'ether_type': "ipv4",
'src_ipv6': parseIPv6(default_src_ipv6),
'dst_ipv6': parseIPv6(default_dst_ipv6),
'protocol': default_protocol,
'payload_needed': True
}
headers.append(header)
return headers
def generateFromHeaders(headers, pcapfile, **kwargs):
'''
This function will read the input file and creates a pcap from its content
:param inputfile: input file to read
:param pcapfile: pcap output file
:return: None
'''
n=len(headers)
print("\n### PCAP GENERATION ###")
# write out header information to file - 5-tuples will be printed in an .nfo files as well
for i in range(1, int(n) + 1):
# print out the remaining percentage to know when the generate will finish
calculateRemainingPercentage("|-- Generating packets in all packet sizes required", i, int(n))
# set here the header variables
timestamp = headers[i-1]['timestamp']
#Get/calculate timestamp
if timestamp is None: #timestamp was not set, use current time
time = createTimestamp(reverse=True)
else:
time = createTimestamp(time=timestamp, reverse=True)
#recall, time is a tuple (sec, usec)
#L2 addresses
src_mac = headers[i-1]['src_mac']
dst_mac = headers[i-1]['dst_mac']
#L2 vlan
vlan = headers[i-1]['vlan']
#L3 - IPv4 addresses + TTL
src_ip = headers[i-1]['src_ip']
dst_ip = headers[i-1]['dst_ip']
ttl = headers[i-1]['ttl']
#L3 - IPv6 addresses
src_ipv6 = headers[i-1]['src_ipv6']
dst_ipv6 = headers[i-1]['dst_ipv6']
# TODO: add IPv6 TTL field if needed
# L4 ports and protocol
sport = headers[i-1]['src_port']
dport = headers[i-1]['dst_port']
protocol = headers[i-1]['protocol']
#PAYLOAD NEEDED?
payload_needed = headers[i-1]['payload_needed']
#Let's keep track of the full header size to later generate padding if needed w.r.t. the required packet size
full_header_length = 0
#ETHER_TYPE
ether_type = headers[i-1]['ether_type']
if ether_type == "ipv4":
eth_type = "08 00" #we need the ether_type variable later, so create a new one to be used below only (from line 484)
IPv6 = False #indicator for easier handling later
else: # ipv6
eth_type = "86 DD"
IPv6 = True #indicator for easier handling later
# GTP layer (if any)
gtp_teid = headers[i-1]['gtp']
ext_src_ip = headers[i-1]['ext_src_ip']
ext_dst_ip = headers[i-1]['ext_dst_ip']
#VLAN HANDLING - it requires other eth_type and additional headers
if vlan is None:
# update ethernet header for each packet
eth_header = dst_mac + ' ' + src_mac + eth_type # append ether_type to indicate ipv4/6
else:
eth_header = dst_mac + ' ' + src_mac + \
'81 00' + \
'0V VV' + \
eth_type # append ether_type to indicate ipv4/6 - #TODO: not sure about VLAN + IPv6
# update vlan header
eth_header = eth_header.replace('0V VV', "0%03x" % vlan)
#update full header length
full_header_length += getByteLength(eth_header)
# +----------------+
# | IPv4 packet |
# +----------------+
if not IPv6:
# +----------------+
# | GTP packet | # NOTE: GTP only available for IPv4
# +----------------+
# GTP tunneling: it requires additional headers
if gtp_teid is not None:
gtp = gtp_header
gtp = gtp.replace('TE ID TE ID', "%08x" % gtp_teid)
#update full header length
full_header_length += getByteLength(gtp)
# generate the external headers
gtp_dport = 2152
gtp_sport = 2152
ext_udp = udp_header.replace('XX XX', "%04x" % gtp_dport)
ext_udp = ext_udp.replace('ZZ ZZ', "%04x" % gtp_sport)
ext_ip = ip_header
ext_ip = ext_ip.replace('SS SS SS SS', ext_src_ip)
ext_ip = ext_ip.replace('DD DD DD DD', ext_dst_ip)
ext_ip = ext_ip.replace('TT',"%02x" % ttl)
ext_ip = ext_ip.replace('PP', '11')
#update full header length
full_header_length += getByteLength(ext_ip)
# +-------------------------+
# | IPv4 header assembly |
# +-------------------------+
# update ip header - see on top how it looks like (the last bytes are encoding the IP address)
ip = ip_header
#update source IP
ip = ip.replace('SS SS SS SS', src_ip)
#update destination IP
ip = ip.replace('DD DD DD DD', dst_ip)
#update ttl
ip = ip.replace('TT', "%02x" % ttl)
#update protocol
if protocol == "udp":
ip = ip.replace('PP', '11') #17 for UDP
else:
ip = ip.replace('PP', '06') #6 for TCP
#update full header length
full_header_length += getByteLength(ip)
# +----------------+
# | IPv6 packet |
# +----------------+
else:
# +-------------------------+
# | IPv6 header assembly |
# +-------------------------+
# update ipv6 header - see on top how it looks like (the last bytes are encoding the IP address)
ipv6 = ipv6_header
ipv6 = ipv6.replace('SS SS SS SS SS SS SS SS SS SS SS SS SS SS SS SS', src_ipv6)
ipv6 = ipv6.replace('DD DD DD DD DD DD DD DD DD DD DD DD DD DD DD DD', dst_ipv6)
if protocol == "udp":
ipv6 = ipv6.replace('PP', '11')
else:
ipv6 = ipv6.replace('PP', '06')
#ipv6.replace('XX XX', header_length)
#update full header length
full_header_length += getByteLength(ipv6)
# +----------------+
# | UDP proto |
# +----------------+
if protocol == "udp":
# update ports
udp = udp_header.replace('XX XX', "%04x" % dport)
udp = udp.replace('ZZ ZZ', "%04x" % sport)
#update full header length
full_header_length += getByteLength(udp)
# +----------------+
# | TCP_SYN proto |
# +----------------+
elif protocol == "tcp_syn":
tcp_syn = tcp_syn_header.replace('XX XX', "%04x" % dport)
tcp_syn = tcp_syn.replace('ZZ ZZ', "%04x" % sport)
tcp_syn = tcp_syn.replace('NN NN NN NN', "%08x" % random.randint(1,65535))
#tcp_syn_length requires length in the number of 32-bit words, hence we divide by 4
tcp_syn_len = int(getByteLength(tcp_syn) / 4)
tcp_syn = tcp_syn.replace('L', "%01x" % tcp_syn_len)
#timestamp in the TCP header - we use our time tuple, which is (sec, usec)
#we only need sec here, no usec
tcp_syn = tcp_syn.replace('TT TT TT TT', time[0])
# tcp_syn = tcp_syn.replace('CC CC', checksum)
#update full header length
full_header_length += getByteLength(tcp_syn)
#TODO: else other protocols/subprotocols, e.g., TCP SYN-ACK, TCP ACK
# create packets with the different packet sizes but with the same 5-tuple
for pktSize in packet_sizes:
#TODO: would make more sense to not have the for loop at all if no payload is needed
# but would require too much of refactoring :(
if payload_needed:
# generate the packet payload (random) w.r.t. the full_header_length kept track on before
message = getMessage(pktSize, full_header_length)
else:
message = ''
message_len = getByteLength(message)
ip_len = 0 #initialize IP length with 0
#### ONCE we get the packet payload, we can calculate payload length, protocol length, checksum, etc. ####
if protocol == "udp":
# generate the headers (in case of tunneling: internal headers)
udp_len = message_len + getByteLength(udp_header)
udp = udp.replace('YY YY', "%04x" % udp_len)
ip_len = udp_len #add UDP + payload length to IP length
elif protocol == "tcp_syn":
# print("tcp header length: {}".format(getByteLength(tcp_syn_header)))
ip_len = message_len + getByteLength(tcp_syn_header) #add tcp syn length + payload to ip len
#we don't use TCP protocol length as it is in the TCP header, but will use this info in the IP length and PCAP length later on
# print(ether_type)
if ether_type == "ipv4":
# +----------------+
# | IPv4 packet |
# +----------------+
# update ip_len with IPv4 header size
ip_len += getByteLength(ip_header)
ip = ip.replace('XX XX', "%04x" % ip_len)
# print(ip)
checksum = ip_checksum(ip.replace('YY YY', '00 00')) # we have to temporarily overwrite the field to 0000 to make it sense as YYYY is not hex
ip = ip.replace('YY YY', "%04x" % checksum)
tot_len = ip_len
#UDP checksum calculation
if protocol == "udp":
#+-----------------------+
#| UDPv4 CHECKSUM CALC |
#+-----------------------+
udp_checksum = ip_checksum(
src_ip + #src ip
dst_ip + #dst ip
'00 11' + #8w0 + protocol
str("%04x" % udp_len) + #udp length in 16 bit
udp.replace('CC CC', '') + #udp header without checksum, so we remove it from the header when calculating it, replacing it with 00 00 would work, too
message
)
udp = udp.replace('CC CC', "%04x" % udp_checksum)
#TCP checksum calculation
if protocol == "tcp_syn":
#+-----------------------+
#| TCPv4 CHECKSUM CALC |
#+-----------------------+
tcp_checksum = (ip_checksum(
src_ip + #src IP
dst_ip + #dst IP
'00 06' + #8w0 + protocol
str("%04x" % (tot_len - getByteLength(ip_header))) + #tcp header+data length -> ip_total_length - ip_header_length
tcp_syn.replace('CC CC','') + #tcp header without the checksum, so we remove it from the header when calculating it, replacing it with 00 00 would work, too
message
)
)
tcp_syn = tcp_syn.replace('CC CC', "%04x" % tcp_checksum)
# encapsulation (external header) #TODO: FIX GTP
if gtp_teid is not None: # GTP is only supported for IPv4 and UDP packets
gtp_len = ip_len
gtp = gtp.replace('LL LL', "%04x" % gtp_len)
# print(gtp)
# generate the external headers
ext_udp_len = gtp_len + getByteLength(gtp) + getByteLength(udp_header)
ext_udp = ext_udp.replace('YY YY', "%04x" % ext_udp_len)
#GTP UTP IPv4 packets have checksum of 00 00
#TODO: calculate it properly
ext_udp = ext_udp.replace('CC CC', '00 00')
# print(ext_udp)
ext_ip_len = ext_udp_len + getByteLength(ip_header)
if ext_ip_len > 1500:
print("WARNING! Generating >MTU size packets: {}".format(ext_ip_len))
ext_ip = ext_ip.replace('XX XX', "%04x" % ext_ip_len)
checksum = ip_checksum(ext_ip.replace('YY YY', '00 00'))
ext_ip = ext_ip.replace('YY YY', "%04x" % checksum)
tot_len = ext_ip_len
else:
# +----------------+
# | IPv6 packet |
# +----------------+
ipv6 = ipv6.replace('XX XX', "%04x" % ip_len)
checksum = ip_checksum(ipv6.replace('YY YY', '00 00'))
ipv6 = ipv6.replace('YY YY', "%04x" % checksum)
# update ip_len with IPv6 header size
ip_len += getByteLength(ipv6_header)
tot_len = ip_len
#UDP checksum calculation
if protocol == "udp":
#+-----------------------+
#| UDPv6 CHECKSUM CALC |
#+-----------------------+
udp_checksum = ip_checksum(
src_ipv6 + #src ipv6
dst_ipv6 + #dst ipv6
str("%08x" % udp_len) + #udp length in 16 bit
'00 00 00 11' + #24w0 + protocol/next header
udp.replace('CC CC', '') + #udp header without checksum, so we remove it from the header when calculating it (replacing it with 00 00 would work, too)
message
)
udp = udp.replace('CC CC', "%04x" % udp_checksum)
#TCP checksum calculation
if protocol == "tcp_syn":
#+-----------------------+
#| TCPv4 CHECKSUM CALC |
#+-----------------------+
tcp_checksum = (ip_checksum(
src_ipv6 + #src IPv6
dst_ipv6 + #dst IPv6
str("%08x" % (tot_len - getByteLength(ipv6_header))) + #tcp header+data length -> ip_total_length - ip_header_length
'00 00 00 06' + #24w0 + protocol/next header
tcp_syn.replace('CC CC','') + #tcp header without the checksum, so we remove it from the header when calculating it, replacing it with 00 00 would work, too
message
)
)
tcp_syn = tcp_syn.replace('CC CC', "%04x" % tcp_checksum)
# print(tot_len)
pcap_len = tot_len + getByteLength(eth_header)
hex_str = "%08x" % pcap_len
reverse_hex_str = hex_str[6:] + hex_str[4:6] + hex_str[2:4] + hex_str[:2]
pcaph = pcap_packet_header.replace('XX XX XX XX', reverse_hex_str)
pcaph = pcaph.replace('YY YY YY YY', reverse_hex_str)
#using the timestamp values stored in time, we append it to the PCAP header
pcaph = pcaph.replace('T1 T1 T1 T1', time[0]) # time[0] is seonds
pcaph = pcaph.replace('T2 T2 T2 T2', time[1]) # time[1] is useonds
# at the first packet we need the global pcap header
if i == 1:
pcap_init = pcap_global_header + pcaph
# otherwise, we do not need the pcap global header
else:
pcap_init = pcaph
if ether_type == "ipv4":
# +----------------+
# | IPv4 packet |
# +----------------+
if protocol == "udp":
# +----------------+
# | UDP packet |
# +----------------+
if gtp_teid is not None:
# +----------------+
# | GTP packet |
# +----------------+
bytestring = pcap_init + eth_header + ext_ip + ext_udp + gtp + ip + udp + message
else:
bytestring = pcap_init + eth_header + ip + udp + message
elif protocol == "tcp_syn":
# +----------------+
# | TCP packet |
# +----------------+
if gtp_teid is not None:
# +----------------+
# | GTP packet |
# +----------------+
bytestring = pcap_init + eth_header + ext_ip + ext_udp + gtp + ip + tcp_syn + message
else:
bytestring = pcap_init + eth_header + ip + tcp_syn + message
#NOTE: implement further protocol here
else:
# +----------------+
# | IPv6 packet |
# +----------------+
if protocol == "udp":
# +----------------+
# | UDP packet |
# +----------------+
bytestring = pcap_init + eth_header + ipv6 + udp + message
elif protocol == "tcp_syn":
# +----------------+
# | TCP packet |
# +----------------+
bytestring = pcap_init + eth_header + ipv6 + tcp_syn + message
# this function is writing out pcap file per se
if verbose:
print("Packet to be written out:\n{}".format(headers[i-1]))
writeByteStringToFile(bytestring, pcapfile + str(".%dbytes.pcap" % pktSize))
def getRandomMAC():
return "1a" + str("%0.10X" % random.randint(1,0xffffffffff))
def getRandomIP():
# to avoid multicast addresses (range is between 0.0.0.0/8 and 223.255.255.255)
ip = str("%0.8X" % random.randint(0x01000000,0xdfffffff))
#avoid others 127.0.0.0/8 - hex(127)=7F
while ip.startswith("7F"):
#print "Ooops, accidentally a 127.0.0.0/8 IP was generated...REGENERATING!"
ip = str("%0.8X" % random.randint(0x01000000, 0xdfffffff))
return ip
def getRandomPort(**args):
'''
Use 'exlude=[XXX]' to exlude a list of ports (even 1 port has to be defined as a list)
'''
port = random.randint(1,65535)
exlude = args.get("exclude", [4305])
if(port in exlude):
getRandomPort()
return int(port)
def parseMAC(mac):
ret_val=mac.replace(":","").upper()
if len(ret_val) != 12: #check mac address length
print("ERROR during parsing mac address - not long enough!: {}".format(mac))
exit(-1)
return ret_val
def parseIPv6(ip):
ret_val=ip.replace(":","").upper()
if len(ret_val) != 32: #check ipv6 address length
#TODO: implement compressed ipv6 addresses, i.e., omitted zeros and ::
print("ERROR during parsing ipv6 address - not long enough, please DO NOT OMIT 0s!: {}".format(ip))
exit(-1)
return ret_val
def parseIP(ip):
ret_val = ""
#split IP address into 4 8-bit values
ip_segments=ip.split(".")
for i in ip_segments:
ret_val+=str("%0.2X" % int(i))
if len(ret_val) != 8: #check length of IP
print("ERROR during parsing IP address - not long enough!: {}".format(ip))
exit(-1)
return ret_val
def splitN(str1, n):
return [str1[start:start + n] for start in range(0, len(str1), n)]
# Calculates and returns the IP checksum based on the given IP Header
def ip_checksum(iph):
# print("---- CHECKSUM CALC ----")
# print(iph)
# split into bytes
words = splitN(''.join(iph.split()), 4)
csum = 0
for word in words:
csum += int(word, base=16)
csum += (csum >> 16)
csum = csum & 0xFFFF ^ 0xFFFF
return csum
def getMessage(packetsize, header_length):
'''
This function creates random message to pad the packet as payload w.r.t. the header_length and the required packet size
If header size is already bigger than the required packetsize, null-char is returned as message
For instance, if packet size is 64B and protocol is TCP, then no message will be appended as TCP header already makes the packet size to go beyond 64
@params
int packetsize = required final packet size
int header_length = the size of the already used headers
'''
message = ''
header_length = header_length + 4 #we have to calculate with the checksum appended by the interface itself when sent out (offloaded to the hardware on modern systems)
#check if header_length is already enough for the required packet size
if header_length > packetsize:
return message
#otherwise fill the message with random numbers as HEX chars
for i in range(0, int(packetsize) - header_length):
message += "%0.2X " % random.randint(0, 255)
return message
def showHelp():
print("{}usage: pcap_generator_from_csv.py <input_csv_file> <desired_output_pcapfile_prefix>{}".format(bold,none))
print('Example: ./pcap_generator_from_csv.py input.csv output')
print("{}Note: Existing files with the given <desired_output_pcapfile_prefix>.[PacketSize].pcap will be overwritten!{}".format(yellow,none))
print("This python script generates pcap files according to the header information stored in a CSV file")
print("See 'input.csv' file for CSV details")
print("")
print("Supported header fields: {}\n" \
" VLAN \n" \
" L2 (src and dst MAC, ethertype) \n" \
" L3 (src and dst IPv4/IPv6, TTL) \n" \