-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·75 lines (59 loc) · 1.19 KB
/
main.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
#!/usr/bin/env python2
QUEUE_NUM = 5
#hush verbosity
import logging
l=logging.getLogger("scapy.runtime")
l.setLevel(49)
import os,sys,time
from sys import stdout as out
import nfqueue,socket
from scapy.all import *
import GeoIP
gi = GeoIP.open("GeoLiteCity.dat",GeoIP.GEOIP_STANDARD)
lastip=""
def DoGeoIP(pkt):
global lastip
ip = pkt[IP].src
if lastip==ip:
out.write('.')
out.flush()
return
lastip=ip
gir = gi.record_by_addr(ip)
if gir != None:
out.write("\n%s %s %s %s "%(
time.strftime("%Y-%m-%d %H:%M:%S"),
ip,
gir['country_name'] or "?",
gir['city'] or "?"))
out.flush()
def process_packet(dummy, payload):
payload.set_verdict(nfqueue.NF_ACCEPT)
data = payload.get_data()
pkt = IP(data)
proto = pkt.proto
if proto is 0x01:
if pkt[ICMP].type is 8:
DoGeoIP(pkt)
#automatic iptables rules?
def hook():
pass
def unhook():
pass
def main():
q = nfqueue.queue()
q.open()
q.bind(socket.AF_INET)
q.set_callback(process_packet)
q.create_queue(QUEUE_NUM)
try:
hook()
q.try_run()
except KeyboardInterrupt:
unhook()
print("Exit...")
q.unbind(socket.AF_INET)
q.close()
sys.exit(0)
print("Listening on queue number "+str(QUEUE_NUM))
main()