Advertisement
timurgaliev

Task3

Sep 25th, 2022
1,071
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.60 KB | None | 0 0
  1. from scapy.all import *
  2. from scapy.layers.dns import DNS, DNSQR
  3. import os
  4.  
  5. from pyspark.sql import SparkSession
  6. from pyspark.conf import SparkConf
  7. from pyspark.sql.types import StructType, StructField, StringType, IntegerType
  8.  
  9.  
  10.  
  11. if __name__ == "__main__":
  12.     dir_path = r'/home/timur/Documents/testing 3/'
  13.     res_pcap = []
  14.     # Iterate directory
  15.     for file in os.listdir(dir_path):
  16.         # check only text files
  17.         if file.endswith('.pcap'):
  18.             res_pcap.append(file)  
  19.  
  20.     #conf1 = SparkConf().setAll([('spark.executor.memory', '3g'), ('spark.executor.cores', '2'), ('spark.cores.max', '2'), ('spark.driver.memory','6g')])
  21.     #spark.stop()
  22.     #spark = pyspark.SparkContext(conf=conf)
  23.     spark = SparkSession.builder.getOrCreate()
  24.     spark.sparkContext._conf.getAll()
  25.     conf = spark.sparkContext._conf.setAll([('spark.executor.memory', '500m'), ('spark.app.name', 'Spark Updated Conf'), ('spark.executor.cores', '5'), ('spark.cores.max', '5'), ('spark.executor.instances', '20') , ('spark.driver.cores', '2'), ('spark.driver.memory','4g')])
  26.     spark.sparkContext.stop()
  27.     spark = SparkSession.builder.config(conf=conf).getOrCreate()
  28.  
  29.  
  30.     schema = StructType([\
  31.                     StructField("source", StringType(), True),\
  32.                     StructField("destination", StringType(), True),\
  33.                     StructField("ip_type", StringType(), True), \
  34.                     StructField("proto", StringType(), True), \
  35.                     StructField("sport", IntegerType(), True), \
  36.                     StructField("dport", IntegerType(), True), \
  37.                     StructField("qname", StringType(), True), \
  38.                     StructField("qtype", StringType(), True), \
  39.                     StructField("qclass", StringType(), True), \
  40.                     StructField("rrname", StringType(), True), \
  41.                     StructField("rtype", StringType(),  True), \
  42.                     StructField("rclass", StringType(), True), \
  43.                     StructField("ttl", IntegerType(), True), \
  44.                     StructField("rdlen", StringType(), True), \
  45.                     StructField("rdata", StringType(), True), \
  46.                     StructField("rname", StringType(), True)
  47.                     ])
  48.  
  49.     df = spark.createDataFrame(data = [], schema=schema)
  50.  
  51.     #res_pcap = ['pcap4.pcap']
  52.  
  53.     types = {0: 'ANY', 255: 'ALL',1: 'A', 2: 'NS', 3: 'MD', 4: 'MD', 5: 'CNAME',
  54.          6: 'SOA', 7:  'MB',8: 'MG',9: 'MR',10: 'NULL',11: 'WKS',12: 'PTR',
  55.          13: 'HINFO',14: 'MINFO',15: 'MX',16: 'TXT',17: 'RP',18: 'AFSDB',
  56.          28: 'AAAA', 33: 'SRV',38: 'A6',39: 'DNAME'}
  57.     protocols =  {17: 'UDP'}
  58.     classes = {1: 'IN'}
  59.  
  60.     data = []
  61.  
  62.  
  63.     for pcap in res_pcap:
  64.         dns_packets = PcapReader(pcap).read_all()
  65.         for packet in dns_packets:
  66.             if packet.haslayer(DNS):
  67.                 source, destination, ip_type, proto, sport, dport = None, None, None, None, None, None
  68.                 qname, qtype, qclass = None, None, None
  69.                 rrname, rtype, rclass, ttl, rdlen, rdata, rname = None, None, None, None, None, None, None
  70.  
  71.  
  72.                 if (packet[Ether].type == 2048):                #IPv4
  73.                     source = packet[IP].src
  74.                     destination = packet[IP].dst
  75.                     ip_type = "IPv4"
  76.                     proto = protocols[packet[IP].proto]
  77.                 elif (packet[Ether].type == 34525):             #IPv6
  78.                     source = packet[IPv6].src
  79.                     destination = packet[IPv6].dst
  80.                     ip_type = "IPv6"
  81.                     proto = protocols[packet[IPv6].nh]
  82.                 sport = packet[UDP].sport
  83.                 dport = packet[UDP].dport
  84.  
  85.                 if (packet[DNS].qdcount != 0):
  86.                     qname = "".join(map(chr, packet[DNSQR].qname))
  87.                     qtype = types[packet[DNSQR].qtype]
  88.                     qclass = classes[packet[DNSQR].qclass]
  89.                 if (packet[DNS].ancount == packet[DNS].nscount == packet[DNS].arcount == 0):
  90.                     data = [(source, destination, ip_type, proto, sport, dport, qname, qtype, qclass, \
  91.                                 rrname, rtype, rclass, ttl, rdlen, rdata, rname)]
  92.                     df2 = spark.createDataFrame(data = data, schema = schema)
  93.                     df = df.union(df2)
  94.  
  95.                 else:
  96.                     for an in range(packet[DNS].ancount-1,-1,-1):
  97.                         rrname = "".join(map(chr, packet[DNSRR][an].rrname))
  98.                         rtype = types[packet[DNSRR][an].type]
  99.                         rclass = packet[DNSRR][an].rclass
  100.                         ttl = packet[DNSRR][an].ttl
  101.                         rdlen = packet[DNSRR][an].rdlen
  102.                         try:
  103.                             rdata = "".join(map(chr, packet[DNSRR][an].rdata))
  104.                         except:
  105.                             rdata = "".join(map(str, packet[DNSRR][an].rdata))
  106.  
  107.                         data = [(source, destination, ip_type, proto, sport, dport, qname, qtype, qclass, \
  108.                                 rrname, rtype, rclass, ttl, rdlen, rdata, rname)]
  109.                         df2 = spark.createDataFrame(data = data, schema = schema)
  110.                         df = df.union(df2)
  111.  
  112.                     for ns in range(packet[DNS].nscount-1,-1,-1):
  113.                         try:
  114.                             rrname = "".join(map(chr, packet[DNSRRSOA][ns].rrname))
  115.                             rtype = types[packet[DNSRRSOA][ns].type]
  116.                             rclass = packet[DNSRRSOA][ns].rclass
  117.                             ttl = packet[DNSRRSOA][ns].ttl
  118.                             rdlen = packet[DNSRRSOA][ns].rdlen
  119.                             try:
  120.                                 rname = "".join(map(chr, packet[DNSRRSOA][ns].rname))
  121.                             except:
  122.                                 rname = "".join(map(str, packet[DNSRRSOA][ns].rname))
  123.                         except:
  124.                             rrname, rtype, rclass, ttl, rdlen, rdata, rname = None, None, None, None, None, None, None
  125.                         data = [(source, destination, ip_type, proto, sport, dport, qname, qtype, qclass, \
  126.                                 rrname, rtype, rclass, ttl, rdlen, rdata, rname)]
  127.                         df2 = spark.createDataFrame(data = data, schema = schema)
  128.                         df = df.union(df2)
  129.  
  130.                     for ar in range(packet[DNS].arcount-1,-1,-1):
  131.                         try:
  132.                             rrname = "".join(map(chr, packet[DNSRR][ar].rrname))
  133.                             rtype = types[packet[DNSRR][ar].type]
  134.                             rclass = packet[DNSRR][ar].rclass
  135.                             ttl = packet[DNSRR][ar].ttl
  136.                             rdlen = packet[DNSRR][ar].rdlen
  137.                             try:
  138.                                 rdata = "".join(map(chr, packet[DNSRR][ar].rdata))
  139.                             except:
  140.                                 rdata = "".join(map(str, packet[DNSRR][ar].rdata))
  141.                         except:
  142.                             rrname, rtype, rclass, ttl, rdlen, rdata, rname = None, None, None, None, None, None, None
  143.                         data = [(source, destination, ip_type, proto, sport, dport, qname, qtype, qclass, \
  144.                                 rrname, rtype, rclass, ttl, rdlen, rdata, rname)]
  145.                         df2 = spark.createDataFrame(data = data, schema = schema)
  146.                         df = df.union(df2)
  147.  
  148.    
  149.     #df = spark.createDataFrame(data=data, schema=schema)
  150.  
  151.     df.write.parquet("data_test16.parquet")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement