Giter Site home page Giter Site logo

Root access about adb_shell HOT 22 CLOSED

jefflirion avatar jefflirion commented on July 21, 2024
Root access

from adb_shell.

Comments (22)

JeffLIrion avatar JeffLIrion commented on July 21, 2024

Thanks for the kind words!

That's not "officially" supported, but I took a look at python-adb and I think you should be able to accomplish this via:

from adb_shell.adb_device import AdbDeviceTcp, _AdbTransactionInfo

dev = AdbDeviceTcp(...)

# you could use timeout_s=None and total_timeout_s=10.0
adb_info = _AdbTransactionInfo(None, None, timeout_s, total_timeout_s)

dev._streaming_command(b'root', b'', adb_info)

Please let me know if that works.

from adb_shell.

alonroth avatar alonroth commented on July 21, 2024

Interesting.. Thanks for your quick response.. I'll try it and let you know.

Thanks!

from adb_shell.

alonroth avatar alonroth commented on July 21, 2024

Unfortunately, it's not quite working..
The command itself maybe works but the reason I'm trying to enter the "root mode" is so I could pull data from the "/data/data" path which is only available via root.

Trying to do the following:

adb = AdbDeviceTcp(...)
adb_info = _AdbTransactionInfo(None, None, 10, 10)
b = adb._streaming_command(b'root', b'', adb_info)
response = adb.shell('ls -l /data/data')

still resolved in ls: /data/data: Permission denied as before

from adb_shell.

JeffLIrion avatar JeffLIrion commented on July 21, 2024

Try

# same as above, and then
b''.join(adb._streaming_command(b'root', b'', adb_info))

What is the output of that?

Also, are you able to get root access using the official adb binary?

from adb_shell.

JeffLIrion avatar JeffLIrion commented on July 21, 2024

@alonroth have you had a chance to try

# same as above, and then
b''.join(adb._streaming_command(b'root', b'', adb_info))

And are you able to get root access using the ADB binary?

from adb_shell.

alonroth avatar alonroth commented on July 21, 2024

Hi @JeffLIrion

Sorry for the delay I've been sick for the last couple of days..

I tried the following and it didn't work.:
b''.join(adb._streaming_command(b'root', b'', adb_info))

When I try the offical adb library it kind of works:
adb_commands.Root()

After running this command, I see it's been switched to root when I run on my CLI:
adb shell whoami

Although, I get this exception on my app:
ConnectionResetError: [Errno 54] Connection reset by peer

Currently, I have a workaround to make it work - I use the offical library in order to move to root mode and I catch the above exception so I could continue working with the ADB.

What do you think?

from adb_shell.

JeffLIrion avatar JeffLIrion commented on July 21, 2024

How did you check that b''.join(adb._streaming_command(b'root', b'', adb_info)) didn't work? Was there any output from that command?

from adb_shell.

JeffLIrion avatar JeffLIrion commented on July 21, 2024

The command above that you ran using adb-shell is exactly equivalent to the adb_commands.Root() command in the adb package, so I don't understand why one would work but not the other.

When you connect via CLI (i.e., using the ADB binary), I think that will break the connection established by the adb and/or adb-shell packages. I think that's what caused the ConnectionResetError that you got. Furthermore, I'm not sure that running adb shell whoami via CLI is a valid check that the command run by the adb / adb-shell Python package actually got you root access; you should be able to run dev.shell("whoami") in adb-shell or dev.Shell("whoami") in adb to check this.

If you really want to know what's going on in adb-shell, do

import logging

logging.getLogger().setLevel(logging.DEBUG)

# run the adb-shell commands from above

from adb_shell.

JeffLIrion avatar JeffLIrion commented on July 21, 2024

For reference:

adb

adb-shell

from adb_shell.

JeffLIrion avatar JeffLIrion commented on July 21, 2024

Any updates on this?

from adb_shell.

alonroth avatar alonroth commented on July 21, 2024

I agree, I saw that it's quite the same thing as well, I don't understand either why one works and the other doesn't... I just keep using both of the libraries :)

from adb_shell.

JeffLIrion avatar JeffLIrion commented on July 21, 2024

I pushed out a new release. If you install it via pip install --upgrade adb-shell, you can try to gain root access via:

import logging
logging.getLogger().setLevel(logging.DEBUG)  # show debugging log info

dev = AdbDeviceTcp(...)  # fill in the arguments here
dev._service(b'root', b'')

from adb_shell.

alonroth avatar alonroth commented on July 21, 2024

Interesting, I'll try and keep you posted

from adb_shell.

alonroth avatar alonroth commented on July 21, 2024

@JeffLIrion

Not working for me... :|

  File "ADB.py", line 20, in __init__
    dev._service(b'root', b'')
  File "server/env/lib/python3.7/site-packages/adb_shell/adb_device.py", line 404, in _service
    return b''.join(self._streaming_command(service, command, adb_info)).decode('utf8')
  File "server/env/lib/python3.7/site-packages/adb_shell/adb_device.py", line 1011, in _streaming_command
    self._open(b'%s:%s' % (service, command), adb_info)
  File "server/env/lib/python3.7/site-packages/adb_shell/adb_device.py", line 795, in _open
    self._send(msg, adb_info)
  File "server/env/lib/python3.7/site-packages/adb_shell/adb_device.py", line 980, in _send
    self._handle.bulk_write(msg.pack(), adb_info.timeout_s)
  File "server/env/lib/python3.7/site-packages/adb_shell/handle/tcp_handle.py", line 151, in bulk_write
    _, writeable, _ = select.select([], [self._connection], [], timeout)
TypeError: argument must be an int, or have a fileno() method.

from adb_shell.

JeffLIrion avatar JeffLIrion commented on July 21, 2024

I forgot: you have to connect to the device.

import os

from adb_shell.adb_device import AdbDeviceTcp
from adb_shell.auth.sign_pythonrsa import PythonRSASigner

import logging
logging.getLogger().setLevel(logging.DEBUG)  # show debugging log info

with open(os.path.expanduser('~/.android/adbkey')) as f:  # update the path to the key file accordingly
    priv = f.read()
signer = PythonRSASigner('', priv)

dev = AdbDeviceTcp('192.168.0.222', 5555, default_timeout_s=9.)
dev.connect(rsa_keys=[signer], auth_timeout_s=0.1)

dev._service(b'root', b'')
print(dev.shell('whoami'))

from adb_shell.

JeffLIrion avatar JeffLIrion commented on July 21, 2024

@alonroth have you tried using the latest code that I posted?

from adb_shell.

JeffLIrion avatar JeffLIrion commented on July 21, 2024
import os

from adb_shell.adb_device import AdbDeviceTcp
from adb_shell.auth.sign_pythonrsa import PythonRSASigner

import logging
logging.getLogger().setLevel(logging.DEBUG)  # show debugging log info

with open(os.path.expanduser('~/.android/adbkey')) as f:  # update the path to the key file accordingly
    priv = f.read()
signer = PythonRSASigner('', priv)

dev = AdbDeviceTcp('192.168.0.222', 5555, default_timeout_s=9.)
dev.connect(rsa_keys=[signer], auth_timeout_s=0.1)

dev._service(b'root', b'')
print(dev.shell('whoami'))

@Halastra or @matthieuxyz, can either of you confirm that the above code snippet successfully gets root access? You'll need to be running version 0.1.2 of adb-shell or newer.

from adb_shell.

Halastra avatar Halastra commented on July 21, 2024

I don't have a rooted device at the moment. Sorry.

from adb_shell.

matthieuxyz avatar matthieuxyz commented on July 21, 2024

Neither do I, sorry.

from adb_shell.

dark-lbp avatar dark-lbp commented on July 21, 2024

Test with current master branch.
Device_Info: Android 6.0.1

import os
from adb_shell.adb_device import AdbDeviceTcp
from adb_shell.auth.sign_pythonrsa import PythonRSASigner

import logging
logging.getLogger().setLevel(logging.DEBUG)  # show debugging log info

with open(os.path.expanduser('~/.android/adbkey')) as f:  # update the path to the key file accordingly
    priv = f.read()
signer = PythonRSASigner('', priv)

dev = AdbDeviceTcp('x.x.x.x', 5555, default_timeout_s=9.)
dev.connect(rsa_keys=[signer], auth_timeout_s=0.1)

dev._service(b'root', b'')
print("dev.shell('whoami'): {}".format(dev.shell('whoami')))
print("dev.shell('su -c whoami'): {}".format(dev.shell('su -c whoami')))
>>>dev.shell('whoami'): shell
>>>dev.shell('su -c whoami'): root

from adb_shell.

JeffLIrion avatar JeffLIrion commented on July 21, 2024

@dark-lbp thanks for testing! I'll add a root method and close out this issue.

from adb_shell.

JeffLIrion avatar JeffLIrion commented on July 21, 2024

Closed via #92

from adb_shell.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.