def kensay(request): """Configure Instance for Dynamic Analysis.""" logger.info('Kensaying Android instance') data = {} try: identifier = request.POST['identifier'] create_env = Environment(identifier) if not create_env.connect_n_mount(): msg = 'Connection failed' data = {'status': 'failed', 'message': msg} return json_response(data) version = create_env.kensay_init() if not version: msg = 'Connection failed' data = {'status': 'failed', 'message': msg} return json_response(data) else: data = {'status': 'ok', 'version': version} except Exception as exp: logger.exception('Kensaying Android instance failed') data = {'status': 'failed', 'message': str(exp)} return json_response(data)
def dynamic_analyzer(request): """Android Dynamic Analyzer Environment.""" logger.info('Creating Dynamic Analysis Environment') try: bin_hash = request.GET['hash'] package = request.GET['package'] no_device = False if (is_attack_pattern(package) or not is_md5(bin_hash)): return print_n_send_error_response(request, 'Invalid Parameters') try: identifier = get_device() except Exception: no_device = True if no_device or not identifier: msg = ('Is the android instance running? Kensa cannot' ' find android instance identifier. ' 'Please run an android instance and refresh' ' this page. If this error persists,' ' set ANALYZER_IDENTIFIER in Kensa/settings.py') return print_n_send_error_response(request, msg) env = Environment(identifier) if not env.connect_n_mount(): msg = 'Cannot Connect to ' + identifier return print_n_send_error_response(request, msg) version = env.get_android_version() logger.info('Android Version identified as %s', version) xposed_first_run = False if not env.is_kensayied(version): msg = ('This Android instance is not Kensayed.\n' 'Kensaying the android runtime environment') logger.warning(msg) if not env.kensay_init(): return print_n_send_error_response( request, 'Failed to Kensay the instance') if version < 5: xposed_first_run = True if xposed_first_run: msg = ('Have you Kensayed the instance before' ' attempting Dynamic Analysis?' ' Install Framework for Xposed.' ' Restart the device and enable' ' all Xposed modules. And finally' ' restart the device once again.') return print_n_send_error_response(request, msg) # Clean up previous analysis env.dz_cleanup(bin_hash) # Configure Web Proxy env.configure_proxy(package) # Supported in Android 5+ env.enable_adb_reverse_tcp(version) # Apply Global Proxy to device env.set_global_proxy(version) # Start Clipboard monitor env.start_clipmon() # Get Screen Resolution screen_width, screen_height = env.get_screen_res() logger.info('Installing APK') app_dir = os.path.join(settings.UPLD_DIR, bin_hash + '/') # APP DIRECTORY apk_path = app_dir + bin_hash + '.apk' # APP PATH env.adb_command(['install', '-r', apk_path], False, True) logger.info('Testing Environment is Ready!') context = { 'screen_witdth': screen_width, 'screen_height': screen_height, 'package': package, 'md5': bin_hash, 'android_version': version, 'version': settings.KENSA_VER, 'title': 'Dynamic Analyzer' } template = 'dynamic_analysis/android/dynamic_analyzer.html' return render(request, template, context) except Exception: logger.exception('Dynamic Analyzer') return print_n_send_error_response(request, 'Dynamic Analysis Failed.')