diff --git a/mobsf/StaticAnalyzer/views/android/db_interaction.py b/mobsf/StaticAnalyzer/views/android/db_interaction.py index 08be0810..3015dffb 100755 --- a/mobsf/StaticAnalyzer/views/android/db_interaction.py +++ b/mobsf/StaticAnalyzer/views/android/db_interaction.py @@ -13,6 +13,9 @@ from mobsf.MobSF.utils import ( from mobsf.MobSF.views.home import update_scan_timestamp from mobsf.StaticAnalyzer.models import StaticAnalyzerAndroid from mobsf.StaticAnalyzer.models import RecentScansDB +from mobsf.StaticAnalyzer.views.common.secret_detection import ( + sort_secrets, +) from mobsf.StaticAnalyzer.views.common.suppression import ( process_suppression, process_suppression_manifest, @@ -86,7 +89,8 @@ def get_context_from_db_entry(db_entry: QuerySet) -> dict: 'behaviour': python_dict(db_entry[0].QUARK), 'trackers': python_dict(db_entry[0].TRACKERS), 'playstore_details': python_dict(db_entry[0].PLAYSTORE_DETAILS), - 'secrets': python_list(db_entry[0].SECRETS), + 'secrets': sort_secrets( + python_list(db_entry[0].SECRETS)), 'logs': get_scan_logs(db_entry[0].MD5), 'sbom': python_dict(db_entry[0].SBOM), } @@ -161,7 +165,7 @@ def get_context_from_analysis(app_dic, 'behaviour': code_an_dic['behaviour'], 'trackers': trackers, 'playstore_details': app_dic['playstore'], - 'secrets': code_an_dic['secrets'], + 'secrets': sort_secrets(code_an_dic['secrets']), 'logs': get_scan_logs(app_dic['md5']), 'sbom': code_an_dic['sbom'], } diff --git a/mobsf/StaticAnalyzer/views/android/strings.py b/mobsf/StaticAnalyzer/views/android/strings.py index 4132efd9..41d12ea1 100755 --- a/mobsf/StaticAnalyzer/views/android/strings.py +++ b/mobsf/StaticAnalyzer/views/android/strings.py @@ -8,8 +8,9 @@ from mobsf.StaticAnalyzer.views.common.shared_func import ( strings_and_entropies, url_n_email_extract, ) -from mobsf.StaticAnalyzer.views.common.entropy import ( - get_entropies, +from mobsf.StaticAnalyzer.views.common.secret_detection import ( + detect_known_secrets, + get_secrets, ) from mobsf.MobSF.utils import ( GOOGLE_API_KEY_REGEX, @@ -37,7 +38,7 @@ def strings_from_so(checksum, elf_strings): so_urls, so_urls_nf, so_emails_nf = url_n_email_extract( so_str, so) sos.append({so: { - 'secrets': list(get_entropies(so_str)), + 'secrets': list(get_secrets(str_list)), 'strings': list(set(str_list)), 'urls_list': so_urls, 'urls_nf': so_urls_nf, @@ -93,9 +94,12 @@ def strings_from_apk(checksum, app_dic): # Check for possible secrets if is_secret_key(key) and ' ' not in value: results['secrets'].append(formatted_str) + results['secrets'].extend( + detect_known_secrets(value)) elif app_dic.get('apk_strings'): - # No secret key check for APK strings results['strings'] = app_dic['apk_strings'] + results['secrets'].extend( + detect_known_secrets(results['strings'])) else: msg = 'Failed to extract String data from APK' logger.warning(msg) diff --git a/mobsf/StaticAnalyzer/views/common/secret_detection.py b/mobsf/StaticAnalyzer/views/common/secret_detection.py new file mode 100644 index 00000000..ef1c9b3a --- /dev/null +++ b/mobsf/StaticAnalyzer/views/common/secret_detection.py @@ -0,0 +1,222 @@ +"""Known secret pattern detection.""" +import re + +from mobsf.StaticAnalyzer.views.common.entropy import ( + get_entropies, +) + +KNOWN_SECRET_PATTERNS = ( + ( + 'GitHub token', + re.compile( + r'(?' in line and is_secret_key(_remove_tags(line)): diff --git a/mobsf/StaticAnalyzer/views/ios/strings.py b/mobsf/StaticAnalyzer/views/ios/strings.py index dc05c9df..ea20d35a 100644 --- a/mobsf/StaticAnalyzer/views/ios/strings.py +++ b/mobsf/StaticAnalyzer/views/ios/strings.py @@ -3,8 +3,8 @@ import io import logging -from mobsf.StaticAnalyzer.views.common.entropy import ( - get_entropies, +from mobsf.StaticAnalyzer.views.common.secret_detection import ( + get_secrets, ) from mobsf.StaticAnalyzer.views.common.shared_func import ( url_n_email_extract, @@ -73,7 +73,7 @@ def get_strings_metadata(app_dict, bin_dict, all_files, dy_list): urls_list = [] urls_n_files = [] emails_n_files = [] - secrets = [] + secrets = list(get_secrets(bin_dict['strings'])) # IPA URL and Email Extract str_meta = extract_urls_n_email( @@ -97,7 +97,7 @@ def get_strings_metadata(app_dict, bin_dict, all_files, dy_list): urls_list.extend(urls) urls_n_files.extend(urls_nf) emails_n_files.extend(emails_nf) - secrets.extend(get_entropies(dy_str)) + secrets.extend(get_secrets(s)) bin_dict['strings'].extend(dy_strings) bin_dict['strings'] = list(