This repository has been archived by the owner on Nov 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcpchurn.py
executable file
·271 lines (205 loc) · 6.88 KB
/
tcpchurn.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import argparse
import ipaddress
import monotonic
import os
import socket
import struct
import sys
import time
from bcc import BPF
from collections import OrderedDict
from ctypes import c_uint8, c_uint16, c_uint32, c_uint64, cast, Structure, POINTER
from threading import Thread, Lock
ACCEPT = 0
CONNECT = 1
CLOSE = 2
SOURCE = "tcpchurn.c"
CFLAGS = ['-w']
TRACE_WINDOWS = [1., 5., 10., 30., 60., 120., 240., 500., 1200.]
TERM_WIDTH = 80
class RollingWindow(object):
def __init__(self, windows=None):
self.windows = windows or TRACE_WINDOWS
self.windows.sort(reverse=True)
self._window = []
self._counters = OrderedDict([(w, 0) for w in self.windows])
self._indexes = OrderedDict([(w, 0) for w in self.windows])
self._total = 0
self._lock = Lock()
def insert(self, data):
with self._lock:
idx = len(self._window) - 1
while idx >= 0 and data < self._window[idx]:
idx -= 1
self._window.insert(idx + 1, data)
self._total += 1
def update_window(self):
with self._lock:
self._update_window()
def _update_window(self):
now = monotonic.monotonic()
last_idx = len(self._window) - 1
d_idx = None
for wsz in self.windows:
idx = self._indexes[wsz]
min_ts = now - wsz
while idx <= last_idx:
if self._window[idx][0] >= min_ts:
break
idx += 1
if d_idx is None:
d_idx = idx
self._indexes[wsz] = idx - d_idx
self._counters[wsz] = last_idx - idx + 1
if d_idx:
self._window = self._window[d_idx:]
@property
def stats(self):
return self._counters.values() + [self._total]
class EventHandler(object):
def __init__(self, windows=None, ignore_private_dst=False):
self._ignore_private_dst = ignore_private_dst
self._open = RollingWindow(windows)
self._closed = RollingWindow(windows)
def on_ipv4(self, cpu, data, size):
struct_p = cast(data, POINTER(IPv4Event))
self.insert(struct_p.contents.data)
def on_ipv6(self, cpu, data, size):
struct_p = cast(data, POINTER(IPv6Event))
self.insert(struct_p.contents.data)
def insert(self, data):
daddr = data[2]
state = data[-2]
if self._ignore_private_dst:
try:
if ipaddress.ip_address(unicode(daddr)).is_private:
return
except Exception as e:
sys.stderr.write("Invalid address {}: {}".format(daddr, e))
return
if state == CLOSE:
self._closed.insert(data)
else:
self._open.insert(data)
@property
def windows(self):
return self._open.windows
@property
def stats(self):
self._open.update_window()
self._closed.update_window()
return self._open.stats, self._closed.stats
class StatsPrinter(Thread):
def __init__(self, event_handler, clear_screen=False, interval=1.):
super(StatsPrinter, self).__init__(target=self._print)
self.daemon = True
self.clear_screen = clear_screen
self.interval = float(interval)
self._event_handler = event_handler
self._data_fmt = None
def _print(self):
columns = self._event_handler.windows + ['ALL']
while True:
if self.clear_screen:
os.system('clear')
opened, closed = self._event_handler.stats
self._print_row(columns)
self._print_row(opened, prefix="O")
self._print_row(closed, prefix="C")
time.sleep(self.interval)
def _print_row(self, data, prefix=' '):
c_cnt = len(data) - 1
p_sz = len(prefix) if prefix else 0
c_sz = TERM_WIDTH / len(data)
lc_sz = TERM_WIDTH - p_sz - c_sz * c_cnt
if not self._data_fmt:
self._data_fmt = '{p}' + '{:>{sz}}' * c_cnt + '{:>{lsz}}\n'
result = self._data_fmt.format(*data, p=prefix, sz=c_sz, lsz=lc_sz)
sys.stdout.write(result)
class IPv4Event(Structure):
_fields_ = [
("ts_us", c_uint64),
("saddr", c_uint32),
("daddr", c_uint32),
("sport", c_uint16),
("dport", c_uint16),
("pid", c_uint32),
("state", c_uint8)
]
@property
def data(self):
saddr = struct.pack("I", self.saddr)
daddr = struct.pack("I", self.daddr)
return self.ts_us / 1000000., \
socket.inet_ntop(socket.AF_INET, saddr), \
socket.inet_ntop(socket.AF_INET, daddr), \
self.sport, socket.ntohs(self.dport), \
self.state, chr(4)
class IPv6Event(Structure):
_fields_ = [
("ts_us", c_uint64),
("saddr", c_uint64 * 2),
("daddr", c_uint64 * 2),
("sport", c_uint16),
("dport", c_uint16),
("pid", c_uint32),
("state", c_uint8)
]
@property
def data(self):
mask = (1 << 64) - 1
saddr = struct.pack("2Q", self.saddr[0] >> 64, self.saddr[1] & mask)
daddr = struct.pack("2Q", self.daddr[0] >> 64, self.daddr[1] & mask)
return self.ts_us / 1000000., \
socket.inet_ntop(socket.AF_INET6, saddr), \
socket.inet_ntop(socket.AF_INET6, daddr), \
self.sport, socket.ntohs(self.dport), \
self.state, chr(6)
def run(pid, ignore_private_dst=None, windows=None, clear_screen=False, interval=None):
cflags = CFLAGS + ['-DPID={}'.format(pid)]
event_handler = EventHandler(windows, ignore_private_dst)
stats_printer = StatsPrinter(event_handler, clear_screen, interval=interval)
bpf = BPF(src_file=SOURCE, cflags=cflags)
bpf["ipv4_events"].open_perf_buffer(event_handler.on_ipv4)
bpf["ipv6_events"].open_perf_buffer(event_handler.on_ipv6)
stats_printer.start()
while True:
bpf.kprobe_poll()
def main():
parser = argparse.ArgumentParser(description="Trace TCP accept+open and close called by PID")
parser.add_argument(
"pid",
type=int,
help="Process ID"
)
parser.add_argument(
"-c", "--clear",
action='store_true',
help="Clear screen on update"
)
parser.add_argument(
"-p", "--ignore-private",
action='store_true',
help="Skip private dest IP addresses"
)
parser.add_argument(
"-w", "--windows",
nargs='+',
help="Time window sizes (comma separated) [s]"
)
parser.add_argument(
"-i", "--interval",
type=float,
help="Screen update interval [s]",
default=1.
)
args = parser.parse_args()
run(args.pid,
args.ignore_private,
args.windows,
args.clear,
args.interval)
if __name__ == '__main__':
main()