|
| 1 | +# -*- coding: utf_8 -*- |
| 2 | +"""Use aapt2 to extract APK features.""" |
| 3 | +import re |
| 4 | +import logging |
| 5 | +import subprocess |
| 6 | +from platform import system |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +from django.conf import settings |
| 10 | + |
| 11 | +from mobsf.MobSF.utils import ( |
| 12 | + find_aapt, |
| 13 | +) |
| 14 | + |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +class AndroidAAPT: |
| 19 | + |
| 20 | + def __init__(self, apk_path): |
| 21 | + self.aapt2_path = None |
| 22 | + self.aapt_path = None |
| 23 | + self.apk_path = apk_path |
| 24 | + self.data = { |
| 25 | + 'permissions': [], |
| 26 | + 'uses_features': {}, |
| 27 | + 'package': None, |
| 28 | + 'application_label': None, |
| 29 | + 'application_icon': None, |
| 30 | + 'launchable_activity': None, |
| 31 | + 'min_sdk_version': None, |
| 32 | + 'target_sdk_version': None, |
| 33 | + } |
| 34 | + |
| 35 | + # Check for custom AAPT2 path in settings |
| 36 | + if (getattr(settings, 'AAPT2_BINARY', '') |
| 37 | + and len(settings.AAPT2_BINARY) > 0 |
| 38 | + and Path(settings.AAPT2_BINARY).exists()): |
| 39 | + self.aapt2_path = settings.AAPT2_BINARY |
| 40 | + else: |
| 41 | + aapt2 = 'aapt2.exe' if system() == 'Windows' else 'aapt2' |
| 42 | + self.aapt2_path = find_aapt(aapt2) |
| 43 | + |
| 44 | + # Check for custom AAPT path in settings |
| 45 | + if (getattr(settings, 'AAPT_BINARY', '') |
| 46 | + and len(settings.AAPT_BINARY) > 0 |
| 47 | + and Path(settings.AAPT_BINARY).exists()): |
| 48 | + self.aapt_path = settings.AAPT_BINARY |
| 49 | + else: |
| 50 | + aapt = 'aapt.exe' if system() == 'Windows' else 'aapt' |
| 51 | + self.aapt_path = find_aapt(aapt) |
| 52 | + |
| 53 | + # Ensure both aapt and aapt2 are found |
| 54 | + if not (self.aapt2_path and self.aapt_path): |
| 55 | + raise FileNotFoundError('aapt and aapt2 found') |
| 56 | + |
| 57 | + def _execute_command(self, args): |
| 58 | + try: |
| 59 | + out = subprocess.check_output( |
| 60 | + args, |
| 61 | + stderr=subprocess.STDOUT) |
| 62 | + return out.decode('utf-8', errors='ignore') |
| 63 | + except subprocess.CalledProcessError as e: |
| 64 | + logger.warning(e.output) |
| 65 | + return None |
| 66 | + |
| 67 | + def _get_strings(self, output): |
| 68 | + # Regex to match strings while ignoring paths (strings without slashes) |
| 69 | + pattern = r'String #[\d]+ : ([^\/\n]+)' |
| 70 | + matches = re.findall(pattern, output) |
| 71 | + # Strip whitespace and return the extracted strings |
| 72 | + return [match.strip() for match in matches] |
| 73 | + |
| 74 | + def _parse_badging(self, output): |
| 75 | + # Match the package information |
| 76 | + package_match = re.search(r'package: name=\'([\w\.]+)\'', output) |
| 77 | + if package_match: |
| 78 | + self.data['package'] = package_match.group(1) |
| 79 | + |
| 80 | + # Match permissions |
| 81 | + permissions = re.findall(r'uses-permission: name=\'([\w\.]+)\'', output) |
| 82 | + if permissions: |
| 83 | + self.data['permissions'] = permissions |
| 84 | + |
| 85 | + # Match minSdkVersion |
| 86 | + min_sdk_match = re.search(r'minSdkVersion:\'(\d+)\'', output) |
| 87 | + if min_sdk_match: |
| 88 | + self.data['min_sdk_version'] = min_sdk_match.group(1) |
| 89 | + |
| 90 | + # Match targetSdkVersion |
| 91 | + target_sdk_match = re.search(r'targetSdkVersion:\'(\d+)\'', output) |
| 92 | + if target_sdk_match: |
| 93 | + self.data['target_sdk_version'] = target_sdk_match.group(1) |
| 94 | + |
| 95 | + # Match application label |
| 96 | + label_match = re.search(r'application-label(?:-[\w\-]+)?:\'([^\']+)\'', output) |
| 97 | + if label_match: |
| 98 | + self.data['application_label'] = label_match.group(1) |
| 99 | + |
| 100 | + # Match application icon |
| 101 | + icon_match = re.search(r'application:.*icon=\'([^\']+)\'', output) |
| 102 | + if icon_match: |
| 103 | + self.data['application_icon'] = icon_match.group(1) |
| 104 | + |
| 105 | + # Match launchable activity |
| 106 | + activity_match = re.search(r'launchable-activity: name=\'([\w\.]+)\'', output) |
| 107 | + if activity_match: |
| 108 | + self.data['launchable_activity'] = activity_match.group(1) |
| 109 | + |
| 110 | + # Match used features |
| 111 | + features = {} |
| 112 | + feature_matches = re.findall( |
| 113 | + (r'(uses-feature(?:-not-required)?|uses-implied-feature): ' |
| 114 | + r'name=\'([\w\.]+)\'(?: reason=\'([^\']+)\')?'), |
| 115 | + output, |
| 116 | + ) |
| 117 | + for feature_type, feature_name, reason in feature_matches: |
| 118 | + features[feature_name] = { |
| 119 | + 'type': feature_type, |
| 120 | + # e.g., 'uses-feature', |
| 121 | + # 'uses-feature-not-required', |
| 122 | + # 'uses-implied-feature' |
| 123 | + 'reason': reason if reason else 'No reason provided', |
| 124 | + } |
| 125 | + self.data['uses_features'] = features |
| 126 | + |
| 127 | + return self.data |
| 128 | + |
| 129 | + def get_apk_files(self): |
| 130 | + """List all files in the APK.""" |
| 131 | + output = self._execute_command( |
| 132 | + [self.aapt_path, 'list', self.apk_path]) |
| 133 | + if output: |
| 134 | + return output.splitlines() |
| 135 | + return [] |
| 136 | + |
| 137 | + def get_apk_strings(self): |
| 138 | + """Extract strings from the APK.""" |
| 139 | + output = self._execute_command( |
| 140 | + [self.aapt2_path, 'dump', 'strings', self.apk_path]) |
| 141 | + if output: |
| 142 | + return self._get_strings(output) |
| 143 | + return [] |
| 144 | + |
| 145 | + def get_apk_features(self): |
| 146 | + """Extract features from the APK.""" |
| 147 | + output = self._execute_command( |
| 148 | + [self.aapt2_path, 'dump', 'badging', self.apk_path]) |
| 149 | + if output: |
| 150 | + return self._parse_badging(output) |
| 151 | + return self.data |
0 commit comments