Headers Module

class apkInspector.headers.CentralDirectory(entries)

The CentralDirectory containing all the CentralDirectoryEntry entries discovered. The entries are listed as a dictionary where the filename is the key.

classmethod from_dict(entry_dict)

Convert a dictionary back to an instance of the class.

Parameters:

entry_dict (dict) – the dictionary

Returns:

the instance of the class

Return type:

CentralDirectory

classmethod parse(apk_file, eocd: EndOfCentralDirectoryRecord | None = None)

Method that is used to parse the central directory header according to the specification https://pkware.cachefly.net/webdocs/APPNOTE/APPNOTE-6.3.9.TXT based on the offset provided by the end of central directory record: eocd.offset_of_start_of_central_directory.

Parameters:
  • apk_file (bytesIO) – The already read/loaded data of the APK file e.g. with open(‘test.apk’, ‘rb’) as apk_file

  • eocd (EndOfCentralDirectoryRecord) – End of central directory record

Returns:

Returns a dictionary with all the entries discovered. The filename of each entry is used as the key. Besides the fields defined by the specification, each entry has an additional field named ‘Offset in the central directory header’, which includes the offset of the entry in the central directory itself.

Return type:

CentralDirectory

to_dict()

Represent the class as a dictionary.

Returns:

returns the dictionary

Return type:

dict

class apkInspector.headers.CentralDirectoryEntry(version_made_by, version_needed_to_extract, general_purpose_bit_flag, compression_method, file_last_modification_time, file_last_modification_date, crc32_of_uncompressed_data, compressed_size, uncompressed_size, file_name_length, extra_field_length, file_comment_length, disk_number_where_file_starts, internal_file_attributes, external_file_attributes, relative_offset_of_local_file_header, filename, extra_field, file_comment, offset_in_central_directory)

A class representing each entry in the central directory.

classmethod from_dict(entry_dict)

Convert a dictionary back to an instance of the class.

Parameters:

entry_dict (dict) – the dictionary

Returns:

the instance of the class

Return type:

CentralDirectoryEntry

to_dict()

Represent the class as a dictionary.

Returns:

returns the dictionary

Return type:

dict

class apkInspector.headers.EndOfCentralDirectoryRecord(signature, number_of_this_disk, disk_where_central_directory_starts, number_of_central_directory_records_on_this_disk, total_number_of_central_directory_records, size_of_central_directory, offset_of_start_of_central_directory, comment_length, comment)

A class to provide details about the end of central directory record.

classmethod from_dict(entry_dict)

Convert a dictionary back to an instance of the class.

Parameters:

entry_dict (dict) – the dictionary

Returns:

the instance of the class

Return type:

EndOfCentralDirectoryRecord

classmethod parse(apk_file)

Method to locate the “end of central directory record signature” as the first step of the correct process of reading a ZIP archive. Should be noted that certain APKs do not follow the zip specification and declare multiple “end of central directory records”. For this reason the search for the corresponding signature of the eocd starts from the end of the apk.

Parameters:

apk_file (bytesIO) – The already read/loaded data of the APK file e.g. with open(‘test.apk’, ‘rb’) as apk_file

Returns:

Returns the end of central directory record with all the information available if the corresponding signature is found. If not, then it returns None.

Return type:

EndOfCentralDirectoryRecord or None

to_dict()

Represent the class as a dictionary.

Returns:

returns the dictionary

Return type:

dict

class apkInspector.headers.LocalHeaderRecord(version_needed_to_extract, general_purpose_bit_flag, compression_method, file_last_modification_time, file_last_modification_date, crc32_of_uncompressed_data, compressed_size, uncompressed_size, file_name_length, extra_field_length, filename, extra_field)

The local header for each entry discovered.

classmethod from_dict(entry_dict)

Convert a dictionary back to an instance of the class.

Parameters:

entry_dict (dict) – the dictionary

Returns:

the instance of the class

Return type:

LocalHeaderRecord

classmethod parse(apk_file, entry_of_interest: CentralDirectoryEntry)

Method that attempts to read the local file header according to the specification https://pkware.cachefly.net/webdocs/APPNOTE/APPNOTE-6.3.9.TXT.

Parameters:
  • apk_file (bytesIO) – The already read/loaded data of the APK file e.g. with open(‘test.apk’, ‘rb’) as apk_file

  • entry_of_interest (CentralDirectoryEntry) – The central directory header of the specific entry of interest

Returns:

Returns a dictionary with the local header information or None if it failed to find the header.

Return type:

LocalHeaderRecord or None

to_dict()

Represent the class as a dictionary.

Returns:

returns the dictionary

Return type:

dict

class apkInspector.headers.ZipEntry(zip_bytes, eocd: EndOfCentralDirectoryRecord, central_directory: CentralDirectory, local_headers: Dict[str, LocalHeaderRecord])

Is the actual APK represented as a composition of the previous classes, which are: the EndOfCentralDirectoryRecord, the CentralDirectory and a dictionary of values of LocalHeaderRecord.

extract_all(extract_path, apk_name)

Extracts all the contents of the APK.

Parameters:
  • extract_path (str) – where to extract it

  • apk_name (str) – the name of the apk

get_central_directory_entry_dict(filename)

Method to retrieve the central directory entry for a specific filename.

Parameters:

filename (str) – the filename of the file to search for in the central directory

Returns:

returns a dictionary of the central directory entry or None if the filename is not found

Return type:

dict

get_local_header_dict(filename)

Method to retrieve the local header of a specific filename.

Parameters:

filename (str) – the filename of the entry to search for among the local headers

Returns:

returns a ditionary of the local header entry or None if the filename is not found

Return type:

dict

infolist() Dict[str, CentralDirectoryEntry]

List of information about the entries in the central directory.

Returns:

returns a dictionary where the keys are the filenames and the values are each an instance of the CentralDirectoryEntry

Return type:

dict

namelist()

List of the filenames included in the central directory.

Returns:

returns the list of the filenames

Return type:

list

classmethod parse(inc_apk, raw: bool = True)

Method to start processing an APK. The raw (bytes) APK may be passed or the path to it.

Parameters:
  • inc_apk (str or bytesIO) – the incoming apk, either path or bytes

  • raw (bool) – boolean flag to specify whether it is the raw apk in bytes or not

Returns:

returns the instance of the class

Return type:

ZipEntry

classmethod parse_single(apk_file, filename, eocd: EndOfCentralDirectoryRecord | None = None, central_directory: CentralDirectory | None = None)

Similar to parse, but instead of parsing the entire APK, it only targets the specified file.

Parameters:
  • apk_file (bytesIO) – The apk file expected raw

  • filename (str) – the filename of the file to be parsed

  • eocd (EndOfCentralDirectoryRecord(, optional)) – Optionally, the instance of the end of central directory from the APK

  • central_directory (CentralDirectory(, optional)) – Optionally, the instance of the central directory record

Returns:

returns the instance of the class

Return type:

ZipEntry

read(name, save: bool = False)

Method to utilize the extract module and extract a single entry from the APK based on the filename.

Parameters:
  • name (str) – the name of the file to be read/extracted

  • save (bool(, optional)) – boolean to define whether the extracted file should be saved as well or not

Returns:

returns the raw bytes of the filename that was extracted

Return type:

bytes

to_dict()

Represent the class as a dictionary.

Returns:

returns the dictionary

Return type:

dict

apkInspector.headers.print_headers_of_filename(cd_h_of_file, local_header_of_file)

Prints out the details for both the central directory header and the local file header. Useful for the CLI.

Parameters:
  • cd_h_of_file (dict) – central directory header of a filename as it may be retrieved from headers_of_filename

  • local_header_of_file (dict) – local header dictionary of a filename as it may be retrieved from headers_of_filename

apkInspector.headers.show_and_save_info_of_headers(entries, apk_name, header_type: str, export: bool, show: bool)

Print information for each entry for the central directory header and allow to possibly export to JSON.

Parameters:
  • entries (dict) – The dictionary with all the entries for the central directory

  • apk_name (str) – String with the name of the APK, so it can be used for the export.

  • header_type (str) – What type of header that is, either central_directory or local, to be used for the export

  • export (bool) – Boolean for exporting or not to JSON

  • show (bool) – Boolean for printing or not the entries