123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- import sys
- import struct
- import csv
- import time
- IP_SRC = "081.169.252.034"
- def port_dst_src_to_ip(port_dst, port_src):
- """
- Convert a destination/source port to an IP addresse asuuming
- encoding type 5 and 32 marker value bits.
- """
- ip_1 = port_dst >> 8
- ip_2 = port_dst & 0xFF
- ip_3 = port_src >> 8
- ip_4 = port_src & 0xFF
- return "%d.%d.%d.%d" % (ip_1, ip_2, ip_3, ip_4)
- def decode_single(port_dst, port_src):
- ip = port_dst_src_to_ip(port_dst, port_src)
- print("port dst=%d, port src=%d, IP= %s" % (port_dst, port_src, ip))
- def decode_file(filename_read):
- filename_write = filename_read + "_decoded_ips.txt"
- fd_read = open(filename_read, "r")
- fd_write = open(filename_write, "w")
- fd_read.readline()
- csv_reader = csv.reader(fd_read, delimiter=' ')
- cnt = 0
- for val in csv_reader:
- if len(val) < 5:
- continue
- if cnt % 200000 == 0:
- print("...")
- cnt += 1
- if val[3] != IP_SRC:
- continue
- try:
- port_dst, port_src = int(val[5]), int(val[4])
- ip = port_dst_src_to_ip(port_dst, port_src)
- #print("%d %d -> %s" % (port_dst, port_src, ip))
- fd_write.write(ip +"\n")
- except ValueError as ex:
- #print(ex)
- # integer conversion exceptions (malformed CSV)
- pass
- #time.sleep(0.5)
- fd_read.close()
- fd_write.close()
- if len(sys.argv) < 2:
- print("usage: python script.py [single|file] [destination_port] [source_port]")
- sys.exit(1)
- if sys.argv[1] == "single":
- port_dst = int(sys.argv[2])
- port_src = int(sys.argv[3])
- decode_single(port_dst, port_src)
- else:
- decode_file(sys.argv[2])
|