def start_dm(md5_hash, package, launcher): # Start DM #stop_capfuzz(settings.PORT) toolsdir = os.path.join(settings.BASE_DIR, 'DynamicAnalyzer/tools/') # TOOLS DIR adb = getADB(toolsdir) if settings.ANDROID_DYNAMIC_ANALYZER == "MobSF_REAL_DEVICE": print( "\n[INFO] MobSF will perform Dynamic Analysis on real Android Device" ) is_avd = False elif settings.ANDROID_DYNAMIC_ANALYZER == "MobSF_AVD": # adb, avd_path, reference_name, dup_name, emulator is_avd = True refresh_avd(adb, settings.AVD_PATH, settings.AVD_REFERENCE_NAME, settings.AVD_DUP_NAME, settings.AVD_EMULATOR) else: # Refersh VM is_avd = False refresh_vm(settings.UUID, settings.SUUID, settings.VBOX) context = { 'md5': md5_hash, 'pkg': package, 'lng': launcher, 'title': 'Start Testing', 'AVD': is_avd, } return context
def android_dynamic_analyzer(request): """Android Dynamic Analyzer View""" print("\n[INFO] Dynamic Analysis Started") try: if request.method == 'POST': md5_hash = request.POST['md5'] package = request.POST['pkg'] launcher = request.POST['lng'] if re.findall(r';|\$\(|\|\||&&', package) or re.findall( r';|\$\(|\|\||&&', launcher): print("[ATTACK] Possible RCE") return HttpResponseRedirect('/error/') if re.match('^[0-9a-f]{32}$', md5_hash): # Delete ScreenCast Cache screen_file = os.path.join(settings.SCREEN_DIR, 'screen.png') if os.path.exists(screen_file): os.remove(screen_file) # Delete Contents of Screenshot Dir screen_dir = os.path.join(settings.UPLD_DIR, md5_hash + '/screenshots-apk/') if os.path.isdir(screen_dir): shutil.rmtree(screen_dir) else: os.makedirs(screen_dir) # Start DM stop_capfuzz(settings.PORT) toolsdir = os.path.join(settings.BASE_DIR, 'DynamicAnalyzer/tools/') # TOOLS DIR adb = getADB(toolsdir) if settings.ANDROID_DYNAMIC_ANALYZER == "MobSF_REAL_DEVICE": print( "\n[INFO] MobSF will perform Dynamic Analysis on real Android Device" ) is_avd = False elif settings.ANDROID_DYNAMIC_ANALYZER == "MobSF_AVD": # adb, avd_path, reference_name, dup_name, emulator is_avd = True refresh_avd(adb, settings.AVD_PATH, settings.AVD_REFERENCE_NAME, settings.AVD_DUP_NAME, settings.AVD_EMULATOR) else: # Refersh VM is_avd = False refresh_vm(settings.UUID, settings.SUUID, settings.VBOX) context = { 'md5': md5_hash, 'pkg': package, 'lng': launcher, 'title': 'Start Testing', 'AVD': is_avd, } template = "dynamic_analysis/start_test.html" return render(request, template, context) else: return HttpResponseRedirect('/error/') else: return HttpResponseRedirect('/error/') except: PrintException("[ERROR] DynamicAnalyzer") return HttpResponseRedirect('/error/')
def yield_dynamic_run(apk_path): try: #step1: app_info app_info = get_base_info(apk_path) #step2 init virtual android devices by virtualbox refresh_vm(SETTINGS.UUID, SETTINGS.SUUID, SETTINGS.VBOX) connect(DYNAMIC_TOOL_DIR) #step3 install, run install_and_run(DYNAMIC_TOOL_DIR, app_info['apk_path'], app_info['packagename'], app_info['main_activity'], True) #step4 auto_app_test and start download x_logcat.txt #auto_app_test in another thread, download in main thread set_run_state(True) adb = getADB(DYNAMIC_TOOL_DIR) monkey = threading.Thread(target=monkey_test, args=(adb, app_info)) monkey.start() for cnt in yield_droidmon_download(adb, app_info['detail_dir'], app_info['packagename']): yield cnt if not check_run_state(): break monkey.join() except Exception as e: result = {} traceback.print_exc()
def android_dynamic_analyzer(request): """Android Dynamic Analyzer View""" print("\n[INFO] Dynamic Analysis Started") try: if request.method == 'POST': md5_hash = request.POST['md5'] package = request.POST['pkg'] launcher = request.POST['lng'] if re.findall(r';|\$\(|\|\||&&', package) or re.findall(r';|\$\(|\|\||&&', launcher): print("[ATTACK] Possible RCE") return HttpResponseRedirect('/error/') if re.match('^[0-9a-f]{32}$', md5_hash): # Delete ScreenCast Cache screen_file = os.path.join(settings.SCREEN_DIR, 'screen.png') if os.path.exists(screen_file): os.remove(screen_file) # Delete Contents of Screenshot Dir screen_dir = os.path.join( settings.UPLD_DIR, md5_hash + '/screenshots-apk/') if os.path.isdir(screen_dir): shutil.rmtree(screen_dir) else: os.makedirs(screen_dir) # Start DM stop_capfuzz(settings.PORT) adb = getADB() is_avd = False if settings.ANDROID_DYNAMIC_ANALYZER == "MobSF_REAL_DEVICE": print( "\n[INFO] MobSF will perform Dynamic Analysis on real Android Device") elif settings.ANDROID_DYNAMIC_ANALYZER == "MobSF_AVD": # adb, avd_path, reference_name, dup_name, emulator is_avd = True if not os.path.exists(settings.AVD_EMULATOR): return HttpResponseRedirect('/error/') if not refresh_avd(): return HttpResponseRedirect('/error/') else: # Refersh VM refresh_vm(settings.UUID, settings.SUUID, settings.VBOX) context = {'md5': md5_hash, 'pkg': package, 'lng': launcher, 'title': 'Start Testing', 'AVD': is_avd, } template = "dynamic_analysis/start_test.html" return render(request, template, context) else: return HttpResponseRedirect('/error/') else: return HttpResponseRedirect('/error/') except: PrintException("[ERROR] DynamicAnalyzer") return HttpResponseRedirect('/error/')
def init_environment(adb): #Proxy('', '', '', '') if SETTINGS.ANDROID_DYNAMIC_ANALYZER == "MobSF_REAL_DEVICE": print( "\n[INFO] MobSF will perform Dynamic Analysis on real Android Device" ) elif SETTINGS.ANDROID_DYNAMIC_ANALYZER == "MobSF_AVD": # adb, avd_path, reference_name, dup_name, emulator refresh_avd(adb, SETTINGS.AVD_PATH, SETTINGS.AVD_REFERENCE_NAME, SETTINGS.AVD_DUP_NAME, SETTINGS.AVD_EMULATOR) else: # Refersh VM refresh_vm(SETTINGS.UUID, SETTINGS.SUUID, SETTINGS.VBOX) return
def android_dynamic_analyzer(request): """Android Dynamic Analyzer View.""" logger.info('Dynamic Analysis Started') try: if request.method == 'POST': md5_hash = request.POST['md5'] package = request.POST['pkg'] launcher = request.POST['lng'] if (re.findall(r';|\$\(|\|\||&&', package) or re.findall(r';|\$\(|\|\||&&', launcher)): return print_n_send_error_response(request, 'Possible RCE Attack') if re.match('^[0-9a-f]{32}$', md5_hash): # Delete ScreenCast Cache screen_file = os.path.join(settings.SCREEN_DIR, 'screen.png') if os.path.exists(screen_file): os.remove(screen_file) # Delete Contents of Screenshot Dir screen_dir = os.path.join( settings.UPLD_DIR, md5_hash + '/screenshots-apk/') if os.path.isdir(screen_dir): shutil.rmtree(screen_dir) else: os.makedirs(screen_dir) # Start DM stop_capfuzz(settings.PORT) if settings.ANDROID_DYNAMIC_ANALYZER == 'MobSF_REAL_DEVICE': logger.info( 'MobSF Dynamic Analysis on Real Android Device') else: # Refersh VM refresh_vm(settings.UUID, settings.SUUID, settings.VBOX) context = {'md5': md5_hash, 'pkg': package, 'lng': launcher, 'title': 'Start Testing'} template = 'dynamic_analysis/start_test.html' return render(request, template, context) else: return print_n_send_error_response(request, 'Invalid Scan Hash') else: return print_n_send_error_response(request, 'Only POST allowed') except Exception: logger.exception('DynamicAnalyzer') return print_n_send_error_response(request, 'Dynamic Analysis Failed.')
def dynamic_run(apk_path): start_time = time.time() try: #step1: app_info app_info = get_base_info(apk_path) #step2 init virtual android devices by virtualbox refresh_vm(SETTINGS.UUID, SETTINGS.SUUID, SETTINGS.VBOX) connect(DYNAMIC_TOOL_DIR) #step3 install, run, map run error try: install_and_run(DYNAMIC_TOOL_DIR, app_info['apk_path'], app_info['packagename'], app_info['main_activity'], True) except Exception as e: print("[dynamic run], exception at install_and_run:", e, 'app_info: ', app_info) #step4 auto_app_test and start download x_logcat.txt #auto_app_test in another thread, download in main thread set_run_state(True) adb = getADB(DYNAMIC_TOOL_DIR) monkey = threading.Thread(target=monkey_test, args=(adb, app_info)) monkey.start() droidmon_download(adb, app_info['detail_dir'], app_info['packagename']) monkey.join() #step5 power off poweroff_vm(SETTINGS.UUID, SETTINGS.VBOX) return time.time() - start_time except Exception as e: result = {} traceback.print_exc() return 0
def android_dynamic_analyzer(request): """Android Dynamic Analyzer View""" logger.info("Dynamic Analysis Started") try: if request.method == 'POST': md5_hash = request.POST['md5'] package = request.POST['pkg'] launcher = request.POST['lng'] if re.findall(r';|\$\(|\|\||&&', package) or re.findall( r';|\$\(|\|\||&&', launcher): return print_n_send_error_response(request, "Possible RCE Attack") if re.match('^[0-9a-f]{32}$', md5_hash): # Delete ScreenCast Cache screen_file = os.path.join(settings.SCREEN_DIR, 'screen.png') if os.path.exists(screen_file): os.remove(screen_file) # Delete Contents of Screenshot Dir screen_dir = os.path.join(settings.UPLD_DIR, md5_hash + '/screenshots-apk/') if os.path.isdir(screen_dir): shutil.rmtree(screen_dir) else: os.makedirs(screen_dir) # Start DM stop_capfuzz(settings.PORT) adb = getADB() is_avd = False if settings.ANDROID_DYNAMIC_ANALYZER == "MobSF_REAL_DEVICE": logger.info( "MobSF will perform Dynamic Analysis on real Android Device" ) elif settings.ANDROID_DYNAMIC_ANALYZER == "MobSF_AVD": # adb, avd_path, reference_name, dup_name, emulator is_avd = True if not os.path.exists(settings.AVD_EMULATOR): return print_n_send_error_response( request, "Cannot Find AVD Emulator") if not refresh_avd(): return print_n_send_error_response( request, "Cannot Refresh AVD") else: # Refersh VM refresh_vm(settings.UUID, settings.SUUID, settings.VBOX) context = { 'md5': md5_hash, 'pkg': package, 'lng': launcher, 'title': 'Start Testing', 'AVD': is_avd, } template = "dynamic_analysis/start_test.html" return render(request, template, context) else: return print_n_send_error_response(request, "Invalid Scan Hash") else: return print_n_send_error_response(request, "Only POST allowed") except: PrintException("DynamicAnalyzer") return print_n_send_error_response(request, "Dynamic Analysis Failed.")