-
Notifications
You must be signed in to change notification settings - Fork 140
/
pinject.py
254 lines (244 loc) · 7.69 KB
/
pinject.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
import socket
import struct
import threading
import time
import sys
import random
from optparse import OptionParser
ETH_P_IP = 0x0800 # Internet Protocol Packet
def checksum(data):
s = 0
n = len(data) % 2
for i in range(0, len(data)-n, 2):
s+= ord(data[i]) + (ord(data[i+1]) << 8)
if n:
s+= ord(data[i+1])
while (s >> 16):
s = (s & 0xFFFF) + (s >> 16)
s = ~s & 0xffff
return s
class layer():
pass
class ETHER(object):
def __init__(self, src, dst, type=ETH_P_IP):
self.src = src
self.dst = dst
self.type = type
def pack(self):
ethernet = struct.pack('!6s6sH',
self.dst,
self.src,
self.type)
return ethernet
class IP(object):
def __init__(self, source, destination, payload='', proto=socket.IPPROTO_TCP):
self.version = 4
self.ihl = 5 # Internet Header Length
self.tos = 0 # Type of Service
self.tl = 20+len(payload)
self.id = 0#random.randint(0, 65535)
self.flags = 0 # Don't fragment
self.offset = 0
self.ttl = 255
self.protocol = proto
self.checksum = 2 # will be filled by kernel
self.source = socket.inet_aton(source)
self.destination = socket.inet_aton(destination)
def pack(self):
ver_ihl = (self.version << 4) + self.ihl
flags_offset = (self.flags << 13) + self.offset
ip_header = struct.pack("!BBHHHBBH4s4s",
ver_ihl,
self.tos,
self.tl,
self.id,
flags_offset,
self.ttl,
self.protocol,
self.checksum,
self.source,
self.destination)
self.checksum = checksum(ip_header)
ip_header = struct.pack("!BBHHHBBH4s4s",
ver_ihl,
self.tos,
self.tl,
self.id,
flags_offset,
self.ttl,
self.protocol,
socket.htons(self.checksum),
self.source,
self.destination)
return ip_header
def unpack(self, packet):
_ip = layer()
_ip.ihl = (ord(packet[0]) & 0xf) * 4
iph = struct.unpack("!BBHHHBBH4s4s", packet[:_ip.ihl])
_ip.ver = iph[0] >> 4
_ip.tos = iph[1]
_ip.length = iph[2]
_ip.ids = iph[3]
_ip.flags = iph[4] >> 13
_ip.offset = iph[4] & 0x1FFF
_ip.ttl = iph[5]
_ip.protocol = iph[6]
_ip.checksum = hex(iph[7])
_ip.src = socket.inet_ntoa(iph[8])
_ip.dst = socket.inet_ntoa(iph[9])
_ip.list = [
_ip.ihl,
_ip.ver,
_ip.tos,
_ip.length,
_ip.ids,
_ip.flags,
_ip.offset,
_ip.ttl,
_ip.protocol,
_ip.src,
_ip.dst]
return _ip
class TCP(object):
def __init__(self, srcp, dstp):
self.srcp = srcp
self.dstp = dstp
self.seqn = 10
self.ackn = 0
self.offset = 5 # Data offset: 5x4 = 20 bytes
self.reserved = 0
self.urg = 0
self.ack = 0
self.psh = 0
self.rst = 0
self.syn = 1
self.fin = 0
self.window = socket.htons(5840)
self.checksum = 0
self.urgp = 0
self.payload = ""
def pack(self, source, destination):
data_offset = (self.offset << 4) + 0
flags = self.fin + (self.syn << 1) + (self.rst << 2) + (self.psh << 3) + (self.ack << 4) + (self.urg << 5)
tcp_header = struct.pack('!HHLLBBHHH',
self.srcp,
self.dstp,
self.seqn,
self.ackn,
data_offset,
flags,
self.window,
self.checksum,
self.urgp)
#pseudo header fields
source_ip = source
destination_ip = destination
reserved = 0
protocol = socket.IPPROTO_TCP
total_length = len(tcp_header) + len(self.payload)
# Pseudo header
psh = struct.pack("!4s4sBBH",
source_ip,
destination_ip,
reserved,
protocol,
total_length)
psh = psh + tcp_header + self.payload
tcp_checksum = checksum(psh)
tcp_header = struct.pack("!HHLLBBH",
self.srcp,
self.dstp,
self.seqn,
self.ackn,
data_offset,
flags,
self.window)
tcp_header+= struct.pack('H', tcp_checksum) + struct.pack('!H', self.urgp)
return tcp_header
def unpack(self, packet):
cflags = { # Control flags
32:"U",
16:"A",
8:"P",
4:"R",
2:"S",
1:"F"}
_tcp = layer()
_tcp.thl = (ord(packet[12])>>4) * 4
_tcp.options = packet[20:_tcp.thl]
_tcp.payload = packet[_tcp.thl:]
tcph = struct.unpack("!HHLLBBHHH", packet[:20])
_tcp.srcp = tcph[0] # source port
_tcp.dstp = tcph[1] # destination port
_tcp.seq = tcph[2] # sequence number
_tcp.ack = hex(tcph[3]) # acknowledgment number
_tcp.flags = ""
for f in cflags:
if tcph[5] & f:
_tcp.flags+=cflags[f]
_tcp.window = tcph[6] # window
_tcp.checksum = hex(tcph[7]) # checksum
_tcp.urg = tcph[8] # urgent pointer
_tcp.list = [
_tcp.srcp,
_tcp.dstp,
_tcp.seq,
_tcp.ack,
_tcp.thl,
_tcp.flags,
_tcp.window,
_tcp.checksum,
_tcp.urg,
_tcp.options,
_tcp.payload]
return _tcp
class UDP(object):
def __init__(self, src, dst, payload=''):
self.src = src
self.dst = dst
self.payload = payload
self.checksum = 0
self.length = 8 # UDP Header length
def pack(self, src, dst, proto=socket.IPPROTO_UDP):
length = self.length + len(self.payload)
pseudo_header = struct.pack('!4s4sBBH',
socket.inet_aton(src), socket.inet_aton(dst), 0,
proto, length)
self.checksum = checksum(pseudo_header)
packet = struct.pack('!HHHH',
self.src, self.dst, length, 0)
return packet
def main():
parser = OptionParser()
parser.add_option("-s", "--src", dest="src", type="string",
help="Source IP address", metavar="IP")
parser.add_option("-d", "--dst", dest="dst", type="string",
help="Destination IP address", metavar="IP")
options, args = parser.parse_args()
if options.dst == None:
parser.print_help()
sys.exit()
else:
dst_host = socket.gethostbyname(options.dst)
if options.src == None:
# Get the current Network Interface
src_host = socket.gethostbyname(socket.gethostname())
else:
src_host = options.src
print("[+] Local Machine: %s"%src_host)
print("[+] Remote Machine: %s"%dst_host)
data = "TEST!!"
print("[+] Data to inject: %s"%data)
# IP Header
ipobj = IP(src_host, dst_host)
# TCP Header
tcpobj = TCP(1234, 80)
response = send(ipobj, tcpobj, iface="eth0", retry=1, timeout=0.3)
if response:
ip = ipobj.unpack(response)
response = response[ip.ihl:]
tcp = tcpobj.unpack(response)
print "IP Header:", ip.list
print "TCP Header:", tcp.list
if __name__=="__main__":
main()