| 1 |
import io |
| 2 |
import os |
| 3 |
import unittest |
| 4 |
import hashlib |
| 5 |
|
| 6 |
from apkInspector.extract import extract_file_based_on_header_info |
| 7 |
from apkInspector.headers import EndOfCentralDirectoryRecord, CentralDirectory, ZipEntry, LocalHeaderRecord |
| 8 |
from apkInspector.indicators import apk_tampering_check |
| 9 |
from apkInspector.axml import get_manifest |
| 10 |
|
| 11 |
|
| 12 |
class ApkInspectorTestCase(unittest.TestCase): |
| 13 |
def setUp(self): |
| 14 |
|
| 15 |
test_dir = os.path.dirname(os.path.abspath(__file__)) |
| 16 |
apk_path_orig = os.path.join(test_dir, 'res', 'minimal_orig.apk') |
| 17 |
apk_path_mod = os.path.join(test_dir, 'res', 'minimal_def_mod.apk') |
| 18 |
self.apk_orig = open(apk_path_orig, "rb") |
| 19 |
self.apk_mod = open(apk_path_mod, "rb") |
| 20 |
|
| 21 |
def tearDown(self): |
| 22 |
|
| 23 |
self.apk_orig.close() |
| 24 |
self.apk_mod.close() |
| 25 |
|
| 26 |
def test_find_eocd(self): |
| 27 |
eocd_orig = EndOfCentralDirectoryRecord.parse(self.apk_orig).to_dict() |
| 28 |
eocd_mod = EndOfCentralDirectoryRecord.parse(self.apk_mod).to_dict() |
| 29 |
orig = {'signature': b'PK\x05\x06', 'number_of_this_disk': 0, 'disk_where_central_directory_starts': 0, |
| 30 |
'number_of_central_directory_records_on_this_disk': 23, 'total_number_of_central_directory_records': 23, |
| 31 |
'size_of_central_directory': 1317, 'offset_of_start_of_central_directory': 151552, 'comment_length': 0, |
| 32 |
'comment': ''} |
| 33 |
mod = {'signature': b'PK\x05\x06', 'number_of_this_disk': 0, 'disk_where_central_directory_starts': 0, |
| 34 |
'number_of_central_directory_records_on_this_disk': 24, 'total_number_of_central_directory_records': 24, |
| 35 |
'size_of_central_directory': 1420, 'offset_of_start_of_central_directory': 151552, 'comment_length': 0, |
| 36 |
'comment': ''} |
| 37 |
self.assertEqual(eocd_orig, orig) |
| 38 |
self.assertEqual(eocd_mod, mod) |
| 39 |
|
| 40 |
def test_find_eocd_at_offset_1026(self): |
| 41 |
test_dir = os.path.dirname(os.path.abspath(__file__)) |
| 42 |
expected_hash = "5599105c7d7a09e2d87c39282ae92c2c76c09eb4849ddf8bc27137b294e0afd0" |
| 43 |
with open(os.path.join(test_dir, 'res', 'eocd_at_offset_1026.apk'), 'rb') as apk_file: |
| 44 |
eocd = EndOfCentralDirectoryRecord.parse(apk_file).to_dict() |
| 45 |
self.assertEqual(expected_hash, hashlib.sha256(str(eocd).encode('utf-8')).hexdigest()) |
| 46 |
|
| 47 |
def test_eocd_from_dict(self): |
| 48 |
mod = {'signature': b'PK\x05\x06', 'number_of_this_disk': 0, 'disk_where_central_directory_starts': 0, |
| 49 |
'number_of_central_directory_records_on_this_disk': 24, 'total_number_of_central_directory_records': 24, |
| 50 |
'size_of_central_directory': 1420, 'offset_of_start_of_central_directory': 151552, 'comment_length': 0, |
| 51 |
'comment': ''} |
| 52 |
eocd_instance = EndOfCentralDirectoryRecord.from_dict(mod) |
| 53 |
self.assertEqual(eocd_instance.size_of_central_directory, 1420) |
| 54 |
self.assertEqual(eocd_instance.offset_of_start_of_central_directory, 151552) |
| 55 |
self.assertEqual(eocd_instance.total_number_of_central_directory_records, 24) |
| 56 |
|
| 57 |
def test_parse_central_directory(self): |
| 58 |
hash_orig = 'ae178cbf4ccf180d39740060ab3bfbdfbbd8754c8a154f46d67a8ff7966a6d70' |
| 59 |
hash_mod = '7e6c1667c1231f5b5c667be7220775cf7a3c05746aee6e392db1f696c4ce8eea' |
| 60 |
central_directory_entries_orig = CentralDirectory.parse(self.apk_orig).to_dict() |
| 61 |
central_directory_entries_mod = CentralDirectory.parse(self.apk_mod).to_dict() |
| 62 |
tocheck_orig = hashlib.sha256(str(central_directory_entries_orig).encode('utf-8')).hexdigest() |
| 63 |
tocheck_mod = hashlib.sha256(str(central_directory_entries_mod).encode('utf-8')).hexdigest() |
| 64 |
self.assertEqual(tocheck_orig, hash_orig) |
| 65 |
self.assertEqual(tocheck_mod, hash_mod) |
| 66 |
|
| 67 |
def test_central_directory_from_dict(self): |
| 68 |
cd_orig_dict = {'AndroidManifest.xml': {'version_made_by': 788, 'version_needed_to_extract': 20, |
| 69 |
'general_purpose_bit_flag': 0, 'compression_method': 0, |
| 70 |
'file_last_modification_time': 2081, 'file_last_modification_date': 545, |
| 71 |
'crc32_of_uncompressed_data': 998929674, 'compressed_size': 2084, |
| 72 |
'uncompressed_size': 2084, 'file_name_length': 19, |
| 73 |
'extra_field_length': 0, 'file_comment_length': 0, |
| 74 |
'disk_number_where_file_starts': 0, 'internal_file_attributes': 0, |
| 75 |
'external_file_attributes': 2176057344, |
| 76 |
'relative_offset_of_local_file_header': 0, |
| 77 |
'filename': 'AndroidManifest.xml', 'extra_field': '', |
| 78 |
'file_comment': '', 'offset_in_central_directory': 151552}, |
| 79 |
'classes.dex': {'version_made_by': 788, 'version_needed_to_extract': 20, |
| 80 |
'general_purpose_bit_flag': 0, 'compression_method': 8, |
| 81 |
'file_last_modification_time': 2081, 'file_last_modification_date': 545, |
| 82 |
'crc32_of_uncompressed_data': 1432748217, 'compressed_size': 616, |
| 83 |
'uncompressed_size': 1032, 'file_name_length': 11, 'extra_field_length': 0, |
| 84 |
'file_comment_length': 0, 'disk_number_where_file_starts': 0, |
| 85 |
'internal_file_attributes': 0, 'external_file_attributes': 2175008768, |
| 86 |
'relative_offset_of_local_file_header': 2136, 'filename': 'classes.dex', |
| 87 |
'extra_field': '', 'file_comment': '', 'offset_in_central_directory': 151617}} |
| 88 |
cd_dir_instance = CentralDirectory.from_dict(cd_orig_dict) |
| 89 |
self.assertEqual(cd_dir_instance.entries['AndroidManifest.xml'].to_dict()['file_last_modification_time'], 2081) |
| 90 |
|
| 91 |
def test_parse_local_header(self): |
| 92 |
ze_orig = ZipEntry.parse_single(self.apk_orig, "AndroidManifest.xml") |
| 93 |
ze_mod = ZipEntry.parse_single(self.apk_mod, "AndroidManifest.xml") |
| 94 |
comp_orig = {'version_needed_to_extract': 20, 'general_purpose_bit_flag': 0, 'compression_method': 0, |
| 95 |
'file_last_modification_time': 2081, 'file_last_modification_date': 545, |
| 96 |
'crc32_of_uncompressed_data': 998929674, 'compressed_size': 2084, 'uncompressed_size': 2084, |
| 97 |
'file_name_length': 19, 'extra_field_length': 3, 'filename': 'AndroidManifest.xml', |
| 98 |
'extra_field': '\x00\x00\x00'} |
| 99 |
comp_mod = {'version_needed_to_extract': 20, 'general_purpose_bit_flag': 0, 'compression_method': 30208, |
| 100 |
'file_last_modification_time': 45686, 'file_last_modification_date': 22321, |
| 101 |
'crc32_of_uncompressed_data': 2768741858, 'compressed_size': 2084, 'uncompressed_size': 2084, |
| 102 |
'file_name_length': 19, 'extra_field_length': 0, 'filename': 'AndroidManifest.xml', |
| 103 |
'extra_field': ''} |
| 104 |
self.assertEqual(ze_orig.local_headers["AndroidManifest.xml"].to_dict(), comp_orig) |
| 105 |
self.assertEqual(ze_mod.local_headers["AndroidManifest.xml"].to_dict(), comp_mod) |
| 106 |
|
| 107 |
def test_local_header_from_dict(self): |
| 108 |
andro_local_dict = {'version_needed_to_extract': 20, 'general_purpose_bit_flag': 0, 'compression_method': 30208, |
| 109 |
'file_last_modification_time': 45686, 'file_last_modification_date': 22321, |
| 110 |
'crc32_of_uncompressed_data': 2768741858, 'compressed_size': 2084, |
| 111 |
'uncompressed_size': 2084, 'file_name_length': 19, 'extra_field_length': 0, |
| 112 |
'filename': 'AndroidManifest.xml', 'extra_field': ''} |
| 113 |
local_instance = LocalHeaderRecord.from_dict(andro_local_dict) |
| 114 |
self.assertEqual(local_instance.filename, 'AndroidManifest.xml') |
| 115 |
self.assertEqual(local_instance.compression_method, 30208) |
| 116 |
|
| 117 |
def test_extract_file_based_on_header_info(self): |
| 118 |
zipentry = ZipEntry.parse(self.apk_orig) |
| 119 |
cd_h_of_file = zipentry.get_central_directory_entry_dict("AndroidManifest.xml") |
| 120 |
local_header_of_file = zipentry.get_local_header_dict("AndroidManifest.xml") |
| 121 |
ext_hash = '5f37db22380177c20804d8602ffbdc048caeaa851412ab127e6fe1c9a9b1c78e' |
| 122 |
androidManifest_extracted = \ |
| 123 |
extract_file_based_on_header_info(self.apk_orig, local_header_of_file, cd_h_of_file)[0] |
| 124 |
self.assertEqual(hashlib.sha256(androidManifest_extracted).hexdigest(), ext_hash) |
| 125 |
|
| 126 |
def test_extract_modified_file_based_on_header_info(self): |
| 127 |
zipentry = ZipEntry.parse(self.apk_mod) |
| 128 |
cd_h_of_file = zipentry.get_central_directory_entry_dict("AndroidManifest.xml") |
| 129 |
local_header_of_file = zipentry.get_local_header_dict("AndroidManifest.xml") |
| 130 |
ext_hash = '5dc10a071f28ef25c6caa3cd3aef0d93985dacb1dba08ca46632ba655129e503' |
| 131 |
androidManifest_extracted = extract_file_based_on_header_info(self.apk_mod, local_header_of_file, cd_h_of_file)[ |
| 132 |
0] |
| 133 |
self.assertEqual(hashlib.sha256(androidManifest_extracted).hexdigest(), ext_hash) |
| 134 |
|
| 135 |
def test_extract_when_zero_size_and_extra_field_present(self): |
| 136 |
test_dir = os.path.dirname(os.path.abspath(__file__)) |
| 137 |
with open(os.path.join(test_dir, 'res', 'minimal_SIZE_ExtraField.apk'), 'rb') as apk_file: |
| 138 |
zipentry = ZipEntry.parse(apk_file) |
| 139 |
cd_h_of_file = zipentry.get_central_directory_entry_dict("AndroidManifest.xml") |
| 140 |
local_header_of_file = zipentry.get_local_header_dict("AndroidManifest.xml") |
| 141 |
ext_hash = '5f37db22380177c20804d8602ffbdc048caeaa851412ab127e6fe1c9a9b1c78e' |
| 142 |
androidManifest_extracted = \ |
| 143 |
extract_file_based_on_header_info(apk_file, local_header_of_file, cd_h_of_file)[0] |
| 144 |
self.assertEqual(hashlib.sha256(androidManifest_extracted).hexdigest(), ext_hash) |
| 145 |
|
| 146 |
def test_zipentry_raw_and_path(self): |
| 147 |
test_dir = os.path.dirname(os.path.abspath(__file__)) |
| 148 |
apk_file = os.path.join(test_dir, 'res', 'minimal_orig.apk') |
| 149 |
zipentry_path = ZipEntry.parse(apk_file, raw=False) |
| 150 |
zipentry_raw = ZipEntry.parse(self.apk_orig) |
| 151 |
self.assertEqual(hashlib.sha256(zipentry_path.zip.read()).hexdigest(), |
| 152 |
hashlib.sha256(zipentry_raw.zip.read()).hexdigest()) |
| 153 |
|
| 154 |
def test_zipentry_read(self): |
| 155 |
zipentry = ZipEntry.parse(self.apk_orig) |
| 156 |
manifest = zipentry.read("AndroidManifest.xml") |
| 157 |
self.assertEqual(hashlib.sha256(manifest).hexdigest(), |
| 158 |
"5f37db22380177c20804d8602ffbdc048caeaa851412ab127e6fe1c9a9b1c78e") |
| 159 |
|
| 160 |
def test_zipentry_read_non_existing(self): |
| 161 |
zipentry = ZipEntry.parse(self.apk_orig) |
| 162 |
with self.assertRaises(KeyError): |
| 163 |
zipentry.read("non-existing-entry") |
| 164 |
|
| 165 |
def test_zipentry_central_non_existing(self): |
| 166 |
zipentry = ZipEntry.parse(self.apk_orig) |
| 167 |
with self.assertRaises(KeyError) as context: |
| 168 |
zipentry.get_central_directory_entry_dict("non-existing-entry") |
| 169 |
|
| 170 |
def test_zipentry_to_dict(self): |
| 171 |
zipentry = ZipEntry.parse(self.apk_mod) |
| 172 |
self.assertEqual(hashlib.sha256(str(zipentry.to_dict()).encode('utf-8')).hexdigest(), |
| 173 |
"fbddeffe316446f47e49abe1dbf03fa2a8c0f5af3ad965609368b63932683a2d") |
| 174 |
|
| 175 |
def test_zipentry_infolist(self): |
| 176 |
zipentry = ZipEntry.parse(self.apk_mod) |
| 177 |
self.assertEqual(set(zipentry.infolist().keys()), |
| 178 |
{'res/13.webp', 'res/Nt.webp', 'res/j_.webp', 'classes.dex', 'AndroidManifest.xml', |
| 179 |
'res/sK.webp', 'res/u5.webp', 'res/0w.xml', 'res/fq.webp', 'res/qs.webp', 'res/MO.webp', |
| 180 |
'res/BW.xml', 'resources.arsc', 'res/d2.webp', 'res/iE.webp', 'res/-6.webp', 'res/5c.webp', |
| 181 |
'res/Fd.xml', 'res/yw.webp', 'res/0K.xml', 'res/Qr.xml', 'res/9Q.webp', |
| 182 |
'META-INF/com/android/build/gradle/app-metadata.properties', 'res/Sn.webp'}) |
| 183 |
|
| 184 |
def test_zipentry_namelist(self): |
| 185 |
zipentry = ZipEntry.parse(self.apk_mod) |
| 186 |
self.assertEqual(zipentry.namelist(), ['AndroidManifest.xml', 'classes.dex', 'resources.arsc', |
| 187 |
'META-INF/com/android/build/gradle/app-metadata.properties', |
| 188 |
'res/BW.xml', 'res/9Q.webp', 'res/Sn.webp', 'res/5c.webp', 'res/yw.webp', |
| 189 |
'res/Nt.webp', 'res/MO.webp', 'res/-6.webp', 'res/j_.webp', 'res/0K.xml', |
| 190 |
'res/Qr.xml', 'res/d2.webp', 'res/fq.webp', 'res/0w.xml', 'res/iE.webp', |
| 191 |
'res/qs.webp', 'res/Fd.xml', 'res/u5.webp', 'res/13.webp', |
| 192 |
'res/sK.webp']) |
| 193 |
|
| 194 |
def test_android_manifest_decoding_orig(self): |
| 195 |
zipentry = ZipEntry.parse(self.apk_orig) |
| 196 |
cd_h_of_file = zipentry.get_central_directory_entry_dict("AndroidManifest.xml") |
| 197 |
local_header_of_file = zipentry.get_local_header_dict("AndroidManifest.xml") |
| 198 |
extracted_data = io.BytesIO( |
| 199 |
extract_file_based_on_header_info(self.apk_orig, local_header_of_file, cd_h_of_file)[0]) |
| 200 |
manifest = get_manifest(extracted_data) |
| 201 |
manifest_orig = '2846a9e29eb2d75623246440ef02d5a098cde7d21e3948b3ece3c68e3bae13f3' |
| 202 |
self.assertEqual(hashlib.sha256(str(manifest).encode('utf-8')).hexdigest(), manifest_orig) |
| 203 |
|
| 204 |
def test_android_manifest_decoding_mod(self): |
| 205 |
zipentry = ZipEntry.parse(self.apk_mod) |
| 206 |
cd_h_of_file = zipentry.get_central_directory_entry_dict("AndroidManifest.xml") |
| 207 |
local_header_of_file = zipentry.get_local_header_dict("AndroidManifest.xml") |
| 208 |
extracted_data = io.BytesIO( |
| 209 |
extract_file_based_on_header_info(self.apk_mod, local_header_of_file, cd_h_of_file)[0]) |
| 210 |
manifest = get_manifest(extracted_data) |
| 211 |
manifest_mod = '2846a9e29eb2d75623246440ef02d5a098cde7d21e3948b3ece3c68e3bae13f3' |
| 212 |
self.assertEqual(hashlib.sha256(str(manifest).encode('utf-8')).hexdigest(), manifest_mod) |
| 213 |
|
| 214 |
def test_tampering_indicators(self): |
| 215 |
orig_val = {'zip tampering': {}, 'manifest tampering': {}} |
| 216 |
mod_val = {'zip tampering': {'AndroidManifest.xml': {'central compression method': 30208, 'local compression ' |
| 217 |
'method': 30208, |
| 218 |
'actual compression method': 'STORED_TAMPERED'}}, |
| 219 |
'manifest tampering': {'unexpected_starting_signature_of_androidmanifest': '0x0', 'string_pool': { |
| 220 |
'string_count': 49, 'real_string_count': 32}}} |
| 221 |
orig = apk_tampering_check(self.apk_orig, False) |
| 222 |
mod = apk_tampering_check(self.apk_mod, False) |
| 223 |
self.assertEqual(orig, orig_val) |
| 224 |
self.assertEqual(mod, mod_val) |
| 225 |
|
| 226 |
def test_tampering_indicators_size(self): |
| 227 |
test_dir = os.path.dirname(os.path.abspath(__file__)) |
| 228 |
expected = {'zip tampering': {'AndroidManifest.xml': { |
| 229 |
'differing headers': ['compressed_size', 'extra_field', 'extra_field_length', 'uncompressed_size']}}, |
| 230 |
'manifest tampering': {}} |
| 231 |
with open(os.path.join(test_dir, 'res', 'minimal_SIZE_ExtraField.apk'), 'rb') as apk_file: |
| 232 |
res = apk_tampering_check(apk_file, True) |
| 233 |
self.assertEqual(res, expected) |
| 234 |
|
| 235 |
def test_tampering_zero_end_ns(self): |
| 236 |
test_dir = os.path.dirname(os.path.abspath(__file__)) |
| 237 |
expected = {'zip tampering': {}, 'manifest tampering': {'zero_size_header_for_namespace_end_nodes': True}} |
| 238 |
with open(os.path.join(test_dir, 'res', 'minimal_zero_sized_end_ns.apk'), 'rb') as apk_file: |
| 239 |
res = apk_tampering_check(apk_file, False) |
| 240 |
self.assertEqual(res, expected) |
| 241 |
|
| 242 |
def test_inconsistencies_in_central_local_entries(self): |
| 243 |
test_dir = os.path.dirname(os.path.abspath(__file__)) |
| 244 |
with open(os.path.join(test_dir, 'res', 'truncated-cd.apk'), "rb") as apk_file: |
| 245 |
res = apk_tampering_check(apk_file, False) |
| 246 |
self.assertIn('classes.dex', res['zip tampering']['unique_entries']) |
| 247 |
self.assertIn('classes.deP', res['zip tampering']['unique_entries']) |
| 248 |
|
| 249 |
def test_manifest_invalid_res_map(self): |
| 250 |
test_dir = os.path.dirname(os.path.abspath(__file__)) |
| 251 |
with open(os.path.join(test_dir, 'res', 'manifest', 'AndroidManifest_resource_map_not_found.xml'), "rb") as mf: |
| 252 |
with self.assertRaises(ValueError) as context: |
| 253 |
get_manifest(io.BytesIO(mf.read())) |
| 254 |
self.assertEqual(str(context.exception), "Resource Map header was not detected.") |
| 255 |
|
| 256 |
|
| 257 |
def test_manifest_cdata(self): |
| 258 |
test_dir = os.path.dirname(os.path.abspath(__file__)) |
| 259 |
with open(os.path.join(test_dir, 'res', 'manifest', 'AndroidManifest_CDATA.xml'), "rb") as mf: |
| 260 |
mnfst = get_manifest(io.BytesIO(mf.read())) |
| 261 |
lines = mnfst.split('\n') |
| 262 |
self.assertEqual(lines[5], '<intent-filter>com.erev0s.minimal<action android:name="android.intent.action.MAIN">') |
| 263 |
|
| 264 |
if __name__ == '__main__': |
| 265 |
unittest.main() |