forked from CptSaucisson/USBerry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
launch-scan.py
235 lines (167 loc) · 5.66 KB
/
launch-scan.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
#!/usr/bin/python
import requests
import json
import os
import glob
import pyudev
import sys
import time
import subprocess
import logging
mypath = os.path.dirname(os.path.realpath(__file__))
irma_brain = "172.16.1.30"
api = "http://"+irma_brain+"/api/v1.1/"
test_format_file = mypath+"/formats.txt"
listOfTest = list()
with open(test_format_file) as f:
for l in f.readlines():
tmp = l[:-1]
listOfTest.append(tmp.lower())
listOfTest.append(tmp.upper())
#logging.debug(listOfTest)
logging.basicConfig(filename='/var/log/usberry.log',level=logging.DEBUG)
class scanner:
def __init__(self, rootPath):
self.files = list() # files : list( [path, result] )
self.root = rootPath
self.api = API(api)
self.nFilesToScan = 0
def shouldBeScanned(self, file):
filename = file.rsplit('/')[-1]
fileformat = file.rsplit('.')[-1]
logging.debug(filename+" ==> "+fileformat)
logging.debug(listOfTest)
#if no extension
if(fileformat == filename):
return True
if(fileformat in listOfTest):
return True
return False
def getFiles(self):
files = [file for file in glob.glob(self.root + '/**/*', recursive=True)if not os.path.isdir(file)]
logging.info(files)
for f in files:
if(self.shouldBeScanned(f)):
self.nFilesToScan = self.nFilesToScan + 1
self.files.append((f,None))
def scanFiles(self):
self.scan = self.api.createScan()
for f in self.files:
logging.debug("FILE : "+str(f[0]))
self.api.uploadFiles(f[0],self.scan)
self.api.startScan(self.scan)
def getResult(self):
pass
class API:
def __init__(self, api):
self.url = api
def sendRequest(self, url, request, payload, files=None, json=None):
logging.info("______________________")
logging.info("Request : "+str(request)+" "+str(url))
logging.info("payload :"+str(payload))
if(request == "GET"):
r = requests.get(self.url+url, payload, files=files)
else:
r = requests.post(self.url+url, data=payload, files=files, json=json)
logging.info("SendRequest : "+str(r.status_code))
if(r.status_code != 200):
logging.info("ERROR !")
logging.info(r.status_code)
logging.info(r.json())
return (r.status_code, r.json(), r.headers)
def createScan(self):
r = self.sendRequest("scans", "POST", None)
if(r[0] == 200):
res = r[1]
#logging.info(res['id'])
return res['id']
return r
def uploadFiles(self, files, scan):
r = self.sendRequest("scans/"+scan+"/files", "POST", {'scanId':scan}, files={'file':open(files, 'rb')})
if(r[0] == 200):
logging.info("file added : "+str(r[1]))
def startScan(self, scan):
payload = {'scanId':scan, 'options':{'force':True, 'mimetype_filtering':True,'resubmit_files':True}}
#payload = {'force':True, 'mimetype_filtering':True,'resubmit_files':True}
pay = json.dumps(payload)
logging.info(pay)
r = self.sendRequest("scans/"+scan+"/launch", "POST", {}, json=payload )
if(r[0] == 200):
logging.info("SCAN LAUNCHED")
logging.debug(r)
logging.info("GO TO : http://"+irma_brain+"/scan/"+r[1]["id"]+"/")
def getAllProbes(self):
return "all"
r = self.sendRequest("probes", "GET", {})
if(r[0] == 200):
result = r[1]["data"]
return result
return "[]"
#
########################################################################################
#
def write(p):
with open("/var/log/usberry.log","a+") as f:
f.write(p)
def getDevName(kernelId):
context = pyudev.Context()
usb = None
for device in context.list_devices(subsystem='block'):
try:
#logging.info(">"+str(device.device_node)+" <==> "+str(device.device_path))
if(sys.argv[1] in device.device_path):
logging.info(device)
usb = device
break
except Exception as e:
logging.info(e)
path = usb.device_node
return path
def mountFS(path):
logging.info("Mounting file system")
errors = False
try:
res_mkdir = subprocess.check_call(["mkdir","/mnt/usberry"])
except Exception as e:
logging.error(e)
errors = True
try:
res_mount = subprocess.check_call(["mount", path, "/mnt/usberry"])
if(res_mount != 0):
logging.error("ERREUR mounting")
sys.exit(1)
except Exception as e:
logging.error(e)
errors = True
if(not errors):
logging.info("System mounted")
else:
logging.error("FAILED MOUNTING !")
return errors
def startScan(path):
logging.info("STARTING SCAAAAAAAAAN !!!")
s = scanner(path)
s.getFiles()
if(s.nFilesToScan > 0):
s.scanFiles()
else:
logging.info("NO FILES TO SCAN !")
if __name__ == '__main__':
if(len(sys.argv) != 2):
logging.info("NEED Mounted Kernel")
sys.exit(0)
time.sleep(5)
logging.info("starting with kernel :"+sys.argv[1]+"\n")
path = getDevName(sys.argv[1])
logging.info("path = "+path)
res = False
try:
res = mountFS(path)
except Exception as e:
logging.error(e)
res = True
if(res):sys.exit(1)
try:
startScan("/mnt/usberry")
except Exception as e:
logging.error(e)