|
22 | 22 |
|
23 | 23 | from mobsf.MobSF.forms import FormUtil, UploadFileForm
|
24 | 24 | from mobsf.MobSF.utils import (
|
| 25 | + MD5_REGEX, |
25 | 26 | api_key,
|
26 | 27 | get_md5,
|
27 | 28 | is_dir_exists,
|
@@ -244,16 +245,6 @@ def zip_format(request):
|
244 | 245 | return render(request, template, context)
|
245 | 246 |
|
246 | 247 |
|
247 |
| -def not_found(request, *args): |
248 |
| - """Not Found Route.""" |
249 |
| - context = { |
250 |
| - 'title': 'Not Found', |
251 |
| - 'version': settings.MOBSF_VER, |
252 |
| - } |
253 |
| - template = 'general/not_found.html' |
254 |
| - return render(request, template, context) |
255 |
| - |
256 |
| - |
257 | 248 | @login_required
|
258 | 249 | def dynamic_analysis(request):
|
259 | 250 | """Dynamic Analysis Landing."""
|
@@ -337,18 +328,43 @@ def download_apk(request):
|
337 | 328 |
|
338 | 329 |
|
339 | 330 | @login_required
|
340 |
| -def search(request): |
341 |
| - """Search Scan by MD5 Route.""" |
342 |
| - md5 = request.GET['md5'] |
343 |
| - if re.match('[0-9a-f]{32}', md5): |
344 |
| - db_obj = RecentScansDB.objects.filter(MD5=md5) |
345 |
| - if db_obj.exists(): |
346 |
| - e = db_obj[0] |
347 |
| - url = f'/{e.ANALYZER}/{e.MD5}/' |
348 |
| - return HttpResponseRedirect(url) |
349 |
| - else: |
350 |
| - return HttpResponseRedirect('/not_found/') |
351 |
| - return print_n_send_error_response(request, 'Invalid Scan Hash') |
| 331 | +def search(request, api=False): |
| 332 | + """Search scan by checksum or text.""" |
| 333 | + if request.method == 'POST': |
| 334 | + query = request.POST['query'] |
| 335 | + else: |
| 336 | + query = request.GET['query'] |
| 337 | + |
| 338 | + if not query: |
| 339 | + msg = 'No search query provided.' |
| 340 | + return print_n_send_error_response(request, msg, api) |
| 341 | + |
| 342 | + checksum = query if re.match(MD5_REGEX, query) else find_checksum(query) |
| 343 | + |
| 344 | + if checksum and re.match(MD5_REGEX, checksum): |
| 345 | + db_obj = RecentScansDB.objects.filter(MD5=checksum).first() |
| 346 | + if db_obj: |
| 347 | + url = f'/{db_obj.ANALYZER}/{db_obj.MD5}/' |
| 348 | + if api: |
| 349 | + return {'checksum': db_obj.MD5} |
| 350 | + else: |
| 351 | + return HttpResponseRedirect(url) |
| 352 | + |
| 353 | + msg = 'You can search by MD5, app name, package name, or file name.' |
| 354 | + return print_n_send_error_response(request, msg, api, 'Scan not found') |
| 355 | + |
| 356 | + |
| 357 | +def find_checksum(query): |
| 358 | + """Get the first matching checksum from the database.""" |
| 359 | + search_fields = ['FILE_NAME', 'PACKAGE_NAME', 'APP_NAME'] |
| 360 | + |
| 361 | + for field in search_fields: |
| 362 | + result = RecentScansDB.objects.filter( |
| 363 | + **{f'{field}__icontains': query}).first() |
| 364 | + if result: |
| 365 | + return result.MD5 |
| 366 | + |
| 367 | + return None |
352 | 368 |
|
353 | 369 | # AJAX
|
354 | 370 |
|
@@ -453,7 +469,7 @@ def delete_scan(request, api=False):
|
453 | 469 | else:
|
454 | 470 | md5_hash = request.POST['md5']
|
455 | 471 | data = {'deleted': 'scan hash not found'}
|
456 |
| - if re.match('[0-9a-f]{32}', md5_hash): |
| 472 | + if re.match(MD5_REGEX, md5_hash): |
457 | 473 | # Delete DB Entries
|
458 | 474 | scan = RecentScansDB.objects.filter(MD5=md5_hash)
|
459 | 475 | if scan.exists():
|
@@ -485,10 +501,7 @@ def delete_scan(request, api=False):
|
485 | 501 | except Exception as exp:
|
486 | 502 | msg = str(exp)
|
487 | 503 | exp_doc = exp.__doc__
|
488 |
| - if api: |
489 |
| - return print_n_send_error_response(request, msg, True, exp_doc) |
490 |
| - else: |
491 |
| - return print_n_send_error_response(request, msg, False, exp_doc) |
| 504 | + return print_n_send_error_response(request, msg, api, exp_doc) |
492 | 505 |
|
493 | 506 |
|
494 | 507 | class RecentScans(object):
|
|
0 commit comments