-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommon.py~
executable file
·170 lines (148 loc) · 5.15 KB
/
common.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
#!/usr/bin/env python
import time
import threading
from scapy.all import *
import sys
import socket
import json
import Queue
import interfaces
maxhop = 25
# A request that will trigger the great firewall but will NOT cause
# the web server to process the connection. You probably want it here
triggerfetch = """YOU MIGHT WANT SOMETHING HERE"""
# A couple useful functions that take scapy packets
def isRST(p):
return (TCP in p) and (p[IP][TCP].flags & 0x4 != 0)
def isICMP(p):
return ICMP in p
def isTimeExceeded(p):
return ICMP in p and p[IP][ICMP].type == 11
# A general python object to handle a lot of this stuff...
#
# Use this to implement the actual functions you need.
class PacketUtils:
def __init__(self, dst=None):
# Get one's SRC IP & interface
i = interfaces.interfaces()
self.src = i[1][0]
self.iface = i[0]
self.netmask = i[1][1]
self.enet = i[2]
self.dst = dst
sys.stderr.write("SIP IP %s, iface %s, netmask %s, enet %s\n" %
(self.src, self.iface, self.netmask, self.enet))
# A queue where received packets go. If it is full
# packets are dropped.
self.packetQueue = Queue.Queue(100000)
self.dropCount = 0
self.idcount = 0
self.ethrdst = ""
# Get the destination ethernet address with an ARP
self.arp()
# You can add other stuff in here to, e.g. keep track of
# outstanding ports, etc.
# Start the packet sniffer
t = threading.Thread(target=self.run_sniffer)
t.daemon = True
t.start()
time.sleep(.1)
# generates an ARP request
def arp(self):
e = Ether(dst="ff:ff:ff:ff:ff:ff",
type=0x0806)
gateway = ""
srcs = self.src.split('.')
netmask = self.netmask.split('.')
for x in range(4):
nm = int(netmask[x])
addr = int(srcs[x])
if x == 3:
gateway += "%i" % (addr & nm + 1)
else:
gateway += ("%i" % (addr & nm)) + "."
sys.stderr.write("Gateway %s\n" % gateway)
a = ARP(hwsrc=self.enet,
pdst=gateway)
p = srp1([e/a], iface=self.iface, verbose=0)
self.etherdst = p[Ether].src
sys.stderr.write("Ethernet destination %s\n" % (self.etherdst))
# A function to send an individual packet.
def send_pkt(self, payload=None, ttl=32, flags="",
seq=None, ack=None,
sport=None, dport=80,ipid=None,
dip=None,debug=False):
if sport == None:
sport = random.randint(1024, 32000)
if seq == None:
seq = random.randint(1, 31313131)
if ack == None:
ack = random.randint(1, 31313131)
if ipid == None:
ipid = self.idcount
self.idcount += 1
t = TCP(sport=sport, dport=dport,
flags=flags, seq=seq, ack=ack)
ip = IP(src=self.src,
dst=self.dst,
id=ipid,
ttl=ttl)
p = ip/t
if payload:
p = ip/t/payload
else:
pass
e = Ether(dst=self.etherdst,
type=0x0800)
# Have to send as Ethernet to avoid interface issues
sendp([e/p], verbose=1, iface=self.iface)
# Limit to 20 PPS.
time.sleep(.05)
# And return the packet for reference
return p
# Has an automatic 5 second timeout.
def get_pkt(self, timeout=5):
try:
return self.packetQueue.get(True, timeout)
except Queue.Empty:
return None
# The function that actually does the sniffing
def sniffer(self, packet):
try:
# non-blocking: if it fails, it fails
self.packetQueue.put(packet, False)
except Queue.Full:
if self.dropCount % 1000 == 0:
sys.stderr.write("*")
sys.stderr.flush()
self.dropCount += 1
def run_sniffer(self):
sys.stderr.write("Sniffer started\n")
rule = "src net %s or icmp" % self.dst
sys.stderr.write("Sniffer rule \"%s\"\n" % rule);
sniff(prn=self.sniffer,
filter=rule,
iface=self.iface,
store=0)
# Sends the message to the target in such a way
# that the target receives the msg without
# interference by the Great Firewall.
#
# ttl is a ttl which triggers the Great Firewall but is before the
# server itself (from a previous traceroute incantation
def evade(self, target, msg, ttl):
return "NEED TO IMPLEMENT"
# Returns "DEAD" if server isn't alive,
# "LIVE" if teh server is alive,
# "FIREWALL" if it is behind the Great Firewall
def ping(self, target):
# self.send_msg([triggerfetch], dst=target, syn=True)
return "NEED TO IMPLEMENT"
# Format is
# ([], [])
# The first list is the list of IPs that have a hop
# or none if none
# The second list is T/F
# if there is a RST back for that particular request
def traceroute(self, target, hops):
return "NEED TO IMPLEMENT"