11import logging
22from tempfile import NamedTemporaryFile
3+ import requests
4+ import hashlib
35
46from androguard .core .androconf import is_android
57from django .conf import settings
2830from bazaar .core .models import Yara
2931from bazaar .core .tasks import analyze , retrohunt
3032from bazaar .core .utils import get_sha256_of_file , get_matching_items_by_dexofuzzy
31- from bazaar .front .forms import SearchForm , BasicUploadForm , SimilaritySearchForm
33+ from bazaar .front .forms import SearchForm , BasicUploadForm , SimilaritySearchForm , BasicUrlDownloadForm
3234from bazaar .front .og import generate_og_card
3335from bazaar .front .utils import transform_results , get_similarity_matrix , compute_status , generate_world_map , \
3436 transform_hl_results , get_sample_timeline , get_andro_cfg_storage_path
@@ -150,6 +152,37 @@ def get(self, request, *args, **kwargs):
150152 return redirect (reverse_lazy ('front:home' ))
151153
152154
155+ def basic_url_download_view (request ):
156+ if request .method == 'POST' :
157+ form = BasicUrlDownloadForm (request .POST )
158+ if form .is_valid ():
159+ url = form .cleaned_data .get ('url' )
160+ res = requests .get (url , stream = True )
161+
162+ if res .status_code not in [200 , 301 , 302 ]:
163+ messages .warning (request , 'URL is not available.' )
164+ return redirect (reverse_lazy ('front:home' ))
165+
166+ sha256_hash = hashlib .sha256 ()
167+ with NamedTemporaryFile () as tmp :
168+ for chunk in res .iter_content (chunk_size = 16 * 1024 ):
169+ tmp .write (chunk )
170+ sha256_hash .update (chunk )
171+
172+ sha256 = str (sha256_hash .hexdigest ()).lower ()
173+ if is_android (tmp .name ) != 'APK' :
174+ messages .warning (request , 'Submitted file is not a valid APK.' )
175+
176+ if default_storage .exists (sha256 ):
177+ return redirect (reverse_lazy ('front:report' , [sha256 ]))
178+ else :
179+ default_storage .save (sha256 , tmp )
180+ analyze (sha256 )
181+ return redirect (reverse_lazy ('front:report' , [sha256 ]))
182+
183+ return redirect (reverse_lazy ('front:home' ))
184+
185+
153186def basic_upload_view (request ):
154187 if request .method == 'POST' :
155188 form = BasicUploadForm (request .POST , request .FILES )
@@ -414,15 +447,14 @@ def get_andgrocfg_code(request, sha256, foo):
414447 out = default_storage .open (f'{ storage_path } /{ foo } ' ).read ()
415448
416449 if f'{ storage_path } /{ foo } ' .endswith ('.raw' ):
417- out_formatted = highlight (out ,JavaLexer (), HtmlFormatter (style = U39bStyle , noclasses = True ))
450+ out_formatted = highlight (out , JavaLexer (), HtmlFormatter (style = U39bStyle , noclasses = True ))
418451 return HttpResponse (out_formatted , content_type = "text/html" )
419452 elif f'{ storage_path } /{ foo } ' .endswith ('.png' ):
420453 return HttpResponse (out , content_type = 'image/bmp' )
421454 else :
422455 return HttpResponse (out , content_type = "image/bmp" )
423456
424457
425-
426458def get_genom (request ):
427459 es = Elasticsearch (settings .ELASTICSEARCH_HOSTS )
428460 entire_genom = []
@@ -436,7 +468,8 @@ def get_genom(request):
436468 threat = 'unknown'
437469 try :
438470 genom = report .get ('_source' ).get ('andro_cfg' ).get ('genom' )
439- threat = report .get ('_source' ).get ('vt_report' ).get ('attributes' ).get ('popular_threat_classification' ).get ('suggested_threat_label' )
471+ threat = report .get ('_source' ).get ('vt_report' ).get ('attributes' ).get (
472+ 'popular_threat_classification' ).get ('suggested_threat_label' )
440473 except Exception :
441474 pass
442475 if genom :
0 commit comments