This repository has been archived by the owner on Nov 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscipio.py
executable file
·358 lines (295 loc) · 10.9 KB
/
scipio.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#valtyr
import argparse
import random
import json
import requests
import time
import base64
import binascii
import string
import re
import file_manipulator as fm
import perceptron
def print_title():
lines=fm.read_file("titles/scipio_title.txt")
for line in lines:
print(line.strip('\n'))
print(" <3 valtyr")
return
def save_training_data(target,blob):
#save server challenge and answer in a training file
line=target+":"+blob+"\n"
fm.write_file('data/scipio_training_data.txt',line,'a')
def parse_response(response):
#retrieve target from json
#decode blob from base64 into hex
try:
targets=response.json()['target']
blob=(binascii.hexlify(base64.b64decode(response.json()['binary']))).decode('ascii')
except:
print("ERR: Json Parse Failed")
return (targets, blob)
def start_session():
return requests.session()
def request():
#request challenge from server
url = base_url.format("challenge")
time.sleep(speed)
try:
response=session.get(url,headers=token)
if response.status_code == 429:
print('ERR: Rate Limit Exception: Waiting 60 seconds...')
time.sleep(60)
print("60 seconds elapsed...continuing...")
except:
print("ERR: Request Failed")
return parse_response(response)
def solve(target):
#send answer to server
time.sleep(speed)
url = base_url.format("solve")
response=session.post(url,data={"target":target})
return response
def save_weights():
#loop through all perceptrons and save weights to file
weights=[]
for percep in perceptrons:
weights.append(percep.label+":"+str(percep.weight)+'\n')
fm.write_file('data/weights.txt',weights,'w+')
return
def save_hash(line):
fm.write_file('data/hashes.txt',line,'a')
return
def ask_hash(email):
#request challenge hash when count>=500
#https://mlb.praetorian.com/hash
url=base_url.format("hash")
response=session.post(url, data={"email":email})
save_hash(response.json())
return
def get_user():
#if no user file - create one using email - for user setup
tmp=fm.read_file("data/user.txt")
if tmp!=None:
return tmp
else:
print("\nUser File Not Found: Please use aeneas to init a user file")
exit(0)
def load_weights(file_name):
#default location for data files
data=fm.read_file('data/'+str(file_name))
if data==None:
#alt location
data=fm.read_file(str(file_name))
temp=[]
for item in data:
temp.append([float(x) for x in str(item.split(':')[1]).replace('\n','').replace('[','').replace(']','').split(', ')])
i=0
for perceptron in perceptrons:
perceptron.weight=temp[i]
i=i+1
print("Weights loaded!")
return
def get_training_data():
file_name="data/scipio_training_data.txt"
data=fm.read_file(file_name)
data_clean=[]
#vet data
for line in data:
data_clean.append(line.strip('\n').split(':'))
return data_clean
def compute_frequency(blob,chunk):
frequency=[]
blob = re.findall(chunk, blob)
for section in blob:
i=0
for letter in alphabet:
frequency.append(section.count(letter))
while i < 10:
frequency.append(section.count(str(i)))
i=i+1
return frequency
def print_perceptrons():
for perceptron in perceptrons:
print("Node: "+perceptron.label+"\t\tweights: "+str(perceptron.weight))
def train():
training_data=get_training_data()
i=0
while i < epochs:
num_correct=0
for line in training_data:
count={}
for percep in perceptrons:
percep.input=compute_frequency(line[1],percep.chunk)
percep.get_output()
for percep in perceptrons:
if percep.output==1:
if percep.label in count.keys():
count[percep.label]=count[percep.label]+1
else:
count[percep.label]=1
try:
guess=max(count,key=count.get)
except:
guess=None
while guess==None:
for percep in perceptrons:
percep.adjust_weight(line[0])
for percep in perceptrons:
percep.input=compute_frequency(line[1],percep.chunk)
percep.get_output()
for percep in perceptrons:
if percep.output==1:
if percep.label in count.keys():
count[percep.label]=count[percep.label]+1
else:
count[percep.label]=1
try:
guess=max(count,key=count.get)
except:
guess=None
if guess==line[0]:
num_correct=num_correct+1
for percep in perceptrons:
percep.adjust_weight(line[0])
percep.mod=random.choice([0,1])
num_total=int(len(training_data))
print("epoch: "+str(i+1)+"\tcorrect: "+str(num_correct)+"\ttotal: "+str(num_total)+"\taccuracy: "+str((num_correct/num_total)*100)+"%")
i=i+1
save_weights()
return
def execute():
counter=0
streak=0
top_streak=int(fm.read_file('data/top_streak.txt')[0])
time_start=time.time()
while True:
(targets, blob)=request()
#set all outputs
for percep in perceptrons:
percep.input=compute_frequency(blob,percep.chunk)
percep.get_output()
count={}
for percep in perceptrons:
if percep.output==1 and targets.__contains__(percep.label):
if percep.label in count.keys():
count[percep.label]=count[percep.label]+1
else:
count[percep.label]=1
try:
guess=max(count,key=count.get)
#if confidence low ie low fire rate .. raise and pull
net_conf=int(count.get(guess))
if net_conf <= confidence:
print('low answer confidence...low fire['+str(int(count.get(guess)))+"]")
raise('')
print("Guess:\t"+guess+'\tConfidence: '+str(net_conf))
response=solve(guess)
correct_resp=response.json()['correct']
target=response.json()['target']
accuracy=response.json()['accuracy']
try:
hash_resp=response.json()['hash']
print("HASH COLLECTED!")
time_stop=time.time()
time_total=time_stop-time_start
save_hash("Streak: "+str(fm.read_file('data/top_streak.txt'))+"Correct: "+str(correct_resp)+"\tAccuracy: "+str(accuracy)+"\tTotal Time: "+str(time_total)+"\nHash: "+hash_resp+"\n")
# ask_hash(email)
except:
hash_resp="None"
print('Answer: '+target+'\tNumber: '+str(correct_resp)+"\tAccuracy: "+str(accuracy))
if(target==guess):
streak=streak+1
print("Streak: "+str(streak)+'\n')
else:
print('streak reset...\n')
streak=0
for percep in perceptrons:
percep.adjust_weight(target)
percep.mod=random.choice([0,1])
counter=counter+1
save_training_data(target,blob)
#save weights every 10 cycles
if counter % 10 == 0:
print("Weights saved!")
save_weights()
except:
pass
if streak > top_streak:
top_streak=streak
fm.store_streak(top_streak)
return
##CLI##
#python3 scipio.py -alpha 0.000001 -epoch 5
#python3 scipio.py -alpha 0.00001 -time 0 -load weights.txt -conf 19
##GLOBAL VARS##
alpha=0.001
threshold=0.5
epochs=0
speed=0
confidence=20
email=''
perceptrons=[]
session=start_session()
alphabet=string.ascii_lowercase[:6]
label=['avr', 'alphaev56', 'arm', 'm68k', 'mips', 'mipsel', 'powerpc', 's390', 'sh4', 'sparc', 'x86_64', 'xtensa']
base_url="https://mlb.praetorian.com/{}"
##PROGRAM START##
parser = argparse.ArgumentParser(description='Scipio Neural Net')
parser.add_argument('-a',action='store', dest='alpha',help='alpha perceptron learning rate | value b/w 0 and 1')
parser.add_argument('-thresh',action='store', dest='threshold',help='perceptron threshold value | default 0.5')
parser.add_argument('-e',action='store', dest='epochs',help='number of epochs to train > 0')
parser.add_argument('-l',action='store', dest='load',help='file name to load neural net weights')
parser.add_argument('-t',action='store', dest='speed',help='amount of time to pause between requesting and solving a challenge')
parser.add_argument('-c',action='store', dest='confidence',help='amount of confidence scipio needs to be sure of it\'s answer. default: 6')
args = parser.parse_args()
print_title()
[email,token]=get_user()
token = json.loads(token)
print("\nUSER DETAILS")
print("\tEmail:\t"+email.strip("\n"))
print("\tAuth:\t"+str(token['Authorization'])[:60]+"...")
print("\n~Neural Net Configuration~")
if(args.alpha==None):
print("alpha:\t0.1\t[default]")
elif(float(args.alpha)<=0 or float(args.alpha)>1):
print("alpha:\t0.1\t[default][invalid]")
elif(float(args.alpha)>0 or float(args.alpha)<1):
print("alpha:\t"+str(args.alpha)+"\t[user]")
alpha=float(args.alpha)
else:
print("alpha:\t0.1\t[default][invalid]")
if(args.threshold==None):
print("thresh:\t0.5\t[default]")
elif(float(args.threshold)<=0 or float(args.threshold)>1):
print("thresh:\t0.5\t[default][invalid]")
elif(float(args.threshold)>0 or float(args.threshold)<1):
print("thresh:\t"+str(args.threshold)+"\t[user]")
threshold=float(args.threshold)
else:
print("thresh:\t0.5\t[default][invalid]")
if(args.speed==None):
speed=0.1
print("time:\t"+str(speed)+"\t[default]")
else:
speed=float(args.speed)
print("time:\t"+str(speed)+"\t[user]")
if(args.epochs==None):
print("epochs:\tnone\t[default]")
training=False
else:
epochs=int(args.epochs)
print("epochs:\t"+str(epochs)+"\t[user]")
training=True
if(args.confidence!=None):
confidence=int(args.confidence)
print('conf:\t'+str(confidence)+"\t[user]")
else:
print('conf:\t'+str(confidence)+"\t[default]")
perceptron.init_percepts(perceptrons,alpha,label,threshold)
if(args.load!=None):
load_weights(str(args.load))
if(training==True):
train()
else:
execute()