-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapticket-nonce-checker.py
executable file
·59 lines (48 loc) · 2.06 KB
/
apticket-nonce-checker.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
57
58
#!/usr/bin/python
# Script which parses 32-bit SHSH/APTickets and prints the APTicket nonce, if any.
# Compatible with macOS and Linux. Requires openssl.
# Author: axi0mX
import binascii, plistlib, subprocess, sys
def print_apticket_nonce(data):
try:
p = subprocess.Popen(['openssl', 'asn1parse', '-inform', 'DER'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
(stdout, stderr) = p.communicate(data)
except OSError as e:
print 'ERROR: Something went wrong executing openssl! Details:', e
sys.exit(1)
if 'SEQUENCE' not in stdout:
print 'ERROR: OpenSSL returned unexpected output when parsing APTicket.'
sys.exit(1)
for line in stdout.split('\n'):
if 'prim: cont [ 18 ]' in line:
tokens = line.split()
assert tokens[2] == 'l='
tokens0 = tokens[0].split(':')
assert tokens0[1][:2] == 'd='
tokens1 = tokens[1].split('=')
assert tokens1[0] == 'hl'
offset = int(tokens0[0])
header_length = int(tokens1[1])
data_length = int(tokens[3])
nonce = data[offset + header_length:offset + header_length + data_length]
print 'APTicket has a nonce.'
print 'Nonce (hex dump): %s' % binascii.hexlify(nonce)
print 'Nonce length: %s bytes' % len(nonce)
return
print 'APTicket does not have a nonce.'
if __name__ == '__main__':
if len(sys.argv) < 2:
print 'ERROR: Provide path to SHSH or APTicket as argument to this script.'
sys.exit(1)
data = open(sys.argv[1], 'rb').read()
if data.startswith('bplist00'):
print 'ERROR: This is a binary plist. Convert it to XML first.'
print 'This command works on macOS: plutil -convert xml1 %s' % sys.argv[1]
sys.exit(1)
if '<plist version=' in data:
print 'Parsing APTicket from SHSH file.'
shsh = plistlib.readPlistFromString(data)
print_apticket_nonce(shsh['APTicket'].data)
else:
print 'Parsing APTicket.'
print_apticket_nonce(data)