-
Notifications
You must be signed in to change notification settings - Fork 2
/
sscript.py
262 lines (261 loc) · 9.36 KB
/
sscript.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
255
256
257
258
259
260
261
262
#
# This is the Brute Forcing Program for Serial Connections such as UART.
# Program Name: sbrute.py
# Program Version Number: 1.0
# Author(s): ab@merimetso.net
# Date: 31st October 2020
#
#
import serial
import time
import sys
#
# This is the function that prints/displays the help/user-manual for the program.
#
def usage():
print("\nProgram: sscript.py - Version: 1.0 - Author: ab@merimetso.net - Date: 2020/10/21")
print("")
print("USAGE: sscript.py [-h] [-d device] [-b baudrate] [-U username] [-f commandfile]")
print(" [-v] [ -s loginsuccessstring] [-l usernameprompt] ")
print(" [-p passwordprompt] [-t timeoutvalue] [-P password]")
print("")
print(" Optional Arguments:")
print(" -h Show this help message and exit.")
print(" -d This is the serial device to connect to. The default is:/dev/USB0")
print(" -b This is the baud rate to be used. The default is: 115200")
print(" -f This is the command file to be used. The default is: command.txt")
print(" -l This is string that defines the username logon prompt.")
print(" The default is 'Login:'")
print(" -p This is string that defines the login password prompt. The default")
print(" is 'Password:'")
print(" -P This is the password to login. The default is: letmein")
print(" -s This is string that defines a successful login. The default value:")
print(" is 'admin@localhost$'")
print(" -t This is the timeout value for the serial connection. The default")
print(" value is 1.")
print(" -U This is the username that is to be used for the brute forcing.")
print(" The default value is: admin")
print(" -v This is verbose reporting. By default this is turned: OFF.")
print("")
print(" Example Usage:")
print(" $ sscript.py -d /dev/ttyACM0 -b 115200 -U root -P Admin123 -f batch.txt")
sys.exit(0)
#
# This is the main function that performs the brute forcing function against a serial connection.
#
def main():
# Open the serial connection to the device, else report and error opneing the filem.
ser = serial.Serial()
try:
ser.port = device
ser.baudrate = baudrt
ser.timeout = timout
ser.open()
ser.write("\n".encode())
if verbos:
print('[OK].............Opening Serial Device : ' + str(device))
print(' +-----------> Baud Rate : ' + str(baudrt))
print(' +-----------> TimeOut : ' + str(timout))
except:
print("ERROR: There has been an error trying to configure and open the device: " + str(device))
sys.exit(1)
# Open the command file lese report an error in opening the command file.
try:
comfile = open(psfile, "r")
if verbos: print('[OK].............Opening Command File: ' + str(psfile) + "\n")
except:
print("ERROR: There has been an error trying to open the password file: " + str(device))
sys.exit(1)
# Read Command file and load commands into a list
fn = comfile.readlines()
# Login and Authenticate using username and password provided.
ser.write('\n'.encode())
ser.flush()
while True:
reading = ser.readline()
if reading.decode()[:-1] == sussst:
if verbos:
print("--> Authenticated: Username: " + str(usernm) + " // Password:" + str(passwd) + "\n")
ser.flush()
break
if reading.decode()[:-1] == userst:
username = usernm + "\n"
username = username.encode()
ser.write(username)
ser.flush()
elif reading.decode()[:-1] == passst:
ser.write(passwd.encode())
ser.flush()
ser.write('\n'.encode())
ser.flush()
if verbos:
print("Authenticating with Username: " + str(usernm) + " and Password: " + str(passwd))
else:
ser.flush()
# Execute the command file.
for command in fn:
while True:
if reading.decode()[:-1] == sussst:
if verbos:
print("--> Exeucting command now: " + str(command))
ser.write(command.encode())
ser.flush()
time.sleep(1)
break
else:
ser.write('\n'.encode())
ser.flush()
ser.write('\n'.encode())
ser.flush()
#
#
# This function is used to parse parameters in the command line and return True/False, plus
# their position in the command line list: sys.argv
#
def member(argList, member):
counter = 0
for item in argList:
if argList[counter] == member: return True, counter
counter = counter + 1
return False, -1
#
# This is the element of the program that processes the command line and then invokes the main() function.
#
if __name__ == "__main__":
#
usernm = "admin"
device = "/dev/USB0"
baudrt = 115200
psfile = "commands.txt"
verbos = False
userst = "Login:"
passst = "Password:"
sussst = "admin@localhost"
timout = 1
passwd = 'letmein'
#
if len(sys.argv) == 1: main()
if sys.argv[0] != "sscript.py":
usage()
else:
sys.argv.pop(0)
#
memTruth, index = member(sys.argv, '-h')
if memTruth:
usage()
sys.argv.pop(index)
#
try:
memTruth, index = member(sys.argv, '-v')
if memTruth:
verbos = True
sys.argv.pop(index)
except:
print('ERROR: Error processing "-v" argument in the command line.')
print(' If you need to see the help page then use the "-h" option.\n')
sys.exit(0)
#
try:
memTruth, index = member(sys.argv, '-d')
if memTruth:
sys.argv.pop(index)
device = sys.argv.pop(index)
except:
print('ERROR: Error processing "-d" argument in the command line.')
print(' If you need to see the help page then use the "-h" option.\n')
sys.exit(0)
#
try:
memTruth, index = member(sys.argv, '-b')
if memTruth:
sys.argv.pop(index)
baudrt = sys.argv.pop(index)
except:
print('ERROR: Error processing "-b" argument in the command line.')
print(' If you need to see the help page then use the "-h" option.\n')
sys.exit(0)
#
try:
memTruth, index = member(sys.argv, '-f')
if memTruth:
sys.argv.pop(index)
psfile = sys.argv.pop(index)
except:
print('ERROR: Error processing "-f" argument in the command line.')
print(' If you need to see the help page then use the "-h" option.\n')
sys.exit(0)
#
try:
memTruth, index = member(sys.argv, '-l')
if memTruth:
sys.argv.pop(index)
userst = sys.argv.pop(index)
except:
print('ERROR: Error processing "-l" argument in the command line.')
print(' If you need to see the help page then use the "-h" option.\n')
sys.exit(0)
#
try:
memTruth, index = member(sys.argv, '-p')
if memTruth:
sys.argv.pop(index)
passst = sys.argv.pop(index)
except:
print('ERROR: Error processing "-p" argument in the command line.')
print(' If you need to see the help page then use the "-h" option.\n')
sys.exit(0)
#
try:
memTruth, index = member(sys.argv, '-P')
if memTruth:
sys.argv.pop(index)
passwd = sys.argv.pop(index)
except:
print('ERROR: Error processing "-P" argument in the command line.')
print(' If you need to see the help page then use the "-h" option.\n')
sys.exit(0)
#
try:
memTruth, index = member(sys.argv, '-s')
if memTruth:
sys.argv.pop(index)
sussst = sys.argv.pop(index)
except:
print('ERROR: Error processing "-s" argument in the command line.')
print(' If you need to see the help page then use the "-h" option.\n')
sys.exit(0)
#
try:
memTruth, index = member(sys.argv, '-t')
if memTruth:
sys.argv.pop(index)
timout = sys.argv.pop(index)
except:
print('ERROR: Error processing "-t" argument in the command line.')
print(' If you need to see the help page then use the "-h" option.\n')
sys.exit(0)
#
try:
memTruth, index = member(sys.argv, '-U')
if memTruth:
sys.argv.pop(index)
usernm = sys.argv.pop(index)
except:
print('ERROR: Error processing "-U" argument in the command line.')
print(' If you need to see the help page then use the "-h" option.\n')
sys.exit(0)
#
if len(sys.argv) == 0:
print("\nRunning Serial Brute Forcing Too - Version: 1.0 - Author:ab@merimetso.net\n")
main()
sys.exit(1)
else:
print('ERROR: There is an illegal parameter in the command line.')
print(' If you need to see the help page then use the "-h" option.\n')
sys.exit(1)
#
#
#
# The end of the program - Last updated 31st Oct 2020 - ab@merimetso.net
#
#