mirror of https://github.com/dirtbags/netarch.git
Lots of things for Birthday Bear
This commit is contained in:
parent
60291721c8
commit
5f0f3693e8
45
__init__.py
45
__init__.py
|
@ -1,6 +1,8 @@
|
||||||
#! /usr/bin/python
|
#! /usr/bin/python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
## 2008 Massive Blowout
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import struct
|
import struct
|
||||||
|
|
||||||
|
@ -111,29 +113,9 @@ def assert_in(a, *b):
|
||||||
## Binary stuff
|
## Binary stuff
|
||||||
##
|
##
|
||||||
|
|
||||||
def bin(i):
|
|
||||||
"""Return the binary representation of i"""
|
|
||||||
|
|
||||||
r = []
|
|
||||||
while i > 0:
|
|
||||||
r.append(i % 2)
|
|
||||||
i = i >> 1
|
|
||||||
r.reverse()
|
|
||||||
s = ''.join(str(x) for x in r)
|
|
||||||
return s
|
|
||||||
|
|
||||||
class BitVector:
|
class BitVector:
|
||||||
def __init__(self, i=0, length=None):
|
def __init__(self, i=0, length=None):
|
||||||
if type(i) == type(''):
|
if type(i) == type(1):
|
||||||
self._val = 0
|
|
||||||
for c in i:
|
|
||||||
self._val <<= 8
|
|
||||||
self._val += ord(c)
|
|
||||||
if length is not None:
|
|
||||||
self._len = length
|
|
||||||
else:
|
|
||||||
self._len = len(i) * 8
|
|
||||||
else:
|
|
||||||
self._val = i
|
self._val = i
|
||||||
if length is not None:
|
if length is not None:
|
||||||
self._len = length
|
self._len = length
|
||||||
|
@ -142,6 +124,15 @@ class BitVector:
|
||||||
while i > 0:
|
while i > 0:
|
||||||
i >>= 1
|
i >>= 1
|
||||||
self._len += 1
|
self._len += 1
|
||||||
|
else:
|
||||||
|
self._val = 0
|
||||||
|
for c in i:
|
||||||
|
self._val <<= 8
|
||||||
|
self._val += ord(c)
|
||||||
|
if length is not None:
|
||||||
|
self._len = length
|
||||||
|
else:
|
||||||
|
self._len = len(i) * 8
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return self._len
|
return self._len
|
||||||
|
@ -161,6 +152,8 @@ class BitVector:
|
||||||
return BitVector(i & mask, length=l)
|
return BitVector(i & mask, length=l)
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
|
"""Iterate from LSB to MSB"""
|
||||||
|
|
||||||
v = self._val
|
v = self._val
|
||||||
for i in xrange(self._len):
|
for i in xrange(self._len):
|
||||||
yield int(v & 1)
|
yield int(v & 1)
|
||||||
|
@ -195,6 +188,16 @@ class BitVector:
|
||||||
else:
|
else:
|
||||||
raise ValueError("Can't extend with this type yet")
|
raise ValueError("Can't extend with this type yet")
|
||||||
|
|
||||||
|
def bitstr(self):
|
||||||
|
bits = [str(x) for x in self]
|
||||||
|
bits.reverse()
|
||||||
|
return ''.join(bits)
|
||||||
|
|
||||||
|
|
||||||
|
def bin(i, bits=None):
|
||||||
|
"""Return the binary representation of i"""
|
||||||
|
|
||||||
|
return BitVector(i, bits).bitstr()
|
||||||
|
|
||||||
|
|
||||||
##
|
##
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#! /usr/bin/python
|
#! /usr/bin/python
|
||||||
|
|
||||||
## Codebreaking tools
|
## Codebreaking tools
|
||||||
## 2007 Neale Pickett
|
## 2007 Massive Blowout
|
||||||
## I should get an LAUR for this so we can share it.
|
## I should get an LAUR for this so we can share it.
|
||||||
|
|
||||||
from sets import Set
|
from sets import Set
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#! /usr/bin/python
|
#! /usr/bin/python
|
||||||
|
|
||||||
|
## 2008 Massive Blowout
|
||||||
|
|
||||||
"""Functions to treat a list as a string with gaps.
|
"""Functions to treat a list as a string with gaps.
|
||||||
|
|
||||||
Lists should have only string and integer items.
|
Lists should have only string and integer items.
|
||||||
|
@ -122,9 +124,9 @@ class GapString:
|
||||||
def __xor__(self, mask):
|
def __xor__(self, mask):
|
||||||
if isinstance(mask, int):
|
if isinstance(mask, int):
|
||||||
mask = [mask]
|
mask = [mask]
|
||||||
masklen = len(mask)
|
if isinstance(mask, str) or isinstance(mask, GapString):
|
||||||
if isinstance(mask, str):
|
|
||||||
mask = [ord(c) for c in mask]
|
mask = [ord(c) for c in mask]
|
||||||
|
masklen = len(mask)
|
||||||
|
|
||||||
new = self.__class__(drop=self.drop)
|
new = self.__class__(drop=self.drop)
|
||||||
for i in self.contents:
|
for i in self.contents:
|
||||||
|
|
41
ip.py
41
ip.py
|
@ -1,5 +1,8 @@
|
||||||
#! /usr/bin/python
|
#! /usr/bin/python
|
||||||
|
|
||||||
|
## IP resequencing + protocol reversing skeleton
|
||||||
|
## 2008 Massive Blowout
|
||||||
|
|
||||||
import StringIO
|
import StringIO
|
||||||
import struct
|
import struct
|
||||||
import socket
|
import socket
|
||||||
|
@ -351,6 +354,9 @@ def demux(*pcs):
|
||||||
## Binary protocol stuff
|
## Binary protocol stuff
|
||||||
##
|
##
|
||||||
|
|
||||||
|
class NeedMoreData(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
class Packet(UserDict.DictMixin):
|
class Packet(UserDict.DictMixin):
|
||||||
"""Base class for a packet from a binary protocol.
|
"""Base class for a packet from a binary protocol.
|
||||||
|
|
||||||
|
@ -455,12 +461,20 @@ class Packet(UserDict.DictMixin):
|
||||||
|
|
||||||
data = self.parse(data)
|
data = self.parse(data)
|
||||||
if self.opcode <> None:
|
if self.opcode <> None:
|
||||||
|
try:
|
||||||
f = getattr(self, 'opcode_%s' % self.opcode)
|
f = getattr(self, 'opcode_%s' % self.opcode)
|
||||||
|
except AttributeError:
|
||||||
|
f = self.opcode_unknown
|
||||||
if not self.opcode_desc and f.__doc__:
|
if not self.opcode_desc and f.__doc__:
|
||||||
self.opcode_desc = f.__doc__.split('\n')[0]
|
self.opcode_desc = f.__doc__.split('\n')[0]
|
||||||
f()
|
f()
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
def opcode_unknown(self):
|
||||||
|
"""Unknown opcode"""
|
||||||
|
|
||||||
|
raise AttributeError('Opcode %d unknown' % self.opcode)
|
||||||
|
|
||||||
|
|
||||||
class Session:
|
class Session:
|
||||||
"""Base class for a binary protocol session."""
|
"""Base class for a binary protocol session."""
|
||||||
|
@ -468,6 +482,9 @@ class Session:
|
||||||
# Override this, duh
|
# Override this, duh
|
||||||
Packet = Packet
|
Packet = Packet
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.pending = {}
|
||||||
|
|
||||||
def handle(self, chunk):
|
def handle(self, chunk):
|
||||||
"""Handle a data burst.
|
"""Handle a data burst.
|
||||||
|
|
||||||
|
@ -475,11 +492,20 @@ class Session:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
data = chunk.gapstr()
|
saddr = chunk.first.saddr
|
||||||
|
try:
|
||||||
|
(first, data) = self.pending[saddr]
|
||||||
|
except KeyError:
|
||||||
|
first = chunk.first
|
||||||
|
data = gapstr.GapString()
|
||||||
|
data.extend(chunk.gapstr())
|
||||||
|
try:
|
||||||
while data:
|
while data:
|
||||||
p = self.Packet(chunk.first)
|
p = self.Packet(first)
|
||||||
data = p.handle(data)
|
data = p.handle(data)
|
||||||
self.process(p)
|
self.process(p)
|
||||||
|
except NeedMoreData:
|
||||||
|
self.pending[saddr] = (first, data)
|
||||||
|
|
||||||
def process(self, packet):
|
def process(self, packet):
|
||||||
"""Process a packet.
|
"""Process a packet.
|
||||||
|
@ -493,3 +519,14 @@ class Session:
|
||||||
|
|
||||||
packet.show()
|
packet.show()
|
||||||
|
|
||||||
|
def done(self):
|
||||||
|
"""Called when all packets have been handled"""
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
def handle_packets(self, collection):
|
||||||
|
"""Handle a collection of packets"""
|
||||||
|
|
||||||
|
for chunk in resequence(collection):
|
||||||
|
self.handle(chunk)
|
||||||
|
self.done()
|
||||||
|
|
Loading…
Reference in New Issue