码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • golang程序能不能用LD_PRELOAD进行hook?


    初步的答案是不能。

    高级一点能行的方法在最后的链接里。

    golang自己实现了相关的libc里面的函数,LD_PRELOAD环境变量其实是 libc里面的功能,可以在加载函数时,优先调用 LD_PRELOAD指定的so里面的函数。

    可以用以下代码进行测试:

    1. #define _GNU_SOURCE
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. typedef int (*orig_socket_func_type)(int domain, int type, int protocol);
    12. typedef int (*orig_accept_func_type)(int sockfd,struct sockaddr *addr,socklen_t *addrlen);
    13. typedef int (*orig_bind_func_type)(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
    14. typedef int (*orig_listen_func_type)(int sockfd, int backlog);
    15. const int DEFAULT_PRIORITY = 6;
    16. int bind(int fd, const struct sockaddr *addr,
    17. socklen_t addrlen)
    18. {
    19. int reuse = 1;
    20. int ret = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &reuse, sizeof(reuse));
    21. if(ret<0)
    22. {
    23. printf("setsockopt SO_REUSEPORT failed. result:%d, err:%s\n", ret, strerror(errno));
    24. }
    25. printf("listen bind ok.\n");
    26. orig_bind_func_type orig_func;
    27. orig_func = (orig_bind_func_type)dlsym(RTLD_NEXT, "bind");
    28. return orig_func(fd, addr, addrlen);
    29. }
    30. int listen(int fd, int backlog)
    31. {
    32. int reuse = 1;
    33. int ret = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &reuse, sizeof(reuse));
    34. if(ret<0)
    35. {
    36. printf("setsockopt SO_REUSEPORT failed. result:%d, err:%s\n", ret, strerror(errno));
    37. }
    38. printf("listen hook ok.\n");
    39. orig_listen_func_type orig_func;
    40. orig_func = (orig_listen_func_type)dlsym(RTLD_NEXT, "listen");
    41. return orig_func(fd, backlog);
    42. }

    Makefile

    1. CFLAGS += -std=c99
    2. default: prsocket.so
    3. prsocket.so: prsocket.c
    4. gcc $(CFLAGS) -shared -fPIC prsocket.c -o prsocket.so -ldl
    5. clean:
    6. rm *.so

    运行nc ,可以看到,hook 是正常工作的。

    1. $> LD_PRELOAD=./prsocket.so nc -l 9000
    2. socket hook ok.
    3. listen bind ok.
    4. listen hook ok.

    用golang 进行测试。

    最简单的http服务。 http.go

    1. package main
    2. import (
    3. "fmt"
    4. "net/http"
    5. )
    6. func main() {
    7. http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
    8. fmt.Fprint(writer, "Hello world")
    9. })
    10. server := http.Server{
    11. Addr: ":9091",
    12. }
    13. fmt.Println("Starting server")
    14. if err := server.ListenAndServe(); err != nil {
    15. panic(err)
    16. }
    17. }
    1. go build http.go 
    2. LD_PRELOAD=./prsocket.so  ./http

    完全没有hook上。

    这个链接说可以用Rust写的程序实现对golang的hook,谁懂的可以参考一下。

    文章链接:Hooking Go from Rust - Hitchhiker’s Guide to the Go-laxy MetalBear 🐻 - Tools for Backend EngineersHow did we hook Go functions from Rust to work with mirrord? A quick dive into the Go Runtime and switching from the Go stack to the system stack.https://metalbear.co/blog/hooking-go-from-rust-hitchhikers-guide-to-the-go-laxy/Golang doesn’t use libc on Linux, and instead calls syscalls directly. This is mostly harmless for the common developer - they don’t care about the assembly, syscalls, linkage, etc - they just want their binary to work. Therefore, being self-contained provides a very good user experience, as Go applications aren’t dependent on the local machine’s libc.

    从作者的这个项目来看,他已经实现了对golang的hook,并且应用到了实际的应用中。

    GitHub - metalbear-co/mirrord: Connect your local process and your cloud environment, and run local code in cloud conditions.Connect your local process and your cloud environment, and run local code in cloud conditions. - GitHub - metalbear-co/mirrord: Connect your local process and your cloud environment, and run local code in cloud conditions.https://github.com/metalbear-co/mirrord

    其他参考资料:

    ubuntu - libfaketime doesn't work with golang - Stack Overflow

    Go compiler produces dynamically linked binaries by default (unless instructed otherwise). Just do ldd and you will see that the binary is linked to libc.so (tested on Linux). The difference is that the go packages it depends on, are bundled in the binary. But not the system libraries. 

    – jimis

  • 相关阅读:
    目标框选之单阶段与两阶段目标检测区别
    2022年最新四川建筑八大员(劳务员)模拟题库及答案
    【接口设计注意点】
    OS操作系统——文件管理(测试习题)
    [Python急救站]基于Transformer Models模型完成GPT2的学生AIGC学习训练模型
    2022年最新西藏机动车签字授权人模拟考试及答案
    IDEA版SSM入门到实战(Maven+MyBatis+Spring+SpringMVC) -Maven核心概念
    java SSM农产品订购网站系统myeclipse开发mysql数据库springMVC模式java编程计算机网页设计
    学生邮箱白嫖/免费安装JetBrains全家桶(IDEA/pycharm等) —— 保姆级教程
    latex线上编译器以及三线表代码
  • 原文地址:https://blog.csdn.net/v6543210/article/details/130881262
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号