-
Notifications
You must be signed in to change notification settings - Fork 1
/
rifsniff-client.py
executable file
·150 lines (110 loc) · 4.76 KB
/
rifsniff-client.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
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
"""
RIfSniff Packets Receiver
Receives packets sniffed by `rifsniff_collect` collector program. Packets
can be written on a tun/tap interface or printed to stdout in tcpdump format.
@author: [email protected]
"""
import sys
import pytun
import socket
import logging
import argparse
from binascii import hexlify
from rifsniff import utils
from rifsniff.proto import RIfSniffClientSocket, \
PROTOVERSION, CMD_LIST, CMD_SNIFF, RESP_OK, RESP_KO
VERSION = '0.1'
log = logging.getLogger()
def configure_logger(log):
log.setLevel(logging.DEBUG)
stream = logging.StreamHandler()
stream_fmt = logging.Formatter('[%(process)d] <%(levelname)s> %(message)s')
stream.setFormatter(stream_fmt)
stream.setLevel(logging.DEBUG)
log.addHandler(stream)
def main():
configure_logger(log)
log.info('This is RIfSniff Receiver v%s' % VERSION)
parser = argparse.ArgumentParser(description='RIfSniff Packet Receiver')
parser.add_argument('--version', action='version', version='%(prog)s 0.1')
parser.add_argument('-a', '--address', type=str, default='localhost',
help='server (collector) address')
parser.add_argument('-p', '--port', type=int, default=6384,
help='port where the collector is listening')
parser.add_argument('-L', '--list', action='store_true',
help=('Lists available remote interfaces (if any) '
'and exits'))
parser.add_argument('-r', '--remote', type=str,
help='[mandatory] Remote interface name')
parser.add_argument('-l', '--local', type=str, default='tap0',
help=('Name of the local virtual interface '
'(default: tap0)'))
parser.add_argument('-f', '--filter', type=str, default='',
help=('BPF Filter to attach to remote interface '
'(default: none)'))
parser.add_argument('-s', '--snaplen', type=int, default=1500,
help='Truncate packet at length (default: 1500)')
parser.add_argument('-P', '--promisc', type=bool, default=False,
help='Put interface in promiscuos mode')
parser.add_argument('-m', '--monitor', type=bool, default=False,
help='Put interface in monitor mode')
args = parser.parse_args()
if not args.list and not args.remote:
log.error('Remote interface name missing. Use --remote to select\n')
parser.print_usage()
sys.exit(1)
sock = RIfSniffClientSocket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(10.0)
server_addr = (args.address, args.port)
sock.connect(server_addr)
log.info('connection established. sending request...')
try:
if sock.check_proto_version():
log.info('protocol versions match [%s]' %
(hexlify(PROTOVERSION)))
if args.list:
sock.send_cmd(CMD_LIST)
devs = sock.recv_pyobj()
for dev in devs:
utils.print_device_description(dev)
else:
tap = pytun.TunTapDevice(name=args.local,
flags=pytun.IFF_TAP | pytun.IFF_NO_PI)
tap.up()
log.info('Tap device created. Attach your sniffer, if you want, '
'and press any key to continue...')
raw_input()
tap.mtu = args.snaplen
log.debug('Tap MTU before activation is %d' % tap.mtu)
log.info(args)
sock.send_cmd(CMD_SNIFF)
sock.send_capture_opts(args.remote, args.snaplen, args.filter,
args.promisc, args.monitor)
resp, payload = sock.recv_cmd()
if resp == RESP_KO:
log.error('Remote error')
# receive error description and log it...
elif resp == RESP_OK:
log.info('Everything Ok. Preparing to receive packets from remote')
# TODO: create another channel for packets, using the other one
# to exchange execution status
while True:
pktlen, pkt = sock.recv_packet()
log.debug('%d bytes packet sniffed from %s'
% (pktlen, args.remote))
tap.write(pkt)
log.debug('%d bytes packet wrote into %s'
% (pktlen, args.local))
else:
log.error('Something weird happened!')
log.info('shutting down connection')
sock.shutdown(socket.SHUT_RD)
except:
sock.shutdown(socket.SHUT_RD)
raise
finally:
sock.close()
if __name__ == '__main__':
main()