Skip to content

Commit 87e1feb

Browse files
committed
modifier: pcap headers automatically generated to account to packet resizing (module option only). adds a new dependency, python-scapy. i don't know what this is in Fedora
1 parent 68748a4 commit 87e1feb

4 files changed

Lines changed: 49 additions & 4 deletions

File tree

‎INSTALL‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ YMMV, depending on whether the minimum requirements are met.
4141
- Source: http://sourceforge.net/projects/pyusb/
4242
- Ubuntu 11.04: Not currently packaged
4343
- Fedora 14: Not currently packaged
44+
* python-scapy
45+
- Needed only by usbmodify.py
46+
- Ubuntu 10.04 and later: python-scapy
47+
- Fedora 14:
4448

4549
In addition, for usbreplay, ensure that pyusb-1.0.0 is inserted into the
4650
python tree. For example, on some linux systems, a good location would be:

‎demos/tablet/modify_module.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from array import array
2+
from usbrevue import Packet
3+
4+
def modify(packet_gen, commit_func):
5+
packet_buf = []
6+
for packet in packet_gen('-'):
7+
for byte in packet.data:
8+
if byte >= 0x80:
9+
packet_template = Packet(packet.hdr, packet.datapack[:64] + array('c', ''.join(map(chr, packet_buf))))
10+
11+
packet_template.length = len(packet_template.data)
12+
packet_template.len_cap = len(packet_template.data)
13+
14+
commit_func(packet_template)
15+
packet_buf = []
16+
packet_buf.append(byte)
17+
else:
18+
packet_buf.append(byte)

‎demos/tablet/raw_dump.pcap‎

10.8 KB
Binary file not shown.

‎usbmodify.py‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
import gflags
3232
import re
3333
import struct
34+
import tempfile
35+
import os
36+
from scapy.all import RawPcapWriter, RawPcapReader
3437
from usbrevue import Packet
3538

3639

@@ -56,7 +59,6 @@ def __init__(self, module_file, routine_file, cmdline_exps):
5659
self.routine_file = routine_file
5760
self.cmdline_exps = cmdline_exps
5861

59-
6062
def run(self):
6163
"""If a user-supplied module file is present, simply run that
6264
module. Otherwise, Pass each packet from the packet stream
@@ -102,12 +104,13 @@ def packet_generator(self, input_stream='-'):
102104
self.pcap = pcapy.open_offline(input_stream)
103105

104106
# create the Dumper object now that we have a Reader
105-
self.out = self.pcap.dump_open('-')
107+
if not sys.stdout.isatty():
108+
self.out = self.pcap.dump_open('-')
106109

107110
while True:
108111
(hdr, pack) = self.pcap.next()
109112
if hdr is None:
110-
return # EOF
113+
return # EOF
111114
# keep track of the most recent yielding packet, for diffing
112115
self.orig_packet = Packet(hdr, pack)
113116
yield Packet(hdr, pack)
@@ -135,8 +138,28 @@ def commit_packet(self, packet):
135138
sys.stderr.write('Attempted to dump packets without first reading them -- make sure to call packet_generator()')
136139
sys.exit(1)
137140
else:
141+
# make a temp file, use scapy's RawPcapWriter to write the
142+
# current packet to that file, then read that same packet
143+
# in using a new instance of pcapy.open_offline, then
144+
# write out to stdout the packet that we just read back
145+
# in, then finally delete the temp file
146+
#
147+
# we have to do it this way in case the size of the packet
148+
# changed, in which case the packet's pcap header needs to
149+
# be regenerated. RawPcapWriter does this for us
150+
#
151+
# on reflection, it would be pretty straightforward to
152+
# copy RawPcapWriter's code for generating the pcap
153+
# header(s) and do it ourselves, without the messy temp
154+
# file stuff. this code should be re-worked to do that.
155+
temp = tempfile.mkstemp()
156+
rpw = RawPcapWriter(temp[1], linktype = 220, sync=True)
157+
rpw.write(packet.repack())
158+
mypcap = pcapy.open_offline(temp[1])
159+
(myhdr, mypack) = mypcap.next()
138160
if not sys.stdout.isatty():
139-
self.out.dump(packet.hdr, packet.datapack)
161+
self.out.dump(myhdr, mypack)
162+
os.remove(temp[1])
140163

141164

142165
def apply_routine_file(self, packet):

0 commit comments

Comments
 (0)