2007-11-12 21:11:25 -07:00
|
|
|
#! /usr/bin/python
|
|
|
|
|
2008-07-08 17:52:28 -06:00
|
|
|
## IP resequencing + protocol reversing skeleton
|
|
|
|
## 2008 Massive Blowout
|
|
|
|
|
2007-11-12 21:11:25 -07:00
|
|
|
import StringIO
|
2007-12-12 11:07:49 -07:00
|
|
|
import struct
|
|
|
|
import socket
|
2007-12-21 17:08:00 -07:00
|
|
|
import warnings
|
2008-01-10 17:53:11 -07:00
|
|
|
import heapq
|
2008-01-18 19:09:09 -07:00
|
|
|
import gapstr
|
2008-01-23 17:04:41 -07:00
|
|
|
import time
|
2008-07-21 22:24:36 -06:00
|
|
|
import pcap
|
2008-07-23 09:41:08 -06:00
|
|
|
import os
|
|
|
|
import cgi
|
|
|
|
import urllib
|
2008-01-23 17:04:41 -07:00
|
|
|
import UserDict
|
2008-07-14 17:26:02 -06:00
|
|
|
from __init__ import *
|
2007-12-12 11:07:49 -07:00
|
|
|
|
|
|
|
def unpack_nybbles(byte):
|
|
|
|
return (byte >> 4, byte & 0x0F)
|
|
|
|
|
2008-01-23 17:04:41 -07:00
|
|
|
|
2008-07-14 18:17:43 -06:00
|
|
|
IP = 0x0800
|
|
|
|
ARP = 0x0806
|
|
|
|
|
2007-12-21 17:08:00 -07:00
|
|
|
ICMP = 1
|
2007-12-30 23:30:23 -07:00
|
|
|
TCP = 6
|
|
|
|
UDP = 17
|
2007-12-21 17:08:00 -07:00
|
|
|
|
2008-07-14 18:17:43 -06:00
|
|
|
def str_of_eth(d):
|
|
|
|
return ':'.join([('%02x' % ord(x)) for x in d])
|
|
|
|
|
2007-12-12 11:07:49 -07:00
|
|
|
class Frame:
|
2008-07-14 17:26:02 -06:00
|
|
|
"""Turn an ethernet frame into relevant parts"""
|
2007-12-21 17:08:00 -07:00
|
|
|
|
|
|
|
def __init__(self, pkt):
|
2008-07-14 18:17:43 -06:00
|
|
|
((self.time, self.time_usec, _), frame) = pkt
|
2007-12-21 17:08:00 -07:00
|
|
|
|
2007-12-12 12:22:20 -07:00
|
|
|
# Ethernet
|
2007-12-12 11:07:49 -07:00
|
|
|
(self.eth_dhost,
|
|
|
|
self.eth_shost,
|
|
|
|
self.eth_type,
|
|
|
|
p) = unpack('!6s6sH', frame)
|
2008-07-14 18:17:43 -06:00
|
|
|
if self.eth_type == ARP:
|
2008-07-14 17:26:02 -06:00
|
|
|
# ARP
|
2008-07-14 18:17:43 -06:00
|
|
|
self.name, self.protocol = ('ARP', ARP)
|
2008-07-14 17:26:02 -06:00
|
|
|
(self.ar_hrd,
|
|
|
|
self.ar_pro,
|
|
|
|
self.ar_hln,
|
|
|
|
self.ar_pln,
|
|
|
|
self.ar_op,
|
|
|
|
self.ar_sha,
|
|
|
|
self.ar_sip,
|
|
|
|
self.ar_tha,
|
|
|
|
self.ar_tip,
|
|
|
|
p) = unpack('!HHBBH6si6si', p)
|
|
|
|
self.saddr = self.ar_sip
|
|
|
|
self.daddr = self.ar_tip
|
|
|
|
self.__repr__ = self.__arp_repr__
|
2008-07-14 18:17:43 -06:00
|
|
|
elif self.eth_type == IP:
|
2008-07-14 17:26:02 -06:00
|
|
|
# IP
|
|
|
|
(self.ihlvers,
|
|
|
|
self.tos,
|
|
|
|
self.tot_len,
|
2007-12-21 17:08:00 -07:00
|
|
|
self.id,
|
2008-07-14 17:26:02 -06:00
|
|
|
self.frag_off,
|
|
|
|
self.ttl,
|
|
|
|
self.protocol,
|
|
|
|
self.check,
|
|
|
|
self.saddr,
|
|
|
|
self.daddr,
|
|
|
|
p) = unpack("!BBHHHBBHii", p)
|
|
|
|
|
|
|
|
if self.protocol == TCP:
|
|
|
|
self.name = 'TCP/IP'
|
|
|
|
(self.sport,
|
|
|
|
self.dport,
|
|
|
|
self.seq,
|
|
|
|
self.ack,
|
|
|
|
x2off,
|
|
|
|
self.flags,
|
|
|
|
self.win,
|
|
|
|
self.sum,
|
|
|
|
self.urp,
|
|
|
|
p) = unpack("!HHLLBBHHH", p)
|
|
|
|
(self.off, th_x2) = unpack_nybbles(x2off)
|
|
|
|
opt_length = self.off * 4
|
|
|
|
self.options, p = p[:opt_length - 20], p[opt_length - 20:]
|
|
|
|
self.payload = p[:self.tot_len - opt_length - 20]
|
|
|
|
elif self.protocol == UDP:
|
|
|
|
self.name = 'UDP/IP'
|
|
|
|
(self.sport,
|
|
|
|
self.dport,
|
|
|
|
self.ulen,
|
|
|
|
self.sum,
|
|
|
|
p) = unpack("!HHHH", p)
|
|
|
|
self.payload = p[:self.ulen - 8]
|
|
|
|
elif self.protocol == ICMP:
|
|
|
|
self.name = 'ICMP/IP'
|
|
|
|
self.sport = self.dport = None
|
|
|
|
(self.type,
|
|
|
|
self.code,
|
|
|
|
self.cheksum,
|
|
|
|
self.id,
|
|
|
|
self.seq,
|
|
|
|
p) = unpack('!BBHHH', p)
|
|
|
|
self.payload = p[:self.tot_len - 8]
|
|
|
|
else:
|
|
|
|
self.name = 'IP Protocol %d' % self.protocol
|
|
|
|
self.sport = self.dport = None
|
|
|
|
self.payload = p
|
|
|
|
|
|
|
|
# Nice formatting
|
|
|
|
self.src = (self.saddr, self.sport)
|
|
|
|
self.dst = (self.daddr, self.dport)
|
|
|
|
|
|
|
|
# This hash is the same for both sides of the transaction
|
|
|
|
self.hash = (self.saddr ^ (self.sport or 0)
|
|
|
|
^ self.daddr ^ (self.dport or 0))
|
2007-12-21 17:08:00 -07:00
|
|
|
else:
|
2008-07-14 17:26:02 -06:00
|
|
|
self.name = 'Ethernet type %d' % self.eth_type
|
2008-07-21 17:52:35 -06:00
|
|
|
self.protocol = None
|
2007-12-12 11:07:49 -07:00
|
|
|
|
|
|
|
|
2007-12-12 12:22:20 -07:00
|
|
|
def get_src_addr(self):
|
2007-12-21 17:08:00 -07:00
|
|
|
saddr = struct.pack('!i', self.saddr)
|
|
|
|
self.src_addr = socket.inet_ntoa(saddr)
|
2007-12-12 12:22:20 -07:00
|
|
|
return self.src_addr
|
|
|
|
src_addr = property(get_src_addr)
|
|
|
|
|
|
|
|
def get_dst_addr(self):
|
2007-12-21 17:08:00 -07:00
|
|
|
daddr = struct.pack('!i', self.daddr)
|
|
|
|
self.dst_addr = socket.inet_ntoa(daddr)
|
2007-12-12 12:22:20 -07:00
|
|
|
return self.dst_addr
|
|
|
|
dst_addr = property(get_dst_addr)
|
2007-12-12 11:07:49 -07:00
|
|
|
|
|
|
|
def __repr__(self):
|
2008-07-25 17:38:30 -06:00
|
|
|
return ('<Frame %s %s:%r(%08x) -> %s:%r(%08x) length %d>' %
|
|
|
|
(self.name,
|
|
|
|
self.src_addr, self.sport, self.seq,
|
|
|
|
self.dst_addr, self.dport, self.ack,
|
|
|
|
len(self.payload)))
|
2007-11-12 21:11:25 -07:00
|
|
|
|
2008-07-14 17:26:02 -06:00
|
|
|
def __arp_repr__(self):
|
|
|
|
return '<Frame %s %s(%s) -> %s(%s)>' % (self.name,
|
2008-07-14 18:17:43 -06:00
|
|
|
str_of_eth(self.ar_sha),
|
2008-07-14 17:26:02 -06:00
|
|
|
self.src_addr,
|
2008-07-14 18:17:43 -06:00
|
|
|
str_of_eth(self.ar_tha),
|
2008-07-14 17:26:02 -06:00
|
|
|
self.dst_addr)
|
|
|
|
|
2008-01-18 19:09:09 -07:00
|
|
|
|
2008-01-01 18:55:24 -07:00
|
|
|
FIN = 1
|
|
|
|
SYN = 2
|
|
|
|
RST = 4
|
|
|
|
PSH = 8
|
|
|
|
ACK = 16
|
|
|
|
|
2008-01-10 17:53:11 -07:00
|
|
|
class TCP_Resequence:
|
2007-12-30 23:30:23 -07:00
|
|
|
"""TCP session resequencer.
|
|
|
|
|
|
|
|
>>> p = pcap.open('whatever.pcap')
|
2008-01-10 17:53:11 -07:00
|
|
|
>>> s = TCP_Resequence()
|
2007-12-30 23:30:23 -07:00
|
|
|
>>> while True:
|
|
|
|
... pkt = p.read()
|
|
|
|
... if not pkt:
|
|
|
|
... break
|
|
|
|
... f = Frame(pkt)
|
|
|
|
... r = s.handle(f)
|
|
|
|
... if r:
|
|
|
|
... print ('chunk', r)
|
2007-11-12 21:11:25 -07:00
|
|
|
|
|
|
|
This returns things in sequence. So you get both sides of the
|
|
|
|
conversation in the order that they happened.
|
|
|
|
|
|
|
|
Doesn't (yet) handle fragments or dropped packets. Does handle out
|
|
|
|
of order packets.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2007-12-21 17:08:00 -07:00
|
|
|
def __init__(self):
|
2007-11-12 21:11:25 -07:00
|
|
|
self.cli = None
|
|
|
|
self.srv = None
|
2008-07-25 17:38:30 -06:00
|
|
|
self.lastack = [None, None]
|
2007-12-21 17:08:00 -07:00
|
|
|
self.first = None
|
2007-11-12 21:11:25 -07:00
|
|
|
self.pending = [{}, {}]
|
2007-12-21 17:08:00 -07:00
|
|
|
self.closed = 0
|
2008-07-21 22:24:36 -06:00
|
|
|
self.midstream = False
|
2008-07-23 09:41:08 -06:00
|
|
|
self.hash = 0
|
2007-12-21 17:08:00 -07:00
|
|
|
|
|
|
|
self.handle = self.handle_handshake
|
|
|
|
|
|
|
|
|
|
|
|
def handle(self, pkt):
|
|
|
|
"""Stub.
|
|
|
|
|
|
|
|
This function will never be called, it is immediately overridden
|
|
|
|
by __init__. The current value of this function is the state.
|
|
|
|
"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
def handle_handshake(self, pkt):
|
|
|
|
if not self.first:
|
|
|
|
self.first = pkt
|
2008-07-23 09:41:08 -06:00
|
|
|
self.hash = pkt.hash
|
2007-12-21 17:08:00 -07:00
|
|
|
|
2008-01-01 18:55:24 -07:00
|
|
|
if pkt.flags == SYN:
|
2007-12-21 17:08:00 -07:00
|
|
|
self.cli, self.srv = pkt.src, pkt.dst
|
2008-01-01 18:55:24 -07:00
|
|
|
elif pkt.flags == (SYN | ACK):
|
2008-07-21 17:52:35 -06:00
|
|
|
#assert (pkt.src == (self.srv or pkt.src))
|
2007-12-21 17:08:00 -07:00
|
|
|
self.cli, self.srv = pkt.dst, pkt.src
|
2008-07-23 09:41:08 -06:00
|
|
|
self.lastack = [pkt.seq + 1, pkt.ack]
|
|
|
|
self.handle_packet(pkt)
|
2008-01-01 18:55:24 -07:00
|
|
|
elif pkt.flags == ACK:
|
2008-07-21 17:52:35 -06:00
|
|
|
#assert (pkt.src == (self.cli or pkt.src))
|
2007-12-21 17:08:00 -07:00
|
|
|
self.cli, self.srv = pkt.src, pkt.dst
|
2008-07-23 09:41:08 -06:00
|
|
|
self.lastack = [pkt.ack, pkt.seq]
|
2007-12-21 17:08:00 -07:00
|
|
|
self.handle = self.handle_packet
|
2008-07-23 09:41:08 -06:00
|
|
|
self.handle(pkt)
|
2007-12-21 17:08:00 -07:00
|
|
|
else:
|
2008-01-01 14:47:06 -07:00
|
|
|
# In the middle of a session, do the best we can
|
2008-07-21 22:24:36 -06:00
|
|
|
warnings.warn('Starting mid-stream')
|
|
|
|
self.midstream = True
|
2008-01-01 14:47:06 -07:00
|
|
|
self.cli, self.srv = pkt.src, pkt.dst
|
2008-07-23 09:41:08 -06:00
|
|
|
self.lastack = [pkt.ack, pkt.seq]
|
2008-01-01 14:47:06 -07:00
|
|
|
self.handle = self.handle_packet
|
|
|
|
self.handle(pkt)
|
2007-12-21 17:08:00 -07:00
|
|
|
|
|
|
|
def handle_packet(self, pkt):
|
|
|
|
ret = None
|
|
|
|
|
2008-01-01 14:47:06 -07:00
|
|
|
# Which way is this going? 0 == from client
|
2007-12-21 17:08:00 -07:00
|
|
|
idx = int(pkt.src == self.srv)
|
|
|
|
xdi = 1 - idx
|
|
|
|
|
|
|
|
# Does this ACK after the last output sequence number?
|
2008-07-23 09:41:08 -06:00
|
|
|
seq = self.lastack[idx]
|
2008-01-01 14:47:06 -07:00
|
|
|
if pkt.ack > seq:
|
2007-12-21 17:08:00 -07:00
|
|
|
pending = self.pending[xdi]
|
2008-07-23 10:48:27 -06:00
|
|
|
|
|
|
|
# Get a sorted list of sequence numbers
|
|
|
|
keys = pending.keys()
|
|
|
|
keys.sort()
|
|
|
|
|
|
|
|
# Build up return value
|
|
|
|
gs = gapstr.GapString()
|
|
|
|
if keys:
|
|
|
|
f = pending[keys[0]]
|
|
|
|
ret = (xdi, f, gs)
|
|
|
|
else:
|
|
|
|
ret = (xdi, None, gs)
|
|
|
|
|
|
|
|
# Fill in gs with our frames
|
|
|
|
for key in keys:
|
2007-12-21 17:08:00 -07:00
|
|
|
if key >= pkt.ack:
|
2008-07-23 10:48:27 -06:00
|
|
|
break
|
|
|
|
if key < seq:
|
2008-07-25 17:38:30 -06:00
|
|
|
# Hopefully just a retransmit...
|
|
|
|
del pending[key]
|
|
|
|
continue
|
2008-07-23 10:48:27 -06:00
|
|
|
elif key > seq:
|
|
|
|
gs.append(key - seq)
|
|
|
|
seq = key
|
|
|
|
frame = pending[key]
|
|
|
|
gs.append(frame.payload)
|
|
|
|
seq += len(frame.payload)
|
2007-12-21 17:08:00 -07:00
|
|
|
del pending[key]
|
2008-07-23 10:48:27 -06:00
|
|
|
if seq != pkt.ack:
|
|
|
|
gs.append(pkt.ack - seq)
|
2008-07-23 09:41:08 -06:00
|
|
|
self.lastack[idx] = pkt.ack
|
2007-12-21 17:08:00 -07:00
|
|
|
|
|
|
|
# If it has a payload, stick it into pending
|
|
|
|
if pkt.payload:
|
|
|
|
self.pending[idx][pkt.seq] = pkt
|
|
|
|
|
|
|
|
# Is it a FIN or RST?
|
2008-01-01 18:55:24 -07:00
|
|
|
if pkt.flags & (FIN | RST):
|
2008-07-25 17:38:30 -06:00
|
|
|
self.lastack[xdi] = pkt.seq + 1
|
2007-12-21 17:08:00 -07:00
|
|
|
self.closed += 1
|
|
|
|
if self.closed == 2:
|
|
|
|
# Warn about any unhandled packets
|
|
|
|
if self.pending[0] or self.pending[1]:
|
2008-01-01 18:55:24 -07:00
|
|
|
warnings.warn('Dropping unhandled frames after shutdown' % pkt)
|
2007-12-21 17:08:00 -07:00
|
|
|
self.handle = self.handle_drop
|
2008-07-25 17:38:30 -06:00
|
|
|
ret = None
|
2007-12-21 17:08:00 -07:00
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def handle_drop(self, pkt):
|
2007-11-12 21:11:25 -07:00
|
|
|
"""Warn about any unhandled packets"""
|
|
|
|
|
2008-01-01 18:55:24 -07:00
|
|
|
if pkt.payload:
|
|
|
|
warnings.warn('Spurious frame after shutdown: %r %d' % (pkt, pkt.flags))
|
2007-11-12 21:11:25 -07:00
|
|
|
|
|
|
|
|
2008-07-23 10:48:27 -06:00
|
|
|
class Dispatch:
|
2008-07-21 22:24:36 -06:00
|
|
|
def __init__(self, *filenames):
|
|
|
|
self.pcs = {}
|
2007-12-30 23:30:23 -07:00
|
|
|
|
2008-07-21 22:24:36 -06:00
|
|
|
self.sessions = {}
|
|
|
|
self.tops = []
|
2007-12-21 17:08:00 -07:00
|
|
|
|
2008-07-21 22:24:36 -06:00
|
|
|
self.last = None
|
2008-01-10 17:53:11 -07:00
|
|
|
|
2008-07-21 22:24:36 -06:00
|
|
|
for fn in filenames:
|
|
|
|
self.open(fn)
|
2008-01-10 17:53:11 -07:00
|
|
|
|
2008-07-21 22:24:36 -06:00
|
|
|
def open(self, filename, literal=False):
|
|
|
|
if not literal:
|
|
|
|
parts = filename.split(':::')
|
|
|
|
fn = parts[0]
|
|
|
|
fd = file(fn)
|
|
|
|
pc = pcap.open(fd)
|
|
|
|
if len(parts) > 1:
|
|
|
|
pos = int(parts[1])
|
|
|
|
fd.seek(pos)
|
|
|
|
self._read(pc, fn, fd)
|
|
|
|
else:
|
|
|
|
fd = file(filename)
|
|
|
|
pc = pcap.open(fd)
|
|
|
|
self._read(pc, filename, fd)
|
2008-01-10 17:53:11 -07:00
|
|
|
|
2008-07-21 22:24:36 -06:00
|
|
|
def _read(self, pc, filename, fd):
|
|
|
|
pos = fd.tell()
|
|
|
|
pkt = pc.read()
|
|
|
|
if pkt:
|
|
|
|
heapq.heappush(self.tops, (pkt, pc, filename, fd, pos))
|
2008-01-10 17:53:11 -07:00
|
|
|
|
2008-07-21 22:24:36 -06:00
|
|
|
def __iter__(self):
|
|
|
|
while self.tops:
|
|
|
|
pkt, pc, filename, fd, pos = heapq.heappop(self.tops)
|
|
|
|
if not self.last:
|
|
|
|
self.last = (filename, pos)
|
|
|
|
frame = Frame(pkt)
|
|
|
|
if frame.protocol == TCP:
|
|
|
|
# compute TCP session hash
|
|
|
|
tcp_sess = self.sessions.get(frame.hash)
|
|
|
|
if not tcp_sess:
|
|
|
|
tcp_sess = TCP_Resequence()
|
|
|
|
self.sessions[frame.hash] = tcp_sess
|
2008-07-23 10:48:27 -06:00
|
|
|
ret = tcp_sess.handle(frame)
|
|
|
|
if ret:
|
|
|
|
yield frame.hash, ret
|
2008-07-21 22:24:36 -06:00
|
|
|
self.last = None
|
|
|
|
self._read(pc, filename, fd)
|
2007-12-21 17:08:00 -07:00
|
|
|
|
2007-12-11 17:20:54 -07:00
|
|
|
|
2008-01-23 17:04:41 -07:00
|
|
|
##
|
|
|
|
## Binary protocol stuff
|
|
|
|
##
|
2007-12-11 17:20:54 -07:00
|
|
|
|
2008-07-08 17:52:28 -06:00
|
|
|
class NeedMoreData(Exception):
|
|
|
|
pass
|
|
|
|
|
2008-01-23 17:04:41 -07:00
|
|
|
class Packet(UserDict.DictMixin):
|
|
|
|
"""Base class for a packet from a binary protocol.
|
2007-12-11 17:20:54 -07:00
|
|
|
|
2008-01-23 17:04:41 -07:00
|
|
|
This is a base class for making protocol reverse-engineering easier.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
opcodes = {}
|
|
|
|
|
2008-07-21 22:24:36 -06:00
|
|
|
def __init__(self, session, firstframe=None):
|
|
|
|
self.session = session
|
2008-06-18 21:36:43 -06:00
|
|
|
self.firstframe = firstframe
|
2008-01-23 17:04:41 -07:00
|
|
|
self.opcode = None
|
|
|
|
self.opcode_desc = None
|
|
|
|
self.parts = []
|
|
|
|
self.params = {}
|
|
|
|
self.payload = None
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
r = '<%s packet opcode=%s' % (self.__class__.__name__, self.opcode)
|
|
|
|
if self.opcode_desc:
|
|
|
|
r += '(%s)' % self.opcode_desc
|
|
|
|
keys = self.params.keys()
|
|
|
|
keys.sort()
|
|
|
|
for k in keys:
|
|
|
|
r += ' %s=%s' % (k, self.params[k])
|
|
|
|
r += '>'
|
|
|
|
return r
|
2007-12-11 17:20:54 -07:00
|
|
|
|
|
|
|
|
2008-01-23 17:04:41 -07:00
|
|
|
## Dict methods
|
|
|
|
def __setitem__(self, k, v):
|
|
|
|
self.params[k] = v
|
2007-12-21 17:08:00 -07:00
|
|
|
|
2008-01-23 17:04:41 -07:00
|
|
|
def __getitem__(self, k):
|
|
|
|
return self.params[k]
|
2007-12-21 17:08:00 -07:00
|
|
|
|
2008-01-23 17:04:41 -07:00
|
|
|
def __contains__(self, k):
|
|
|
|
return k in self.params
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self.params.__iter__()
|
|
|
|
|
|
|
|
def has_key(self, k):
|
|
|
|
return self.params.has_key(k)
|
|
|
|
|
|
|
|
def keys(self):
|
|
|
|
return self.params.keys()
|
|
|
|
|
|
|
|
##
|
|
|
|
|
|
|
|
def assert_in(self, a, *b):
|
|
|
|
if len(b) == 1:
|
|
|
|
assert a == b[0], ('%r != %r' % (a, b[0]))
|
|
|
|
else:
|
|
|
|
assert a in b, ('%r not in %r' % (a, b))
|
|
|
|
|
|
|
|
def show(self):
|
|
|
|
print '%s %3s: %s' % (self.__class__.__name__,
|
|
|
|
self.opcode,
|
|
|
|
self.opcode_desc)
|
2008-06-18 21:36:43 -06:00
|
|
|
if self.firstframe:
|
|
|
|
print ' %s:%d -> %s:%d (%s)' % (self.firstframe.src_addr,
|
|
|
|
self.firstframe.sport,
|
|
|
|
self.firstframe.dst_addr,
|
|
|
|
self.firstframe.dport,
|
|
|
|
time.ctime(self.firstframe.time))
|
2008-01-23 17:04:41 -07:00
|
|
|
|
|
|
|
if self.parts:
|
|
|
|
dl = len(self.parts[-1])
|
|
|
|
p = []
|
|
|
|
for x in self.parts[:-1]:
|
|
|
|
if x == dl:
|
|
|
|
p.append('%3d!' % x)
|
|
|
|
else:
|
|
|
|
p.append('%3d' % x)
|
|
|
|
print ' parts: (%s) +%d bytes' % (','.join(p), dl)
|
|
|
|
|
|
|
|
keys = self.params.keys()
|
|
|
|
keys.sort()
|
|
|
|
for k in keys:
|
|
|
|
print ' %12s: %s' % (k, self.params[k])
|
|
|
|
|
|
|
|
if self.payload:
|
|
|
|
try:
|
|
|
|
self.payload.hexdump()
|
|
|
|
except AttributeError:
|
|
|
|
print ' payload: %r' % self.payload
|
|
|
|
|
|
|
|
def parse(self, data):
|
|
|
|
"""Parse a chunk of data (possibly a GapString).
|
|
|
|
|
|
|
|
Anything returned is not part of this packet and will be passed
|
|
|
|
in to a subsequent packet.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.parts = [data]
|
2008-07-21 17:52:35 -06:00
|
|
|
self.payload = data
|
2008-01-23 17:04:41 -07:00
|
|
|
return None
|
|
|
|
|
|
|
|
def handle(self, data):
|
|
|
|
"""Handle data from a Session class."""
|
|
|
|
|
|
|
|
data = self.parse(data)
|
|
|
|
if self.opcode <> None:
|
2008-07-08 17:52:28 -06:00
|
|
|
try:
|
|
|
|
f = getattr(self, 'opcode_%s' % self.opcode)
|
|
|
|
except AttributeError:
|
|
|
|
f = self.opcode_unknown
|
2008-01-23 17:04:41 -07:00
|
|
|
if not self.opcode_desc and f.__doc__:
|
|
|
|
self.opcode_desc = f.__doc__.split('\n')[0]
|
|
|
|
f()
|
|
|
|
return data
|
|
|
|
|
2008-07-08 17:52:28 -06:00
|
|
|
def opcode_unknown(self):
|
|
|
|
"""Unknown opcode"""
|
|
|
|
|
2008-07-25 17:38:30 -06:00
|
|
|
raise AttributeError('Opcode %s unknown' % self.opcode)
|
2008-07-08 17:52:28 -06:00
|
|
|
|
2008-01-23 17:04:41 -07:00
|
|
|
|
|
|
|
class Session:
|
|
|
|
"""Base class for a binary protocol session."""
|
|
|
|
|
|
|
|
# Override this, duh
|
|
|
|
Packet = Packet
|
|
|
|
|
2008-07-23 09:41:08 -06:00
|
|
|
def __init__(self, frame):
|
2008-07-23 10:48:27 -06:00
|
|
|
self.firstframe = frame
|
|
|
|
self.lastframe = [None, None]
|
2008-07-23 09:41:08 -06:00
|
|
|
self.basename = 'transfers/%s' % (frame.src_addr,)
|
2008-07-25 17:38:30 -06:00
|
|
|
self.basename2 = 'transfers/%s' % (frame.dst_addr,)
|
2008-07-08 17:52:28 -06:00
|
|
|
self.pending = {}
|
2008-07-21 22:24:36 -06:00
|
|
|
self.count = 0
|
2008-07-25 17:38:30 -06:00
|
|
|
for d in (self.basename, self.basename2):
|
|
|
|
try:
|
|
|
|
os.makedirs(d)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
2008-07-21 22:24:36 -06:00
|
|
|
self.setup()
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
"""Set things up."""
|
|
|
|
|
|
|
|
pass
|
2008-07-08 17:52:28 -06:00
|
|
|
|
2008-07-23 10:48:27 -06:00
|
|
|
def handle(self, is_srv, frame, gs, lastpos):
|
2008-01-23 17:04:41 -07:00
|
|
|
"""Handle a data burst.
|
|
|
|
|
2008-07-23 10:48:27 -06:00
|
|
|
@param is_srv Is this from the server?
|
|
|
|
@param frame A frame associated with this packet, or None if it's all drops
|
|
|
|
@param gs A gapstring of the data
|
|
|
|
@param lastpos Last position in the source file, for debugging
|
2008-01-23 17:04:41 -07:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2008-07-23 10:48:27 -06:00
|
|
|
if frame:
|
|
|
|
self.lastframe[is_srv] = frame
|
|
|
|
frame = self.lastframe[is_srv]
|
2008-07-21 22:24:36 -06:00
|
|
|
self.lastpos = lastpos
|
2008-07-08 17:52:28 -06:00
|
|
|
try:
|
2008-07-23 10:48:27 -06:00
|
|
|
saddr = frame.saddr
|
2008-07-21 22:24:36 -06:00
|
|
|
try:
|
2008-07-23 10:48:27 -06:00
|
|
|
(f, data) = self.pending.pop(saddr)
|
2008-07-21 22:24:36 -06:00
|
|
|
except KeyError:
|
2008-07-23 10:48:27 -06:00
|
|
|
f = frame
|
2008-07-21 22:24:36 -06:00
|
|
|
data = gapstr.GapString()
|
2008-07-23 10:48:27 -06:00
|
|
|
data.extend(gs)
|
2008-07-21 22:24:36 -06:00
|
|
|
try:
|
|
|
|
while data:
|
2008-07-23 10:48:27 -06:00
|
|
|
p = self.Packet(self, f)
|
2008-07-21 22:24:36 -06:00
|
|
|
data = p.handle(data)
|
|
|
|
self.process(p)
|
|
|
|
except NeedMoreData:
|
2008-07-23 10:48:27 -06:00
|
|
|
self.pending[saddr] = (f, data)
|
2008-07-21 22:24:36 -06:00
|
|
|
self.count += 1
|
|
|
|
except:
|
|
|
|
print 'Lastpos: %s:::%d' % lastpos
|
|
|
|
raise
|
2008-01-23 17:04:41 -07:00
|
|
|
|
|
|
|
def process(self, packet):
|
|
|
|
"""Process a packet.
|
|
|
|
|
|
|
|
When you first start out, this probably does exactly what you
|
|
|
|
want: print out packets as they come in. As you progress you'll
|
|
|
|
probably want to override it with something more sophisticated.
|
|
|
|
That will of course vary wildly between protocols.
|
|
|
|
|
|
|
|
"""
|
2007-12-21 17:08:00 -07:00
|
|
|
|
2008-07-21 22:24:36 -06:00
|
|
|
print 'Lastpos: %s:::%d' % self.lastpos
|
2008-01-23 17:04:41 -07:00
|
|
|
packet.show()
|
2007-12-21 17:08:00 -07:00
|
|
|
|
2008-07-08 17:52:28 -06:00
|
|
|
def done(self):
|
|
|
|
"""Called when all packets have been handled"""
|
|
|
|
|
|
|
|
return
|
|
|
|
|
2008-07-25 17:38:30 -06:00
|
|
|
def open_out(self, fn):
|
|
|
|
frame = self.firstframe
|
|
|
|
fn = '%d-%s~%d-%s~%d---%s' % (frame.time,
|
|
|
|
frame.src_addr, frame.sport,
|
|
|
|
frame.dst_addr, frame.dport,
|
|
|
|
urllib.quote(fn, ''))
|
|
|
|
fullfn = os.path.join(self.basename, fn)
|
|
|
|
fullfn2 = os.path.join(self.basename2, fn)
|
|
|
|
print 'writing %s' % (fn,)
|
|
|
|
fd = file(fullfn, 'w')
|
2008-07-23 09:41:08 -06:00
|
|
|
try:
|
2008-07-25 17:38:30 -06:00
|
|
|
os.unlink(fullfn2)
|
2008-07-23 09:41:08 -06:00
|
|
|
except OSError:
|
|
|
|
pass
|
2008-07-25 17:38:30 -06:00
|
|
|
os.link(fullfn, fullfn2)
|
|
|
|
return fd
|
2008-07-23 09:41:08 -06:00
|
|
|
|
2008-07-08 17:52:28 -06:00
|
|
|
def handle_packets(self, collection):
|
|
|
|
"""Handle a collection of packets"""
|
|
|
|
|
|
|
|
for chunk in resequence(collection):
|
|
|
|
self.handle(chunk)
|
|
|
|
self.done()
|
2008-07-21 17:52:35 -06:00
|
|
|
|
|
|
|
|
|
|
|
class HtmlSession(Session):
|
|
|
|
def __init__(self, frame):
|
2008-07-23 09:41:08 -06:00
|
|
|
Session.__init__(self, frame)
|
2008-07-25 17:38:30 -06:00
|
|
|
self.sessfd = self.open_out('session.html')
|
2008-07-23 10:48:27 -06:00
|
|
|
self.sessfd.write('''<?xml version="1.0" encoding="UTF-8"?>
|
2008-07-21 17:52:35 -06:00
|
|
|
<!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">
|
2008-07-23 10:48:27 -06:00
|
|
|
.server { background-color: white; color: black; }
|
|
|
|
.client { background-color: #884; color: white; }
|
2008-07-21 17:52:35 -06:00
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
''' % self.__class__.__name__)
|
2008-07-23 10:48:27 -06:00
|
|
|
self.sessfd.write('<h1>%s</h1>\n' % self.__class__.__name__)
|
|
|
|
self.sessfd.write('<pre>')
|
2008-07-21 17:52:35 -06:00
|
|
|
self.srv = None
|
|
|
|
|
|
|
|
def __del__(self):
|
2008-07-23 10:48:27 -06:00
|
|
|
self.sessfd.write('</pre></body></html>')
|
2008-07-21 17:52:35 -06:00
|
|
|
|
2008-07-23 09:41:08 -06:00
|
|
|
def log(self, frame, payload, escape=True):
|
|
|
|
if escape:
|
2008-07-25 17:38:30 -06:00
|
|
|
p = cgi.escape(str(payload))
|
2008-07-23 09:41:08 -06:00
|
|
|
else:
|
|
|
|
p = payload
|
|
|
|
if not self.srv:
|
|
|
|
self.srv = frame.saddr
|
2008-07-21 17:52:35 -06:00
|
|
|
if frame.saddr == self.srv:
|
|
|
|
cls = 'server'
|
|
|
|
else:
|
|
|
|
cls = 'client'
|
2008-07-23 10:48:27 -06:00
|
|
|
self.sessfd.write('<span class="%s" title="%s(%s)">' % (cls, time.ctime(frame.time), frame.time))
|
|
|
|
self.sessfd.write(p.replace('\r\n', '\n'))
|
|
|
|
self.sessfd.write('</span>')
|