Reptile Reading

目录结构

  1. Reptile-master
  2. ├── configs
  3. └── defconfig
  4. ├── Kconfig
  5. ├── kernel
  6. ├── backdoor.c # done
  7. ├── dir.c # done
  8. ├── encrypt
  9. └── encrypt.c # done rotate 13 算法加密
  10. ├── file.c # done
  11. ├── include
  12. ├── backdoor.h # done
  13. ├── config.h
  14. ├── dir.h # done
  15. ├── encrypt.h # done
  16. ├── file.h # done
  17. ├── module.h # done
  18. ├── network.h # done
  19. ├── proc.h # done
  20. ├── string_helpers.h # done
  21. └── util.h # done
  22. ├── Kbuild
  23. ├── khook
  24. ├── engine.c
  25. ├── engine.h
  26. ├── engine.lds
  27. ├── internal.h
  28. └── x86
  29. ├── hook.c
  30. ├── Makefile
  31. ├── README.md
  32. ├── stub32.inc
  33. ├── stub.inc
  34. └── stub.S
  35. ├── kmatryoshka
  36. ├── Kbuild
  37. └── kmatryoshka.c
  38. ├── loader
  39. └── loader.c # done
  40. ├── main.c #
  41. ├── module.c # done
  42. ├── network.c # done
  43. ├── proc.c # done
  44. ├── string_helpers.c
  45. └── util.c # done
  46. ├── Makefile
  47. ├── scripts
  48. ├── bashrc
  49. ├── destringify.pl
  50. ├── installer.sh
  51. ├── kconfig
  52. ├── check.sh
  53. ├── conf.c
  54. ├── confdata.c
  55. ├── config.sh
  56. ├── expr.c
  57. ├── expr.h
  58. ├── foo.h
  59. ├── gconf.c
  60. ├── gconf.glade
  61. ├── GNUmakefile
  62. ├── images.c
  63. ├── kxgettext.c
  64. ├── list.h
  65. ├── lkc.h
  66. ├── lkc_proto.h
  67. ├── lxdialog
  68. ├── BIG.FAT.WARNING
  69. ├── checklist.c
  70. ├── check-lxdialog.sh
  71. ├── dialog.h
  72. ├── inputbox.c
  73. ├── menubox.c
  74. ├── textbox.c
  75. ├── util.c
  76. └── yesno.c
  77. ├── Makefile
  78. ├── Makefile.br
  79. ├── mconf.c
  80. ├── menu.c
  81. ├── merge_config.sh
  82. ├── nconf.c
  83. ├── nconf.gui.c
  84. ├── nconf.h
  85. ├── patches
  86. ├── 01-kconfig-kernel-to-buildroot.patch
  87. ├── 06-br-build-system-integration.patch
  88. ├── 100-kconfig-generic-env.patch
  89. ├── 101-kconfig-build.patch
  90. ├── 10-br-build-system.patch
  91. ├── 11-use-mktemp-for-lxdialog.patch
  92. ├── 12-fix-glade-file-path.patch
  93. ├── 14-support-out-of-tree-config.patch
  94. ├── 15-fix-qconf-moc-rule.patch
  95. ├── 16-fix-space-to-de-select-options.patch
  96. └── series
  97. ├── POTFILES.in
  98. ├── qconf.cc
  99. ├── qconf.h
  100. ├── README.buildroot
  101. ├── streamline_config.pl
  102. ├── symbol.c
  103. ├── util.c
  104. ├── zconf.gperf
  105. ├── zconf.hash.c_shipped
  106. ├── zconf.l
  107. ├── zconf.lex.c_shipped
  108. ├── zconf.tab.c_shipped
  109. └── zconf.y
  110. ├── lib
  111. └── Unescape.pm
  112. ├── random.sh
  113. ├── rule
  114. └── start
  115. └── userland
  116. ├── client
  117. ├── client.c
  118. ├── listener.c
  119. └── packet.c
  120. ├── cmd.c
  121. ├── crypto
  122. ├── aes.c
  123. └── sha1.c
  124. ├── include
  125. ├── aes.h
  126. ├── config.h
  127. ├── custom_rol32.h
  128. ├── pel.h
  129. ├── sha1.h
  130. └── util.h
  131. ├── Makefile
  132. ├── shell.c
  133. └── transport
  134. └── pel.c

kernel

  1. kernel/
  2. ├── backdoor.c # done
  3. ├── dir.c # done
  4. ├── encrypt
  5. └── encrypt.c # rol32加密
  6. ├── file.c # this module can't work properly
  7. ├── include
  8. ├── backdoor.h # done
  9. ├── config.h # done
  10. ├── dir.h # done
  11. ├── encrypt.h # done
  12. ├── file.h # done
  13. ├── module.h # done
  14. ├── network.h # done
  15. ├── proc.h # done
  16. ├── string_helpers.h
  17. └── util.h
  18. ├── Kbuild
  19. ├── khook # 第三方钩子模板 khook
  20. ├── engine.c
  21. ├── engine.h
  22. ├── engine.lds
  23. ├── internal.h
  24. └── x86
  25. ├── hook.c
  26. ├── Makefile
  27. ├── README.md
  28. ├── stub32.inc
  29. ├── stub.inc
  30. └── stub.S
  31. ├── kmatryoshka # 第三方库 kmatryoshka LKM loader
  32. ├── Kbuild
  33. └── kmatryoshka.c
  34. ├── loader
  35. └── loader.c
  36. ├── main.c
  37. ├── module.c # done
  38. ├── network.c # done
  39. ├── proc.c # done
  40. ├── string_helpers.c
  41. └── util.c
  • 感觉这种堆叠也挺好的,开发快速,前提是看过和用过的东西要多

主逻辑分析

关键函数分析

技术分析

  1. make config(kconfig)
    1. 探索 Linux 内核:Kconfig/kbuild 的秘密
    2. Kbuild
    3. Linux内核编译系统kbuild简介

总结

参考资料

内核态进程管理简解

(1/3)Khook代码分析

kconfig

  1. https://blog.csdn.net/jianwen_hi/article/details/53398141
  2. https://blog.csdn.net/whatday/article/details/102095114
  3. https://blog.csdn.net/y24283648/article/details/108608966
  4. https://blog.csdn.net/liangdapo/article/details/43699655

reptile 代码分析

内核函数

  • [call_usermodehelper(char *path, char **argv, char **envp, enum umh_wait wait);](https://www.cnblogs.com/embedded-linux/p/7439984.html)
    linux kernel提供了call_usermodehelper,用于内核中直接新建和运行用户空间程序,并且该程序具有root权限。
  • void *skb_header_pointer(const struct sk_buff *skb, int offset, int len, void *buffer)
    • skb_header_pointer这个函数的主要功能很简单,就是从skb字段中获取指定长度到内容到缓存中。
  • memzero_explicit
  • argv_split
  • ftell、fseek、rewind
  • memmove
  • proc部分
    • rcu_read_lock、rcu_read_unlock
    • find_get_pid、put_pid
    • get_pid_task、put_task_struct: 释放进程内核栈和thread_info结构所占的页,释放task_struct占用的slab高速缓存
    • for_each_process
  • kstrtoint
  • strncpy_from_user
  • kallsyms_on_each_symbol
  • module部分
    • mutex_trylock
    • mutex_unlock
      • module_mutex这个锁在哪
    • cpu_relax
    • THIS_MODULE
  • util部分
    • ksym_lookup_name 符号中查找函数
    • access_process_vm
    • get_task_mm、mmput
    • down_read、up_read
  • kmatryoshka
    • __builtin_alloca
    • current_thread_info()->addr_limit.seg
    • kallsyms_on_each_symbol
    • roundup
    • PAGE_SIZE

内核结构体、宏

  • work_struct

有点类似于struct list,都是基础辅助结构体,我们只要拿到这个结构体,放入自己的结构体中(加入自己想要做的事情),使用特定函数头的函数,就可以使用对应的几个宏去完成自定义的任务的内核调度

内核里一直运行类似worker thread,它会对工作队列中的work进行处理,大致的工作流程原理可以参考下图所示:

Reptile - 图1

在这里的work则是work_struct变量,并且绑定一个执行函数——typedef void (*work_func_t)(struct work_struct *work);。在worker thread中会对非空的工作队列进行工作队列的出队操作,并运行work绑定的函数。

函数 功能
INIT_WORK(_work, _func) 初始化一个work
INIT_WORK_ONSTACK(_work, _func) 在栈上初始化一个work
flush_work(struct work_struct *work); 销毁一个work
schedule_work(struct work_struct *work) 调度一个work开始运行
  • task_struct
  • GFP_KERNEL、GFP_ATOMIC
  • sk_buff
  • iphdr
  • icmphdr
  • tcphdr
  • udphdr
  • struct pid
    • find_get _pid
    • get_pid_task
    • put_task_struct
    • put_pid
  • gfp_t
  • mm_struct
  • typeof 此关键字返回变量的类型,可用于变量声明
  • __attribute__((section(".data.khook")))表示分配到.data.khook
  • ldflags-y += -T$(src)/khook/engine.lds
  1. SECTIONS
  2. {
  3. .data : {
  4. KHOOK_tbl = . ; # . 表示当前定位器符号的位置,所以KHOOK_tbl指的.data.khook开头
  5. *(.data.khook)
  6. KHOOK_tbl_end = . ; # KHOOK_tbl_end指的.data.khook结尾
  7. }
  8. }
  • 这一坨没完全分析清楚,只说明是stub填充物(这个STUB会被初始化为stub.inc或stub32.inc。也就是stub的模板)
  1. typedef struct {
  2. // 摆上了16字节(计数)、32字节(原函数地址)、64字节(hook地址)
  3. #pragma pack(push, 1)
  4. union {
  5. unsigned char _0x00_[ 0x10 ];
  6. atomic_t use_count;
  7. };
  8. union {
  9. unsigned char _0x10_[ 0x20 ];
  10. unsigned char orig[0];
  11. };
  12. union {
  13. unsigned char _0x30_[ 0x40 ];
  14. unsigned char hook[0];
  15. };
  16. #pragma pack(pop)
  17. // 这是什么类型??
  18. unsigned nbytes;
  19. } __attribute__((aligned(32))) khook_stub_t;