How to read and write pcap messages to txt
Today, I will talk to you about how to read pcap email and write it to txt. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
# coding=utf-8
Import string
Import urllib
Import json
From scapy.all import *
Import re
Readfile = input ("Please enter the file name to be analyzed:")
Mail_port=input ("Please enter the type of mail you want to parse (IMAP input 143Magi SMTP input 25pm POP3 input 110):)
Savefile=input ("Please enter the file name you want to save:")
Dpkt1=list ()
Dpkt2=list ()
Dpkt = rdpcap (readfile)
Print (dpkt)
Sessions = dpkt.sessions ()
Print (sessions)
For session in sessions:
Payload = baked'# the payload of one HTTP POST
For packet in sessions [session]:
Try:
# if packet ['TCP'] .dport = = int (mail_port):
For data packet dpkt2.append (str (packet [TCP] .window) print (dpkt2) except: pass
String=''.join (dpkt2)
# string=str (dpkt2)
String=string.replace ("\ r\ n", "")
String=string.replace ("\ t", "")
String=string.replace ("'b'", "")
String=string.replace (",")
Dlist=list ()
DNumb=0
I, 1
A=len (dpkt2)
Print (string)
From = '.join (re.findall (u "From: (.com >)", string))
To=''.join (re.findall (u "To: (,?? com? >)", string))
Cc=''.join (re.findall (u "Cc: (,?? com >)", string))
Bcc=''.join (re.findall (u "Bcc: (,?? com >)", string))
Subject=''.join (re.findall (u "Subject: ([A-Za-z0-9] +) X-Priority", string))
Content =''.join (re.findall (u "base64 ([A-Za-z0-9 impulse +] +) -", string)
AttachName ='. Join (re.findall (u "filename= (\".?\ ")", string)
Attach =''.join (re.findall (u "filename=\".?\ "([A-Za-z0-9 impulse +] +)--", string)
Dict= {"From": From, "To": To, "Cc": Cc, "Bcc": Bcc, "Subject": Subject, "Content": base64.b64decode (Content), "AttachName": AttachName, "Attach": Attach}
Dlist.append (dict)
DNumb=dNumb+1
F = open (savefile, "w +")
For i in range (dNumb): # turns out to be dnmb
# f.write (to)
# f.write (str (dlist [I]) + "\ r\ n")
# print (dlist [I])
For key,value in dlist [I] .items ():
Print ('{key}: {value} '.format (key = key, value = value))
F.write (str ('{key}: {value} '.format (key = key, value = value)) + "\ r\ n")
After reading the above, do you have any further understanding of how pcap messages are read and written to txt? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.