ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to use download.pcap file with this program, but when I run it, I keep getting a "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd4 in position 0: invalid continuation byte" error. I am not sure how to fix this.
Code:
import dpkt
import optparse
import socket
THRESH = 1000
def findDownload(pcap):
for (ts, buf) in pcap:
try:
eth = dpkt.ethernet.Ethernet(buf)
ip = eth.data
src = socket.inet_ntoa(ip.src)
tcp = ip.data
http = dpkt.http.Request(tcp.data)
if http.method == 'GET':
uri = http.uri.lower()
if '.zip' in uri and 'loic' in uri:
print ('[!] ' + src + ' Downloaded LOIC.')
except:
pass
def findHivemind(pcap):
for (ts, buf) in pcap:
try:
eth = dpkt.ethernet.Ethernet(buf)
ip = eth.data
src = socket.inet_ntoa(ip.src)
dst = socket.inet_ntoa(ip.dst)
tcp = ip.data
dport = tcp.dport
sport = tcp.sport
if dport == 6667:
if '!lazor' in tcp.data.lower():
print ('[!] DDoS Hivemind issued by: '+src)
print ('[+] Target CMD: ' + tcp.data)
if sport == 6667:
if '!lazor' in tcp.data.lower():
print ('[!] DDoS Hivemind issued to: '+src)
print ('[+] Target CMD: ' + tcp.data)
except:
pass
def findAttack(pcap):
pktCount = {}
for (ts, buf) in pcap:
try:
eth = dpkt.ethernet.Ethernet(buf)
ip = eth.data
src = socket.inet_ntoa(ip.src)
dst = socket.inet_ntoa(ip.dst)
tcp = ip.data
dport = tcp.dport
if dport == 80:
stream = src + ':' + dst
if pktCount.has_key(stream):
pktCount[stream] = pktCount[stream] + 1
else:
pktCount[stream] = 1
except:
pass
for stream in pktCount:
pktsSent = pktCount[stream]
if pktsSent > THRESH:
src = stream.split(':')[0]
dst = stream.split(':')[1]
print ("[+] "+src+" attacked "+dst+" with " \
+ str(pktsSent) + " pkts.")
def main():
parser = optparse.OptionParser("usage %prog '+\
'-p <pcap file> -t <thresh>"
)
parser.add_option("-p", dest='pcapFile', type="string",\
help='specify pcap filename')
parser.add_option("-t", dest="thresh", type="int",\
help="specify threshold count ")
(options, args) = parser.parse_args()
if options.pcapFile == None:
print (parser.usage)
exit(0)
if options.thresh != None:
THRESH = options.thresh
pcapFile = options.pcapFile
f = open(pcapFile)
pcap = dpkt.pcap.Reader(f)
with open(pcapFile, 'rb') as f:
pcap = dpkt.pcap.Reader(f)
findDownload(pcap)
with open(pcapFile, 'rb') as f:
pcap = dpkt.pcap.Reader(f)
findHivemind(pcap)
with open(pcapFile, 'rb') as f:
pcap = dpkt.pcap.Reader(f)
findAttack(pcap)
if __name__ == "__main__":
main()
without knowing that pcap file hard to say anything, probably the message is correct, the file is corrupted. Also would be nice to post the full error message, not only one line.
From the other hand you can use open like this (for example):
Code:
with open(filename, encoding="something") as datafile:
# work on datafile here
Generally speaking, you can sometimes deal with that by calling .decode('utf-8') or .encode('utf-8) on the strings. This works if the data is actually UTF-8.
If NevemTeve is correct (and he is), then the usual way to deal with it is to hex dump the data. Not to print it.
I don't know if you're familiar with hex dumps, but there's a Wikipedia article about them:
There is, in fact, a hexdump utility which can, among other things, print the data in rows of hexadecimal bytes with the corresponding characters (if printable ASCII) beside them.
A data stream is certainly not UTF-encoded. Any algorithm which is told that it is, will try to decode it and probably fail ... although it may seem to succeed some of the time.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.