-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecryptp.py
More file actions
executable file
·163 lines (131 loc) · 5.4 KB
/
decryptp.py
File metadata and controls
executable file
·163 lines (131 loc) · 5.4 KB
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
#!/usr/bin/env python3
"""Main module for cross platform TP-Link Easy Smart Switch Configuration tool"""
import getopt
from sys import argv
import socket
from TLPresentation import present_discovery
from TLPacket import TLPacket
from TLCrypt import tl_rc4_crypt
from TLActions import tl_discover, TLSwitch, tl_get_token,\
PORTSC, PORTCS, tl_init_sockets, tl_test
def choose_switch(switch_ip_arg: str=None):
"""Discover switches, list details and display selection prompt."""
if switch_ip_arg is not None:
print('Only trying {0}\n'.format(switch_ip_arg))
tl_discover(switch_ip_arg)
else:
tl_discover()
if len(TLSwitch.discovered_switches) > 1:
print(' {0:2s}{1:31s} {2:15s} {3:31s} {4:10s}'
.format('#', 'Name', 'IP', 'Model', 'Firmware'))
for i, switch in enumerate(TLSwitch.discovered_switches):
print('{0:2d} '.format(i), end='')
present_discovery(switch.source_packet)
selection = None
while selection is None:
selection_raw = input('Select switch: ')
if (selection_raw.isnumeric() and
int(selection_raw) in range(0, len(TLSwitch.discovered_switches))):
selection = int(selection_raw)
selected_switch = TLSwitch.discovered_switches[selection]
elif len(TLSwitch.discovered_switches) == 1:
print('{0:31s} {1:15s} {2:31s} {3:10s}'
.format('Name', 'IP', 'Model', 'Firmware'))
present_discovery(TLSwitch.discovered_switches[0].source_packet)
selected_switch = TLSwitch.discovered_switches[0]
print()
else:
print('No switches discovered.')
selected_switch = None
return selected_switch
def decrypt_test_dot_raw():
"""Decrypt the packet stored in test.raw, save it to test.dec and display summary"""
with open('test.raw', 'rb') as encrypted_file:
data = encrypted_file.read()
out = tl_rc4_crypt(data)
with open('test.dec', 'wb') as outfile:
outfile.write(out)
packet = TLPacket(out)
with open('test2.dec', 'wb') as outfile:
outfile.write(packet.to_byte_array())
print(packet)
def main():
"""The main method."""
try:
opts = getopt.getopt(argv[1:], 'ldi:')[0]
except getopt.GetoptError:
opts = []
except:
raise
only_decrypt = False
only_listen_and_decrypt = False
switch_ip_arg = None
for opt, arg in opts:
if opt == '-i':
switch_ip_arg = arg
elif opt == '-d':
only_decrypt = True
elif opt == '-l':
only_listen_and_decrypt = True
if only_listen_and_decrypt:
receive1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
receive1.bind(('0.0.0.0', PORTSC))
receive1.setblocking(False)
receive2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
receive2.bind(('0.0.0.0', PORTCS))
receive2.setblocking(False)
while True:
try:
data = receive1.recvfrom(1500)[0]
packet = TLPacket(tl_rc4_crypt(data))
print('\u001B[94m-----> Switch to computer\n{0}'
'\n<----- Switch to computer\n\u001B[0m'.format(str(packet)))
except IOError:
pass
try:
data = receive2.recvfrom(1500)[0]
packet = TLPacket(tl_rc4_crypt(data))
print('\u001B[91m-----> Computer to switch\n{0}'
'\n<----- Computer to switch\n\u001B[0m'.format(str(packet)))
except IOError:
pass
elif only_decrypt:
decrypt_test_dot_raw()
else:
tl_init_sockets()
selected_switch = choose_switch(switch_ip_arg)
for i in range(25700, 65536):
if i % 100 == 0:
print(i)
token = tl_get_token(selected_switch.mac, selected_switch.ip4)
sent, rec = tl_test(i, selected_switch.mac, selected_switch.ip4, token, .3)
if rec is not None:
print('\u001B[91m-----> Computer to switch\n{0}'
'\n<----- Computer to switch\n\u001B[0m'.format(str(sent)))
print('\u001B[94m-----> Switch to computer\n{0}'
'\n<----- Switch to computer\n\u001B[0m'.format(str(rec)))
# stats = tl_get_port_statistics(selected_switch.mac, selected_switch.ip4, 1000)
# present_port_statistics(stats)
# if selected_switch is not None:
# token = tl_get_token(selected_switch.mac, selected_switch.ip4)
#
# logged_in = False
# if token is not None:
# username = '' # type: str
# password = '' # type: str
#
# while not logged_in:
# username = input('User: ')
# password = getpass('Password: ')
#
# login_status = tl_login(selected_switch.mac, selected_switch.ip4,
# token, username, password) # type: int
# if login_status == 1:
# print('Wrong credentials\n')
# elif login_status != 0:
# print('Login failed ({0:d})\n'.format(login_status))
# else:
# print('Login successful\n')
# logged_in = True
if __name__ == '__main__':
main()