-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstarbucks.py
188 lines (168 loc) · 6.3 KB
/
starbucks.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
import socket
import time
import icmp
import threading
import sys
BUFFER_SIZE = 2048
DEBUG = False
DRINKER = 88
TARGET = 87
ICMPID = 50614
count = -1
ic = socket.getprotobyname('icmp')
def wrap(data, code, ident):
p = icmp.ICMPPacket()
global count
count += 1
return p.create(0, code, ident, count, data)
#-------------------------------
#Now just wanna solve local:-1 local:22
class I2T_pipe(threading.Thread):
'''This thread listens to an ICMP socket. For the initializtion,
the address of the 1st ICMP packet will be used when the ICMP
socket acts as a forwarder.
Basically, it relays ICMP packets to the established TCP port.
Precondition: source is an ICMP socket binded to ''.
sink is a TCP socket has not connect to any yet.
TCP_ADDRESS is the address for the tcp to connect.
Note: I think the tcp socket has to be connected to the target port
AFTER the 1st ICMP packet is receive. So as to forward a SYN
or other stuffs to the target. Otherwise, the drinker cannot
get the initialization info from the target port.
'''
def __init__(self, source, sink, TCP_ADDRESS):
threading.Thread.__init__(self)
self.source, self.sink = source, sink
self.TCP_ADDRESS = TCP_ADDRESS
def run(self):
reverseReady = False
first = True
name = threading.currentThread().getName() + str(self.__class__)
if DEBUG: print 'START %s' % name
while True:
buf = self.source.recv(BUFFER_SIZE)
if not buf: break
p = icmp.ICMPPacket()
data = p.parse(buf, DEBUG)
#code, address, ident, data = rip(buf)
global ICMPID
ICMPID = p.id
code = int(p.code)
address = socket.inet_ntoa(p.src)
if int(code) != DRINKER: continue
if not reverseReady:
if DEBUG: print '%s: Got the 1st ICMP packet.' % name
self.sink.connect((self.TCP_ADDRESS))
if DEBUG: print '%s: TCP ESTABLISHED: %s -> %s' % (name, self.sink.getsockname(), self.sink.getpeername())
T2I_pipe(self.sink, self.source, address).start()
time.sleep(1)
reverseReady = True
first = False
else:
self.sink.send(data)
# else:
# if DEBUG: print '%s: Got an ICMP packet not from the drinker' % name
if DEBUG:
print '%s: ICMP from %s RELAYto %s -> %s' % \
(name, address, self.sink.getsockname(), self.sink.getpeername())
class T2I_pipe(threading.Thread):
'''This thread has a readily tcp connection to port 22 on target.
It has an IP address that tells where the ICMP socket to relay
the TCP packets to.
Why a 22 in ICMP_ADDRESS is unknown. (Any number will do)
Precondition: source is a connected TCP socket.
sink is an ICMP socket which send packets to ICMP_ADDRESS.
'''
def __init__(self, source, sink, ICMP_ADDRESS):
threading.Thread.__init__(self)
self.source, self.sink = source, sink
self.ICMP_ADDRESS = ICMP_ADDRESS
def run(self):
name = threading.currentThread().getName() + str(self.__class__)
if DEBUG: print 'START %s' % name
while True:
buf = self.source.recv(BUFFER_SIZE)
if not buf: break
data = wrap(buf, TARGET, ICMPID)
self.sink.sendto(data, (self.ICMP_ADDRESS, 22))
if DEBUG: print '%s: %s -> %s RELAYto ICMP sendto %s.' % \
(name, self.source.getpeername(), self.source.getsockname(), self.ICMP_ADDRESS)
#---------------------------------
class Pinhole():
'''This is a solution for: python <name> local:-1 local:22'''
def __init__(self, listen, send):
self.listen, self.send = listen, send
def run(self):
source = socket.socket(socket.AF_INET, socket.SOCK_RAW, ic)
fwd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
source.bind(('', 22))
I2T_pipe(source, fwd, (self.send[0], self.send[1])).start()
#########################DRINKER######################
class Pipe_T2I(threading.Thread):
'''This thread listens on drinker's port and send ICMP to the
target.
'''
def __init__(self, source, sink, ICMP_ADDRESS):
threading.Thread.__init__(self)
self.source, self.sink = source, sink
self.ICMP_ADDRESS = ICMP_ADDRESS
def run(self):
name = threading.currentThread().getName() + str(self.__class__)
if DEBUG: print 'START %s' % name
self.source.listen(5)
newsock, address = self.source.accept()
if DEBUG: print '%s: Got a connection: %s->%s' % \
(name, newsock.getpeername(), newsock.getsockname())
Pipe_I2T(self.sink, newsock).start()
time.sleep(1)
signal = wrap('Halo', DRINKER, 50614)
self.sink.sendto(signal, (self.ICMP_ADDRESS, 22))
if DEBUG: print '%s: Send ICMP SYN signal to %s' % (name, self.ICMP_ADDRESS)
while True:
buf = newsock.recv(BUFFER_SIZE)
if not buf: break
global ICMPID
data = wrap(buf, DRINKER, ICMPID)
self.sink.sendto(data, (self.ICMP_ADDRESS, 22))
if DEBUG: print '%s: %s -> %s RELAYto ICMP -> %s' % \
(name, newsock.getpeername(), newsock.getsockname(), self.ICMP_ADDRESS)
class Pipe_I2T(threading.Thread):
'''Precondition: source is an ICMP socket.
sink is a connected tcp socket.
In this thread,
it receives from the target ICMP socket and relay the packet
to the tcp socket.
'''
def __init__(self, source, sink):
threading.Thread.__init__(self)
self.source, self.sink = source, sink
def run(self):
name = threading.currentThread().getName() + str(self.__class__)
if DEBUG: print 'START %s' % name
while True:
buf = self.source.recv(BUFFER_SIZE)
if not buf: break
p = icmp.ICMPPacket()
data = p.parse(buf, DEBUG)
code = int(p.code)
address = socket.inet_ntoa(p.src)
if int(code) != TARGET: continue
self.sink.send(data)
if DEBUG: print '%s: ICMP from %s RELAYto %s -> %s' % \
(name, address, self.sink.getsockname(), self.sink.getpeername())
class Holepin():
'''This is a solution for: python <name> local:1234 chenhuo.org:-1'''
def __init__(self, listen, send):
self.listen, self.send = listen, send
def run(self):
source = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
source.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
fwd = socket.socket(socket.AF_INET, socket.SOCK_RAW, ic)
fwd.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
source.bind(('', self.listen[1]))
Pipe_T2I(source, fwd, self.send[0]).start()
if __name__ == '__main__':
listen = (sys.argv[1].split(':')[0], int(sys.argv[1].split(':')[1]))
send = (sys.argv[2].split(':')[0], int(sys.argv[2].split(':')[1]))
if listen[1] < 0: Pinhole(listen, send).run()
else: Holepin(listen, send).run()