• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

gaffe23/linux-inject: Tool for injecting a shared object into a Linux process

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

gaffe23/linux-inject

开源软件地址(OpenSource Url):

https://github.com/gaffe23/linux-inject

开源编程语言(OpenSource Language):

C 97.5%

开源软件介绍(OpenSource Introduction):

linux-inject

Tool for injecting a shared object into a Linux process

  • Provides the Linux equivalent of using CreateRemoteThread() on Windows to inject a DLL into a running process

  • Performs injection using ptrace() rather than LD_PRELOAD, since the target process is already running at the time of injection

  • Supports x86, x86_64, and ARM

  • Does not require the target process to have been built with -ldl flag, because it loads the shared object using __libc_dlopen_mode() from libc rather than dlopen() from libdl

Caveat about ptrace()

  • On many Linux distributions, the kernel is configured by default to prevent any process from calling ptrace() on another process that it did not create (e.g. via fork()).

  • This is a security feature meant to prevent exactly the kind of mischief that this tool causes.

  • You can temporarily disable it until the next reboot using the following command:

      echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
    

Compiling

  • Simply running make should automatically select and build for the correct architecture, but if this fails (or you would like to select the target manually), run one of the following make commands:

    • arm:

        make arm
      
    • x86:

        make x86
      
    • x86_64:

        make x86_64
      

Usage

./inject [-n process-name] [-p pid] [library-to-inject]

Sample

  • In one terminal, start up the sample target app, which simply outputs "sleeping..." each second:

      ./sample-target
    
  • In another terminal, inject sample-library.so into the target app:

      ./inject -n sample-target sample-library.so
    
  • The output should look something like this:

  • First terminal:

         $ ./sample-target
         sleeping...
         sleeping...
         I just got loaded
         sleeping...
         sleeping...
    
  • Second terminal:

         $ ./inject -n sample-target sample-library.so
         targeting process "sample-target" with pid 31490
         library "sample-library.so" successfully injected
         $
    
  • If the injection fails, make sure your machine is configured to allow processes to ptrace() other processes that they did not create. See the "Caveat about ptrace()" section above.

  • You can verify that the injection was successful by checking /proc/[pid]/maps:

      $ cat /proc/$(pgrep sample-target)/maps
      [...]
      7f37d5cc6000-7f37d5cc7000 r-xp 00000000 ca:01 267321                     /home/ubuntu/linux-inject/sample-library.so
      7f37d5cc7000-7f37d5ec6000 ---p 00001000 ca:01 267321                     /home/ubuntu/linux-inject/sample-library.so
      7f37d5ec6000-7f37d5ec7000 r--p 00000000 ca:01 267321                     /home/ubuntu/linux-inject/sample-library.so
      7f37d5ec7000-7f37d5ec8000 rw-p 00001000 ca:01 267321                     /home/ubuntu/linux-inject/sample-library.so
      [...]
    
  • You can also attach gdb to the target app and run info sharedlibrary to see what shared libraries the process currently has loaded:

      $ gdb -p $(pgrep sample-target)
      [...]
      (gdb) info sharedlibrary
      From                To                  Syms Read   Shared Object Library
      0x00007f37d628ded0  0x00007f37d628e9ce  Yes         /lib/x86_64-linux-gnu/libdl.so.2
      0x00007f37d5ee74a0  0x00007f37d602c583  Yes         /lib/x86_64-linux-gnu/libc.so.6
      0x00007f37d6491ae0  0x00007f37d64ac4e0  Yes         /lib64/ld-linux-x86-64.so.2
      0x00007f37d5cc6670  0x00007f37d5cc67b9  Yes         /home/ubuntu/linux-inject/sample-library.so
      (gdb)
    

Compatibility

  • The x86 and x86_64 versions work on Ubuntu 14.04.02 x86_64.

  • The x86 and x86_64 versions work on Arch x86_64.

  • The ARM version works on Arch on both armv6 and armv7.

  • None of the versions seem to work on Debian. __libc_dlopen_mode() in Debian's libc does not load shared libraries in the same manner as Arch's and Ubuntu's versions do. I tested this on both x86_64 and armv6.

TODOs / Known Issues

  • Better support for targeting multi-thread/multi-process apps

  • I seem to get crashes when trying to inject into larger applications

  • Needs further investigation

  • Support both ARM and Thumb mode

  • Currently only supports ARM mode

  • Should just be a matter of checking LSB of PC and acting accordingly

  • Do better checking to verify that the specified shared object has actually been injected into the target process

  • Check /proc/[pid]/maps rather than just looking at the return value of __libc_dlopen_mode()

  • Support more distros

  • Currently only working on Ubuntu and Arch for certain architectures

  • See "Compatibility" section above

  • Possibly support more architectures?

  • 64-bit ARM

  • MIPS




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap