Class: ApkAnalyzer::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/apk_analyzer/analyzer.rb

Constant Summary collapse

HEX_FALSE =
'0x0'.freeze
HEX_TRUE =
'0xffffffff'.freeze
REQUIRED =
'required'.freeze
GL_ES_VERSION =
'glEsVersion'
NAME =
'name'
ACTION =
'action'
CATEGORY =
'category'
ANDROID_MANIFEST_FILE =
'AndroidManifest.xml'

Instance Method Summary collapse

Constructor Details

#initialize(apk_path) ⇒ Analyzer

Returns a new instance of Analyzer.



17
18
19
20
21
22
23
# File 'lib/apk_analyzer/analyzer.rb', line 17

def initialize(apk_path)
  # Deactivating invalid date warnings in zip for apktools gem and apk analyzer code
  Zip.warn_invalid_date = false
  @apk_path = apk_path
  raise 'File is not a valid apk file' unless valid_zip?(apk_path)
  @apk_xml = ApkXml.new(apk_path)
end

Instance Method Details

#collect_cert_infoObject

Certificate info. Issuer and dates



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/apk_analyzer/analyzer.rb', line 70

def collect_cert_info
  # Redirect keytool check error to /dev/null
  os_has_keytool = system('keytool 2>/dev/null')
  raise 'keytool dependency not satisfied. Make sure that JAVA keytool utility is installed' unless os_has_keytool
  cert_info = {}
  certificate_raw = `keytool -printcert -rfc -jarfile #{@apk_path.shellescape}`
  certificate_content_regexp = /(-----BEGIN CERTIFICATE-----.*-----END CERTIFICATE-----)/m
  matched_data = certificate_content_regexp.match(certificate_raw)
  if matched_data
    certificate_content = matched_data.captures[0]
    cert_info = {
        issuer_raw: nil,
        cn: nil,
        ou: nil,
        o: nil,
        st: nil,
        l: nil,
        c: nil,
        creation_date: nil,
        expiration_date: nil
    }
    cert_extract_dates(certificate_content, cert_info)
    cert_extract_issuer(certificate_content, cert_info)
  else
    puts 'Failed to find CERT.RSA file in APK'
  end
  cert_info
end

#collect_manifest_infoObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/apk_analyzer/analyzer.rb', line 25

def collect_manifest_info
  manifest_file_path = find_file_in_apk(ANDROID_MANIFEST_FILE)
  raise 'Failed to find Manifest file in apk' if manifest_file_path.nil?
  begin
    manifest_xml = Nokogiri::XML(@apk_xml.parse_xml('AndroidManifest.xml', true, true))
  rescue => e
    puts "Failed to parse #{ANDROID_MANIFEST_FILE}"
    log_expection e
  end

  manifest_info = {}
  begin
    manifest_info[:path_in_apk] = manifest_file_path
    content = {}
    # application content
    content[:application_info] = collect_application_info(manifest_xml)

    # intents
    content[:intents] = collect_intent_info(manifest_xml)

    # sdk infos
    sdk_infos = collect_sdk_info(manifest_xml)
    content[:uses_sdk] = { minimum_sdk_version: sdk_infos[0], target_sdk_version: sdk_infos[1] }

    # uses permission
    uses_permissions = collect_uses_permission_info(manifest_xml)
    content[:uses_permissions] = uses_permissions

    # uses features
    feature_list = collect_uses_feature_info(manifest_xml)
    content[:uses_features] = feature_list

    # screen compatibility
    supported_screens = collect_supported_screens(manifest_xml)
    content[:supports_screens] = supported_screens

    manifest_info[:content] = content
  rescue => e
    log_expection e
    raise "Invalid xml found"
  end
  manifest_info
end