google / python-adb

Python ADB + Fastboot implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Generate the RSA auth keys

a1ex4 opened this issue · comments

commented

Hi guys, I've been trying to figure out how to generate the ADB keys within this python module, so we can run a 100% Python implementation of ADB, without ever requiring to run the server to first generate the keys. There is an ongoing discussion here regarding this issue.

So I wrote a simple code with the supported RSA librairies:

from adb.usb_exceptions import DeviceAuthError
from adb import adb_commands
from adb.sign_pythonrsa import PythonRSASigner
from adb.adb_protocol import InvalidChecksumError
import os.path as op
from Crypto.PublicKey import RSA
from adb import sign_pycryptodome

Signer = PythonRSASigner.FromRSAKeyPath

uri='192.168.1.86:5555'
byte_uri = str.encode(uri)
adbkey=op.expanduser('~/.android/adbkey')

if op.isfile(adbkey):
    print('Key found, trying with auth')
    signer = Signer(adbkey)
    adb = adb_commands.AdbCommands().ConnectDevice(serial=byte_uri, rsa_keys=[signer])
else:
    print('Adbkey {} not  found, trying without auth'.format(adbkey))
    key = RSA.generate(2048)
    private_key = key.export_key(pkcs=8)
    public_key = key.publickey().export_key(format='DER')
    with open(adbkey, 'wb') as priv:
      priv.write(private_key)
    with open(adbkey+'.pub', 'wb') as pub:
      pub.write(public_key)
    try:
        adb = adb_commands.AdbCommands().ConnectDevice(serial=byte_uri)
    except DeviceAuthError:
        print('Auth key required for device {}'.format(uri))

But the problem is that the original code generates a custom public key, which is "naked" as in no formating is present and there is also the user and hostname of the device.

The code generating the keys is here, although I'm not familiar enough with C to understand and port the function.

To me it seems to be as simple as porting this function, any thoughts ? Could somebody do it ?
Thanks! 🙂

I tried previously and ran out of time trying to figure out how to get the key in that format, sorry! Anybody else want to try?

commented

I tried previously and ran out of time trying to figure out how to get the key in that format, sorry! Anybody else want to try?

I'm not alone who run out of time on ADB stuff