|
| 1 | +import subprocess |
| 2 | +import os |
| 3 | +import re |
| 4 | +import sys |
| 5 | + |
| 6 | +# HACK: This is not ideal, but the project is structured as a collection of |
| 7 | +# top-level scripts and not as a single installable package. This allows us |
| 8 | +# to import modules from sibling directories. |
| 9 | +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) |
| 10 | + |
| 11 | +from sensitive_data_scanner.scanner import scan_directory as scan_for_secrets |
| 12 | +from social_media_analyzer.scam_detector import analyze_text_for_scams |
| 13 | + |
| 14 | + |
| 15 | +# Based on Android documentation and security best practices. |
| 16 | +# These permissions grant access to sensitive user data or system control. |
| 17 | +HIGH_RISK_PERMISSIONS = [ |
| 18 | + "android.permission.READ_CALENDAR", |
| 19 | + "android.permission.WRITE_CALENDAR", |
| 20 | + "android.permission.CAMERA", |
| 21 | + "android.permission.READ_CONTACTS", |
| 22 | + "android.permission.WRITE_CONTACTS", |
| 23 | + "android.permission.GET_ACCOUNTS", |
| 24 | + "android.permission.ACCESS_FINE_LOCATION", |
| 25 | + "android.permission.ACCESS_COARSE_LOCATION", |
| 26 | + "android.permission.RECORD_AUDIO", |
| 27 | + "android.permission.READ_PHONE_STATE", |
| 28 | + "android.permission.READ_PHONE_NUMBERS", |
| 29 | + "android.permission.CALL_PHONE", |
| 30 | + "android.permission.ANSWER_PHONE_CALLS", |
| 31 | + "android.permission.ADD_VOICEMAIL", |
| 32 | + "android.permission.USE_SIP", |
| 33 | + "android.permission.PROCESS_OUTGOING_CALLS", |
| 34 | + "android.permission.READ_CALL_LOG", |
| 35 | + "android.permission.WRITE_CALL_LOG", |
| 36 | + "com.android.voicemail.permission.ADD_VOICEMAIL", |
| 37 | + "android.permission.BODY_SENSORS", |
| 38 | + "android.permission.SEND_SMS", |
| 39 | + "android.permission.RECEIVE_SMS", |
| 40 | + "android.permission.READ_SMS", |
| 41 | + "android.permission.RECEIVE_WAP_PUSH", |
| 42 | + "android.permission.RECEIVE_MMS", |
| 43 | + "android.permission.READ_EXTERNAL_STORAGE", |
| 44 | + "android.permission.WRITE_EXTERNAL_STORAGE", |
| 45 | + "android.permission.SYSTEM_ALERT_WINDOW", |
| 46 | + "android.permission.WRITE_SETTINGS", |
| 47 | + "android.permission.REQUEST_INSTALL_PACKAGES", |
| 48 | + "android.permission.ACCESS_BACKGROUND_LOCATION", |
| 49 | +] |
| 50 | + |
| 51 | +def decompile_apk(apk_path, output_dir): |
| 52 | + """ |
| 53 | + Decompiles an APK file using apktool. |
| 54 | +
|
| 55 | + Args: |
| 56 | + apk_path (str): The path to the APK file. |
| 57 | + output_dir (str): The directory to store the decompiled code. |
| 58 | +
|
| 59 | + Returns: |
| 60 | + bool: True if decompilation was successful, False otherwise. |
| 61 | + """ |
| 62 | + if not os.path.exists(apk_path): |
| 63 | + print(f"Error: APK file not found at {apk_path}") |
| 64 | + return False |
| 65 | + |
| 66 | + print(f"Decompiling {apk_path} to {output_dir}...") |
| 67 | + try: |
| 68 | + # Using -f to force overwrite the output directory if it exists |
| 69 | + command = ["apktool", "d", "-f", apk_path, "-o", output_dir] |
| 70 | + result = subprocess.run(command, capture_output=True, text=True, check=True) |
| 71 | + print("Decompilation successful.") |
| 72 | + return True |
| 73 | + except subprocess.CalledProcessError as e: |
| 74 | + print("Error during decompilation:") |
| 75 | + print(e.stderr) |
| 76 | + return False |
| 77 | + except FileNotFoundError: |
| 78 | + print("Error: 'apktool' not found. Make sure it is installed and in your PATH.") |
| 79 | + return False |
| 80 | + |
| 81 | +def get_permissions(apk_path): |
| 82 | + """ |
| 83 | + Extracts permissions from an APK's AndroidManifest.xml using aapt. |
| 84 | +
|
| 85 | + Args: |
| 86 | + apk_path (str): The path to the APK file. |
| 87 | +
|
| 88 | + Returns: |
| 89 | + list: A list of permissions found in the APK. |
| 90 | + """ |
| 91 | + if not os.path.exists(apk_path): |
| 92 | + print(f"Error: APK file not found at {apk_path}") |
| 93 | + return [] |
| 94 | + |
| 95 | + print(f"Extracting permissions from {apk_path}...") |
| 96 | + try: |
| 97 | + command = ["aapt", "dump", "permissions", apk_path] |
| 98 | + result = subprocess.run(command, capture_output=True, text=True, check=True) |
| 99 | + |
| 100 | + # Regex to find package permissions |
| 101 | + permissions = re.findall(r"uses-permission: name='([^']*)'", result.stdout) |
| 102 | + print(f"Found {len(permissions)} permissions.") |
| 103 | + return permissions |
| 104 | + except subprocess.CalledProcessError as e: |
| 105 | + print("Error extracting permissions:") |
| 106 | + print(e.stderr) |
| 107 | + return [] |
| 108 | + except FileNotFoundError: |
| 109 | + print("Error: 'aapt' not found. Make sure it is installed and in your PATH.") |
| 110 | + return [] |
| 111 | + |
| 112 | +def check_high_risk_permissions(permissions): |
| 113 | + """ |
| 114 | + Checks a list of permissions against the high-risk permissions list. |
| 115 | +
|
| 116 | + Args: |
| 117 | + permissions (list): The list of permissions from an APK. |
| 118 | +
|
| 119 | + Returns: |
| 120 | + list: A list of high-risk permissions found in the APK. |
| 121 | + """ |
| 122 | + found_high_risk = [] |
| 123 | + for perm in permissions: |
| 124 | + if perm in HIGH_RISK_PERMISSIONS: |
| 125 | + found_high_risk.append(perm) |
| 126 | + |
| 127 | + print(f"Found {len(found_high_risk)} high-risk permissions.") |
| 128 | + return found_high_risk |
| 129 | + |
| 130 | + |
| 131 | +def scan_for_sensitive_data(decompiled_dir): |
| 132 | + """ |
| 133 | + Scans a directory for sensitive data using the sensitive_data_scanner module. |
| 134 | +
|
| 135 | + Args: |
| 136 | + decompiled_dir (str): The path to the directory of decompiled code. |
| 137 | +
|
| 138 | + Returns: |
| 139 | + dict: A dictionary of findings. |
| 140 | + """ |
| 141 | + print("\nScanning for sensitive data...") |
| 142 | + findings = scan_for_secrets(decompiled_dir) |
| 143 | + if findings: |
| 144 | + print(f"Found sensitive data in {len(findings)} files.") |
| 145 | + else: |
| 146 | + print("No sensitive data found.") |
| 147 | + return findings |
| 148 | + |
| 149 | +def scan_for_scam_indicators(decompiled_dir): |
| 150 | + """ |
| 151 | + Scans files in a directory for scam indicators using the scam_detector module. |
| 152 | +
|
| 153 | + Args: |
| 154 | + decompiled_dir (str): The path to the directory of decompiled code. |
| 155 | +
|
| 156 | + Returns: |
| 157 | + dict: A dictionary of findings, where keys are file paths. |
| 158 | + """ |
| 159 | + print("\nScanning for scam indicators (phishing, suspicious URLs)...") |
| 160 | + all_findings = {} |
| 161 | + |
| 162 | + # Extensions of text-like files to scan. |
| 163 | + # Smali, xml, and yml are common in decompiled APKs. |
| 164 | + scan_extensions = {'.smali', '.xml', '.yml', '.yaml', '.json', '.html', '.js', '.txt'} |
| 165 | + |
| 166 | + for root, _, files in os.walk(decompiled_dir): |
| 167 | + for filename in files: |
| 168 | + # Check if the file has one of the scannable extensions |
| 169 | + if not any(filename.endswith(ext) for ext in scan_extensions): |
| 170 | + continue |
| 171 | + |
| 172 | + filepath = os.path.join(root, filename) |
| 173 | + try: |
| 174 | + with open(filepath, 'r', encoding='utf-8', errors='ignore') as f: |
| 175 | + content = f.read() |
| 176 | + # Skip very large files to avoid performance issues |
| 177 | + if len(content) > 1000000: # 1MB limit |
| 178 | + continue |
| 179 | + |
| 180 | + analysis_result = analyze_text_for_scams(content) |
| 181 | + if analysis_result.get("indicators_found"): |
| 182 | + all_findings[filepath] = analysis_result |
| 183 | + except Exception: |
| 184 | + # Ignore files that can't be read for any reason |
| 185 | + continue |
| 186 | + |
| 187 | + if all_findings: |
| 188 | + print(f"Found scam indicators in {len(all_findings)} files.") |
| 189 | + else: |
| 190 | + print("No scam indicators found.") |
| 191 | + |
| 192 | + return all_findings |
0 commit comments