背景 工作中写的一个go代码被安全中心检查出来了pprof漏洞,想要避免这个问题,故做了如下实验。
解决方案1:直接去掉pprof 代码如下 左边的代码没有pprof包,右边的代码有pprof包,两边各起了两个httpServer
分别开两个shell启动进程
1 2 3 4 2024/10/22 06:36:47 Starting server on 0.0.0.0:6010 2024/10/22 06:36:47 Starting server on 0.0.0.0:6011
1 2 3 4 2024/10/22 06:37:05 Starting server on 0.0.0.0:6020 2024/10/22 06:37:05 Starting server on 0.0.0.0:6021
校验结果: 有pprof包的结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 [root@ningan chapter58-httpserver] goroutine profile: total 4 1 @ 0x430fd1 0x467fbd 0x661411 0x661245 0x65dce6 0x66f2e8 0x66fde5 0x61dee9 0x61f802 0x6204ae 0x61cd14 0x46d821 1 @ 0x43bb8e 0x434597 0x468385 0x4b2da7 0x4b38fa 0x4b38e8 0x5581a5 0x562125 0x616c57 0x46d821 1 @ 0x43bb8e 0x434597 0x468385 0x4b2da7 0x4b446c 0x4b445a 0x5592e9 0x56965e 0x568970 0x620904 0x620571 0x6774dd 0x677489 0x43b71b 0x46d821 1 @ 0x43bb8e 0x434597 0x468385 0x4b2da7 0x4b446c 0x4b445a 0x5592e9 0x56965e 0x568970 0x620904 0x620571 0x677745 0x6776ec 0x46d821 [root@ningan chapter58-httpserver] [root@ningan chapter58-httpserver] [root@ningan chapter58-httpserver] [root@ningan chapter58-httpserver] goroutine profile: total 4 1 @ 0x430fd1 0x467fbd 0x661411 0x661245 0x65dce6 0x66f2e8 0x66fde5 0x61dee9 0x61f802 0x6204ae 0x61cd14 0x46d821 1 @ 0x43bb8e 0x434597 0x468385 0x4b2da7 0x4b446c 0x4b445a 0x5592e9 0x56965e 0x568970 0x620904 0x620571 0x6774dd 0x677489 0x43b71b 0x46d821 1 @ 0x43bb8e 0x434597 0x468385 0x4b2da7 0x4b446c 0x4b445a 0x5592e9 0x56965e 0x568970 0x620904 0x620571 0x677745 0x6776ec 0x46d821 1 @ 0x46d821
无pprof包的结果如下:
1 2 3 4 [root@ningan chapter58-httpserver] 404 page not found [root@ningan chapter58-httpserver] 404 page not found
结果证明,只要把引用的pprof包删除,便可以解决这个问题~
解决方案2 正常服务端口改为http.NewServeMux
启动程序
1 go run perfect/main.go -a="0.0.0.0:6030" -b="127.0.0.1:6031"
最终结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 [root@ningan ~] 404 page not found [root@ningan ~] curl: (7) Failed to connect to 192.168.20.201 port 6031: Connection refused [root@ningan ~] goroutine profile: total 4 1 @ 0x433911 0x46f09d 0x678b91 0x6789c5 0x6757eb 0x68590f 0x6863fe 0x637489 0x63930d 0x63a34e 0x6360c8 0x474921 1 @ 0x43e1ce 0x436f37 0x46f465 0x4bcb27 0x4bd67a 0x4bd668 0x5657e5 0x56f745 0x6300f7 0x474921 1 @ 0x43e1ce 0x436f37 0x46f465 0x4bcb27 0x4be1cc 0x4be1ba 0x5668e9 0x57727e 0x5765d0 0x63a77e 0x63a411 0x68d8e7 0x68d87b 0x43dd7d 0x474921 1 @ 0x43e1ce 0x436f37 0x46f465 0x4bcb27 0x4be1cc 0x4be1ba 0x5668e9 0x57727e 0x5765d0 0x63a77e 0x63a411 0x68daaa 0x68da55 0x474921 [root@ningan ~] [root@ningan ~]
参考 源码参考:go-practice/chapter58-httpserver at master · ningan123/go-practice
解决方案参考:你的 pprof 暴露了 - Go语言中文网 - Golang中文社区
深入剖析参考:Go 每日一库之 net/http(基础和中间件) - 大俊的博客