-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
367 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
build | ||
package | ||
.DS_Store | ||
__pycache__ | ||
misc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
from .exceptions import * | ||
|
||
import usb.core | ||
import usb.util | ||
|
||
DFU_VENDOR_ID = 0x05AC | ||
DFU_PRODUCT_ID = 0x1227 # can also be 0x1226/0x1228 in case of different SDOM | ||
|
||
ANYA_IBFL_FLAG = (1 << 6) | ||
ANYA_USB_TIMEOUT = 100 | ||
|
||
KBAG_SIZE = 0x30 | ||
|
||
DFU_DETACH = 0 | ||
DFU_DNLOAD = 1 | ||
DFU_UPLOAD = 2 | ||
DFU_GETSTATUS = 3 | ||
DFU_CLR_STATUS = 4 | ||
DFU_GETSTATE = 5 | ||
DFU_ABORT = 6 | ||
ANYA_DECRYPT_KBAG = 7 | ||
ANYA_CLEAR_KBAG = 8 | ||
ANYA_REBOOT = 9 | ||
|
||
|
||
def decode_kbag(kbag: str) -> bytes: | ||
try: | ||
assert len(kbag) == KBAG_SIZE * 2 | ||
return bytearray.fromhex(kbag) | ||
except AssertionError: | ||
raise AnyaValueError("invalid KBAG length") | ||
except ValueError: | ||
raise AnyaValueError("invalid KBAG") | ||
|
||
def encode_key(key: bytes, to_upper: bool = False) -> str: | ||
try: | ||
assert len(key) == KBAG_SIZE | ||
key_string = key.hex() | ||
except (AssertionError, ValueError): | ||
raise AnyaValueError("invalid key") | ||
|
||
if to_upper: | ||
return key_string.upper() | ||
else: | ||
return key_string | ||
|
||
class AnyaDevice: | ||
def __init__(self, vid: int = DFU_VENDOR_ID, pid: int = DFU_PRODUCT_ID, ecid: int = None): | ||
self.vid = vid | ||
self.pid = pid | ||
self.ecid = ecid | ||
|
||
def _match(self, dev): | ||
IBFL = "IBFL:" | ||
ECID = "ECID:" | ||
|
||
if dev.idVendor == self.vid and dev.idProduct == self.pid: | ||
sn = dev.serial_number | ||
if IBFL in sn: | ||
ibfl_pos = sn.index(IBFL) + len(IBFL) | ||
ibfl_val = int(sn[ibfl_pos:ibfl_pos + 2], 16) | ||
|
||
if ibfl_val & ANYA_IBFL_FLAG: | ||
if self.ecid is not None: | ||
if ECID + "%016X" % self.ecid not in sn: | ||
return False | ||
|
||
return True | ||
|
||
return False | ||
|
||
def connect(self): | ||
self._device = usb.core.find(custom_match=self._match) | ||
if not self._device: | ||
raise AnyaNotFound("no Anya devices found") | ||
|
||
self.clear_kbag() | ||
|
||
print("found: %s" % self._device.serial_number) | ||
|
||
def disconnect(self): | ||
usb.util.dispose_resources(self._device) | ||
|
||
def clear_kbag(self): | ||
try: | ||
self._device.ctrl_transfer(0x21, ANYA_CLEAR_KBAG, 0, 0, None) | ||
except Exception: | ||
raise AnyaUSBError("failed to clear KBAG") | ||
|
||
def send_kbag(self, kbag: bytes): | ||
if len(kbag) != KBAG_SIZE: | ||
raise AnyaValueError("KBAG must be exactly bytes %d in size" % KBAG_SIZE) | ||
|
||
try: | ||
assert self._device.ctrl_transfer(0x21, DFU_DNLOAD, 0, 0, kbag) == KBAG_SIZE | ||
except Exception: | ||
raise AnyaUSBError("failed to send KBAG") | ||
|
||
def get_key(self) -> bytes: | ||
try: | ||
key = self._device.ctrl_transfer(0xA1, ANYA_DECRYPT_KBAG, 0, 0, KBAG_SIZE) | ||
assert len(key) == KBAG_SIZE | ||
except Exception: | ||
raise AnyaUSBError("failed to decrypt KBAG") | ||
|
||
return bytes(key) | ||
|
||
def decrypt_kbag(self, kbag: bytes) -> bytes: | ||
self.send_kbag(kbag) | ||
return self.get_key() | ||
|
||
def reboot(self): | ||
try: | ||
self._device.ctrl_transfer(0x21, ANYA_REBOOT, 0, 0, None) | ||
except Exception: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class AnyaError(Exception): | ||
pass | ||
|
||
class AnyaValueError(AnyaError): | ||
pass | ||
|
||
class AnyaNotFound(AnyaError): | ||
pass | ||
|
||
class AnyaUSBError(AnyaError): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import argparse | ||
from time import time | ||
from secrets import token_bytes | ||
|
||
from anya import * | ||
from anya.exceptions import * | ||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description='Decrypt some KBAG or run a benchmark') | ||
parser.add_argument("-k", dest="kbag", help="decrypt a KBAG") | ||
parser.add_argument("-b", dest="count", type=int, help="run benchmark") | ||
parser.add_argument("-e", dest="ecid", type=int, help="ECID to look for") | ||
args = parser.parse_args() | ||
|
||
if (not args.kbag and not args.count) or (args.kbag and args.count): | ||
parser.print_help() | ||
exit(-1) | ||
|
||
dev = AnyaDevice(ecid=args.ecid) | ||
|
||
try: | ||
dev.connect() | ||
except AnyaError as e: | ||
print("failed to connect: %s" % str(e)) | ||
exit(-1) | ||
|
||
if args.kbag: | ||
try: | ||
decoded = decode_kbag(args.kbag) | ||
except AnyaValueError as e: | ||
print("failed to parse KBAG: %s" % str(e)) | ||
exit(-1) | ||
|
||
try: | ||
key = dev.decrypt_kbag(decoded) | ||
except AnyaUSBError as e: | ||
print("failed to decrypt KBAG: %s" % str(e)) | ||
exit(-1) | ||
|
||
print(encode_key(key, True)) | ||
|
||
elif args.count: | ||
kbags = set() | ||
|
||
for i in range(args.count): | ||
kbags.add(bytes(token_bytes(KBAG_SIZE))) | ||
|
||
print("decrypting...") | ||
|
||
start = time() | ||
|
||
for kbag in kbags: | ||
try: | ||
key = dev.decrypt_kbag(kbag) | ||
except AnyaUSBError as e: | ||
print("failed to decrypt KBAG: %s" % str(e)) | ||
exit(-1) | ||
|
||
end = time() | ||
|
||
delta = end - start | ||
average = args.count / delta | ||
|
||
print("decrypted %d KBAGs in %.6f seconds, average - %.6f KBAGs/sec" % (args.count, delta, average)) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Oops, something went wrong.