forked from ayeowch/bitnodes
-
Notifications
You must be signed in to change notification settings - Fork 4
/
pcap.py
executable file
·305 lines (268 loc) · 10.5 KB
/
pcap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# pcap.py - Saves messages from pcap files in Redis.
#
# Copyright (c) Addy Yeow Chin Heng <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
Saves messages from pcap files in Redis.
"""
import dpkt
import glob
import hashlib
import logging
import os
import redis
import socket
import sys
import time
from collections import defaultdict
from ConfigParser import ConfigParser
from Queue import PriorityQueue
from protocol import (ProtocolError, HeaderTooShortError, PayloadTooShortError,
Serializer)
# Redis connection setup
REDIS_SOCKET = os.environ.get('REDIS_SOCKET', "/tmp/redis.sock")
REDIS_PASSWORD = os.environ.get('REDIS_PASSWORD', None)
REDIS_CONN = redis.StrictRedis(unix_socket_path=REDIS_SOCKET,
password=REDIS_PASSWORD)
SETTINGS = {}
class Stream(object):
"""
Implements a stream object with generator function to iterate over the
queued segments while keeping track of captured timestamp.
"""
def __init__(self, segments=None):
self.segments = segments
self.timestamp = 0 # in ms
def data(self):
"""
Generator to iterate over the segments in this stream. Duplicated
segments are ignored.
"""
seqs = set()
while not self.segments.empty():
(self.timestamp, tcp_pkt) = self.segments.get()[1]
if tcp_pkt.seq in seqs:
continue
yield tcp_pkt.data
seqs.add(tcp_pkt.seq)
class Cache(object):
"""
Implements caching mechanic to cache messages from pcap file in Redis.
"""
def __init__(self, filepath):
self.filepath = filepath
self.redis_pipe = REDIS_CONN.pipeline()
self.serializer = Serializer()
self.streams = defaultdict(PriorityQueue)
self.stream = Stream()
self.count = 0
self.keys = set() # ping:ADDRESS-PORT:NONCE
def cache_messages(self):
"""
Reconstructs messages from TCP streams and caches them in Redis.
"""
try:
self.extract_streams()
except dpkt.dpkt.NeedData:
logging.warning("Need data: %s", self.filepath)
for stream_id, self.stream.segments in self.streams.iteritems():
data = self.stream.data()
_data = data.next()
while True:
try:
(msg, _data) = self.serializer.deserialize_msg(_data)
except (HeaderTooShortError, PayloadTooShortError) as err:
logging.debug("%s: %s", stream_id, err)
try:
_data += data.next()
except StopIteration:
break
except ProtocolError as err:
logging.debug("%s: %s", stream_id, err)
try:
_data = data.next()
except StopIteration:
break
else:
src = (stream_id[0], stream_id[1])
dst = (stream_id[2], stream_id[3])
node = src
if src == SETTINGS['tor_proxy']:
node = dst
self.cache_message(node, self.stream.timestamp, msg)
self.redis_pipe.execute()
self.cache_rtt()
def extract_streams(self):
"""
Extracts TCP streams with data from the pcap file. TCP segments in
each stream are queued according to their sequence number.
"""
with open(self.filepath) as pcap_file:
pcap_reader = dpkt.pcap.Reader(pcap_file)
for timestamp, buf in pcap_reader:
try:
frame = dpkt.ethernet.Ethernet(buf)
except dpkt.dpkt.UnpackError:
continue
ip_pkt = frame.data
if (not isinstance(ip_pkt, dpkt.ip.IP) and
not isinstance(ip_pkt, dpkt.ip6.IP6)):
continue
if not isinstance(ip_pkt.data, dpkt.tcp.TCP):
continue
ip_ver = socket.AF_INET
if ip_pkt.v == 6:
ip_ver = socket.AF_INET6
tcp_pkt = ip_pkt.data
stream_id = (
socket.inet_ntop(ip_ver, ip_pkt.src),
tcp_pkt.sport,
socket.inet_ntop(ip_ver, ip_pkt.dst),
tcp_pkt.dport
)
if len(tcp_pkt.data) > 0:
timestamp = int(timestamp * 1000) # in ms
self.streams[stream_id].put(
(tcp_pkt.seq, (timestamp, tcp_pkt)))
logging.info("Streams: %d", len(self.streams))
def cache_message(self, node, timestamp, msg):
"""
Caches inv/pong message from the specified node.
"""
if msg['command'] not in ["inv", "pong"]:
return
if node[0] == "127.0.0.1":
# Restore .onion node
onion_node = REDIS_CONN.get("onion:{}".format(node[1]))
if onion_node:
node = eval(onion_node)
if msg['command'] == "inv":
for inv in msg['inventory']:
key = "inv:{}:{}".format(inv['type'], inv['hash'])
if inv['type'] == 2:
# Redis key for reference (first seen) block inv
rkey = "r{}".format(key)
rkey_ms = REDIS_CONN.get(rkey)
if rkey_ms is None:
REDIS_CONN.set(rkey, timestamp)
self.redis_pipe.set("lastblockhash", inv['hash'])
elif (timestamp - int(rkey_ms)) / 1000 > SETTINGS['ttl']:
# Ignore block inv first seen more than 3 hours ago
logging.debug("Skip: %s", key)
continue
self.redis_pipe.zadd(key, timestamp, self.node_hash(node))
self.redis_pipe.expire(key, SETTINGS['ttl'])
self.count += msg['count']
elif msg['command'] == "pong":
key = "ping:{}-{}:{}".format(node[0], node[1], msg['nonce'])
self.redis_pipe.rpushx(key, timestamp)
self.keys.add(key)
self.count += 1
def node_hash(self, node):
"""
Encodes a tuple of address and port in shorten hash for storage in
Redis.
"""
return hashlib.sha256('%s-%d' % node).hexdigest()[:8]
def cache_rtt(self):
"""
Calculates round-trip time (RTT) values and caches them in Redis.
"""
for key in self.keys:
timestamps = REDIS_CONN.lrange(key, 0, 1)
if len(timestamps) > 1:
rtt_key = "rtt:{}".format(':'.join(key.split(":")[1:-1]))
rtt = int(timestamps[1]) - int(timestamps[0]) # pong - ping
logging.debug("%s: %d", rtt_key, rtt)
self.redis_pipe.lpush(rtt_key, rtt)
self.redis_pipe.ltrim(rtt_key, 0, SETTINGS['rtt_count'] - 1)
self.redis_pipe.expire(rtt_key, SETTINGS['ttl'])
self.redis_pipe.execute()
def cron():
"""
Periodically fetches oldest pcap file to extract messages from.
"""
while True:
time.sleep(0.1)
try:
oldest = min(glob.iglob("{}/*.pcap".format(SETTINGS['pcap_dir'])))
except ValueError as err:
logging.warning(err)
continue
latest = max(glob.iglob("{}/*.pcap".format(SETTINGS['pcap_dir'])))
if oldest == latest:
continue
tmp = oldest
dump = tmp.replace(".pcap", ".pcap_")
try:
os.rename(tmp, dump) # Mark file as being read
except OSError as err:
logging.warning(err)
continue
logging.info("Loading: %s", dump)
start = time.time()
cache = Cache(filepath=dump)
cache.cache_messages()
end = time.time()
elapsed = end - start
logging.info("Dump: %s (%d messages)", dump, cache.count)
logging.info("Elapsed: %d", elapsed)
os.remove(dump)
def init_settings(argv):
"""
Populates SETTINGS with key-value pairs from configuration file.
"""
conf = ConfigParser()
conf.read(argv[1])
SETTINGS['logfile'] = conf.get('pcap', 'logfile')
SETTINGS['debug'] = conf.getboolean('pcap', 'debug')
SETTINGS['ttl'] = conf.getint('pcap', 'ttl')
SETTINGS['rtt_count'] = conf.getint('pcap', 'rtt_count')
tor_proxy = conf.get('pcap', 'tor_proxy').split(":")
SETTINGS['tor_proxy'] = (tor_proxy[0], int(tor_proxy[1]))
SETTINGS['pcap_dir'] = conf.get('pcap', 'pcap_dir')
if not os.path.exists(SETTINGS['pcap_dir']):
os.makedirs(SETTINGS['pcap_dir'])
def main(argv):
if len(argv) < 2 or not os.path.exists(argv[1]):
print("Usage: pcap.py [config]")
return 1
# Initialize global settings
init_settings(argv)
# Initialize logger
loglevel = logging.INFO
if SETTINGS['debug']:
loglevel = logging.DEBUG
logformat = ("%(asctime)s,%(msecs)05.1f %(levelname)s (%(funcName)s) "
"%(message)s")
logging.basicConfig(level=loglevel,
format=logformat,
filename=SETTINGS['logfile'],
filemode='a')
print("Writing output to {}, press CTRL+C to terminate..".format(
SETTINGS['logfile']))
cron()
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))