推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
usb
V2EX  ›  Python

Python 在 Android 上如何取得 root 权限以改写系统文件?

  •  
  •   usb ·
    usb233 · Jan 5, 2016 · 8197 views
    This topic created in 3960 days ago, the information mentioned may be changed or developed.
    rt, 都找不到相关资料
    40 replies  •  2016-01-08 10:59:56 +08:00
    tempdban
    tempdban
    Jan 5, 2016 via Android
    1
    要么你 su -c python
    要么 os.system('su -c')
    usb
    usb
    OP
    Jan 5, 2016
    2
    @tempdban os.system('su -c') 后有授权通知,但打开系统文件还是显示无权限
    usb
    usb
    OP
    Jan 5, 2016
    3
    @tempdban os.system('su -c')
    a = open("/system/etc/a","w")
    print("success!")

    print 不出来
    clino
    clino
    Jan 5, 2016
    4
    os.system 是调用一个子进程 并不会对自己有所影响
    clino
    clino
    Jan 5, 2016
    5
    照楼上的说应该是 os.system('su -c cat /system/etc/a')
    usb
    usb
    OP
    Jan 5, 2016
    6
    @clino 那应该怎么办?
    usb
    usb
    OP
    Jan 5, 2016
    7
    @clino 嗯嗯。等我试试
    tempdban
    tempdban
    Jan 5, 2016 via Android
    8
    @clinoThat's right .
    我想让他琢磨一下,结果感觉还是太早了。
    usb
    usb
    OP
    Jan 5, 2016
    9
    @clino @tempdban 还是 error30
    tempdban
    tempdban
    Jan 5, 2016 via Android
    10
    @usb su -c 是用别的用户执行 c 后边跟的命令,不是说执行完你就是特权用户了。
    usb
    usb
    OP
    Jan 5, 2016
    11
    @tempdban 那应该把代码写到-c 后面吗?新手求不喷😁
    usb
    usb
    OP
    Jan 5, 2016
    12
    @tempdban '(' unexpected
    ChiChou
    ChiChou
    Jan 5, 2016
    13
    su -c python xxx.py
    usb
    usb
    OP
    Jan 5, 2016
    14
    @ChiChou testpy.py:
    (can't execute: Permission denied)
    root 权限都给了啊
    usb
    usb
    OP
    Jan 5, 2016
    15
    @ChiChou
    (testpy.py: can't execute: Permission denied)
    ChiChou
    ChiChou
    Jan 5, 2016
    16
    @usb python xxx.py ,不是直接 xxx.py 。 xxx.py 需要给文件 chmod 0777 ,然后在 python 开头第一行加上 #!/path/to/pyhon ( python 的完整路径)
    tempdban
    tempdban
    Jan 6, 2016 via Android
    17
    import sys,os
    args = [sys.executable] + sys.argv
    os.execlp('su', 'su', '-c', ' '.join(args))
    usb
    usb
    OP
    Jan 6, 2016
    18
    @ChiChou
    我有写 python 啊~ 那是终端显示的
    tempdban
    tempdban
    Jan 6, 2016 via Android
    19
    @usb 看我上面回复
    usb
    usb
    OP
    Jan 6, 2016
    20
    @tempdban
    这次是
    CANNOT LINK EXECUTABLE: library "libpython3.2m.so.1.0" not found
    tempdban
    tempdban
    Jan 6, 2016 via Android
    21
    @usb 你用的什么 app 啊这是
    tempdban
    tempdban
    Jan 6, 2016 via Android
    22
    @usb 莫非是直接调用的 libpython ,而不是用的 interpreter
    usb
    usb
    OP
    Jan 6, 2016
    23
    @tempdban 安卓上只有 qpython 呢
    tempdban
    tempdban
    Jan 6, 2016 via Android❤️1
    24
    @usb 谁说的一号我刚编译了 pyrhon2.7 armeabi bionic
    usb
    usb
    OP
    Jan 6, 2016
    25
    @tempdban 你厉害_(:з」∠)_
    tempdban
    tempdban
    Jan 6, 2016 via Android
    26
    @usb 不是这个意思,你的环境限制你只能从终端读取字符
    usb
    usb
    OP
    Jan 6, 2016
    27
    @tempdban 呃呃,也就是说 Python 脚本在 Android 上不能取得 root 权限?
    tempdban
    tempdban
    Jan 6, 2016 via Android
    28
    @usb os.system('su -c python')能执行么
    usb
    usb
    OP
    Jan 6, 2016
    29
    @tempdban
    error: only position independent executables (PIE) are supported.
    呃呃
    WayToPlay
    WayToPlay
    Jan 6, 2016
    30
    要 root android ,以 linux root 执行 python 干嘛啊,直接先执行 adb root && adb reboot 就行了吧。
    如果 adb 没有权限,先在 root 下 adb kill-server 。
    shyling
    shyling
    Jan 6, 2016
    31
    通过 popen 执行 su -,然后会启动一个 root 权限的 shell ,通过这个 shell 操作。。其他的方法的话。。
    通过 shell 设置你的二进制文件 setuid 位,然后通过 os.setuid(0)。不过估计不太可行。。
    usb
    usb
    OP
    Jan 6, 2016
    32
    @WayToPlay 你可能误解了,我是想用 Python 脚本修改系统文件的
    usb
    usb
    OP
    Jan 6, 2016
    33
    @shyling 其实我是想用 Python 来自动化改 hosts 文件的,每次都 shell 不是更麻烦了吗 Q3Q
    tempdban
    tempdban
    Jan 6, 2016 via Android
    34
    @usb 你看,qpython 是调用 libpython 实现的功能,这种方式都比较麻烦,需要调用 libpython 的进程属主是 su 用户,这个属主应该是 app_process
    tempdban
    tempdban
    Jan 6, 2016 via Android
    35
    @usb 所以……还是用 shell 吧,正好学学 shell 脚本,再下载一个叫 tasker 的东西
    tempdban
    tempdban
    Jan 6, 2016 via Android
    36
    @usb 或者你和 qpython 提意见 让他们用 pie 编译 python
    usb
    usb
    OP
    Jan 6, 2016
    37
    @tempdban 不太明白😁那还有办法吗?
    usb
    usb
    OP
    Jan 6, 2016
    38
    @tempdban 太麻烦了。没动力学😂
    shyling
    shyling
    Jan 8, 2016 via iPad
    39
    @usb 写一个二进制文件做更新 hosts 的操作。。 然后用 su -c 执行
    usb
    usb
    OP
    Jan 8, 2016
    40
    @shyling 这。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1607 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 12:58 · PVG 20:58 · LAX 04:58 · JFK 07:58
    ♥ Do have faith in what you're doing.