-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathget_certificate.py
executable file
·56 lines (49 loc) · 2.13 KB
/
get_certificate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
#! /usr/bin/python3
import os
import sys
import argparse
from androguard.core.bytecodes.apk import APK
from androguard.core import androconf
def convert_x509_name(name):
"""
Convert x509 name to a string
"""
types = {
'country_name': 'C',
'state_or_province_name': 'ST',
'locality_name': 'L',
'organization_name': 'O',
'organizational_unit_name': 'OU',
'common_name': 'CN',
'email_address': 'emailAddress'
}
return '/'.join(['{}={}'.format(types[attr], name.native[attr]) for attr in name.native])
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("APK", help="Path to an APK file")
args = parser.parse_args()
if os.path.isdir(args.APK):
for f in os.listdir(args.APK):
if os.path.isfile(f):
if androconf.is_android(os.path.join(args.APK, f)) == 'APK':
apk = APK(os.path.join(args.APK, f))
if len(apk.get_certificates()) > 0:
cert = apk.get_certificates()[0]
print("{} : {} - {}".format(os.path.join(args.APK, f), cert.sha1_fingerprint.replace(' ', ''), convert_x509_name(cert.issuer)))
else:
print("{} : no certificate".format(os.path.join(args.APK, f)))
elif os.path.isfile(args.APK):
apk = APK(args.APK)
if len(apk.get_certificates()) > 0:
cert = apk.get_certificates()[0]
print("SHA1: {}".format(cert.sha1_fingerprint.replace(' ', '')))
print('Serial: {:X}'.format(cert.serial_number))
print("Issuer: {}".format(convert_x509_name(cert.issuer)))
print("Subject: {}".format(convert_x509_name(cert.subject)))
print("Not Before: {}".format(cert['tbs_certificate']['validity']['not_before'].native.strftime('%b %-d %X %Y %Z')))
print("Not After: {}".format(cert['tbs_certificate']['validity']['not_after'].native.strftime('%b %-d %X %Y %Z')))
else:
print("No certificate here, weird")
else:
print("Invalid file path")
sys.exit(-1)