Lots of junk

This commit is contained in:
Neale Pickett 2008-07-21 17:52:35 -06:00
parent a6f0d4f30a
commit ef0b4ac1e3
3 changed files with 54 additions and 8 deletions

View File

@ -6,7 +6,7 @@
import sys import sys
import struct import struct
printable = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}`~/=-\\?+|\',."<>: ' printable = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}`~/=-\\?+|\',."<>: _'
def unpack(fmt, buf): def unpack(fmt, buf):
"""Unpack buf based on fmt, return the rest as a string.""" """Unpack buf based on fmt, return the rest as a string."""

View File

@ -50,6 +50,11 @@ class GapString:
for c in i: for c in i:
yield c yield c
def hasgaps(self):
for i in self.contents:
if isinstance(i, int):
return True
return False
def hexdump(self, fd=sys.stdout): def hexdump(self, fd=sys.stdout):
offset = 0 offset = 0

55
ip.py
View File

@ -116,6 +116,7 @@ class Frame:
^ self.daddr ^ (self.dport or 0)) ^ self.daddr ^ (self.dport or 0))
else: else:
self.name = 'Ethernet type %d' % self.eth_type self.name = 'Ethernet type %d' % self.eth_type
self.protocol = None
def get_src_addr(self): def get_src_addr(self):
@ -271,11 +272,11 @@ class TCP_Resequence:
if pkt.flags == SYN: if pkt.flags == SYN:
self.cli, self.srv = pkt.src, pkt.dst self.cli, self.srv = pkt.src, pkt.dst
elif pkt.flags == (SYN | ACK): elif pkt.flags == (SYN | ACK):
assert (pkt.src == (self.srv or pkt.src)) #assert (pkt.src == (self.srv or pkt.src))
self.cli, self.srv = pkt.dst, pkt.src self.cli, self.srv = pkt.dst, pkt.src
self.seq = [pkt.ack, pkt.seq + 1] self.seq = [pkt.ack, pkt.seq + 1]
elif pkt.flags == ACK: elif pkt.flags == ACK:
assert (pkt.src == (self.cli or pkt.src)) #assert (pkt.src == (self.cli or pkt.src))
self.cli, self.srv = pkt.src, pkt.dst self.cli, self.srv = pkt.src, pkt.dst
self.seq = [pkt.seq, pkt.ack] self.seq = [pkt.seq, pkt.ack]
self.handle = self.handle_packet self.handle = self.handle_packet
@ -345,11 +346,11 @@ def resequence(pc):
f = Frame(pkt) f = Frame(pkt)
if f.protocol == TCP: if f.protocol == TCP:
# compute TCP session hash # compute TCP session hash
s = sessions.get(f.hash) tcp_sess = sessions.get(f.hash)
if not s: if not tcp_sess:
s = TCP_Resequence() tcp_sess = TCP_Resequence()
sessions[f.hash] = s sessions[f.hash] = tcp_sess
chunk = s.handle(f) chunk = tcp_sess.handle(f)
if chunk: if chunk:
yield chunk yield chunk
@ -481,6 +482,7 @@ class Packet(UserDict.DictMixin):
""" """
self.parts = [data] self.parts = [data]
self.payload = data
return None return None
def handle(self, data): def handle(self, data):
@ -557,3 +559,42 @@ class Session:
for chunk in resequence(collection): for chunk in resequence(collection):
self.handle(chunk) self.handle(chunk)
self.done() self.done()
class HtmlSession(Session):
def __init__(self, frame):
Session.__init__(self)
self.uid = '%s:%d-%s:%d' % (frame.src_addr, frame.sport,
frame.dst_addr, frame.dport)
self.sessionfile = 'transfers/session-%s.html' % self.uid
self.fn = '%s.html' % (self.infilename)
self.fd = file(self.fn, 'w')
self.fd.write('''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>%s</title>
<style type="text/css">
.client { background-color: blue; color: white; }
</style>
</head>
<body>
''' % self.__class__.__name__)
self.fd.write('<h1>%s</h1>\n' % self.__class__.__name__)
self.fd.write('<pre>')
self.srv = None
def __del__(self):
self.fd.write('</pre></body></html>')
def log(self, frame, payload):
if frame.saddr == self.srv:
cls = 'server'
else:
cls = 'client'
self.fd.write('<span class="%s" title="%s(%s)">' % (cls, time.ctime(frame.time), frame.time))
self.fd.write(payload.replace('\r\n', '\n'))
self.fd.write('</span>')