Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Generate the RSA auth keys #131

Open
a1ex4 opened this issue Oct 19, 2018 · 2 comments
Open

Generate the RSA auth keys #131

a1ex4 opened this issue Oct 19, 2018 · 2 comments

Comments

@a1ex4
Copy link

a1ex4 commented Oct 19, 2018

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! 🙂

@fahhem
Copy link
Contributor

fahhem commented Oct 19, 2018

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?

joeleong added a commit to joeleong/python-adb that referenced this issue Jan 6, 2019
joeleong added a commit to joeleong/python-adb that referenced this issue Jul 24, 2019
@0xfk
Copy link

0xfk commented Sep 11, 2021

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants