Skip to content

Commit 5a20e93

Browse files
author
piexlMax(奇淼
committed
增加mux hook
1 parent 03f8ac6 commit 5a20e93

File tree

6 files changed

+184
-2
lines changed

6 files changed

+184
-2
lines changed

core/mux/muxServerHTTP/install.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package muxServeHTTP
2+
3+
import (
4+
"fmt"
5+
"github.com/HXSecurity/DongTai-agent-go/model"
6+
"github.com/brahma-adshonor/gohook"
7+
"github.com/go-mux/mux"
8+
)
9+
10+
func init() {
11+
model.HookMap["muxServeHTTP"] = new(MuxServeHTTP)
12+
}
13+
14+
type MuxServeHTTP struct {
15+
}
16+
17+
func (h *MuxServeHTTP) Hook() {
18+
mx := &mux.Router{}
19+
err := gohook.HookMethod(mx, "ServeHTTP", MyServer, MyServerTemp)
20+
if err != nil {
21+
fmt.Println(err, "HttpServeHTTP")
22+
} else {
23+
fmt.Println("HttpServeHTTP")
24+
}
25+
}
26+
27+
func (h *MuxServeHTTP) UnHook() {
28+
mx := &mux.Router{}
29+
gohook.UnHookMethod(mx, "ServeHTTP")
30+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package muxServeHTTP
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"encoding/base64"
7+
"github.com/HXSecurity/DongTai-agent-go/api"
8+
"github.com/HXSecurity/DongTai-agent-go/global"
9+
"github.com/HXSecurity/DongTai-agent-go/model/request"
10+
"github.com/HXSecurity/DongTai-agent-go/utils"
11+
"github.com/go-mux/mux"
12+
"net/http"
13+
"reflect"
14+
"strconv"
15+
"strings"
16+
)
17+
18+
func MyServer(server *mux.Router, w http.ResponseWriter, r *http.Request) {
19+
worker, _ := utils.NewWorker(global.AgentId)
20+
21+
TraceId := global.TraceId + "-" + strconv.Itoa(int(worker.GetId()))
22+
global.TargetTraceId = TraceId
23+
MyServerTemp(server, w, r)
24+
id := utils.CatGoroutineID()
25+
go func() {
26+
t := reflect.ValueOf(r.Body)
27+
var headerBase string
28+
body := ""
29+
for k, v := range r.Header {
30+
headerBase += k + ": " + strings.Join(v, ",") + "\n"
31+
}
32+
tranceID := TraceId + "." + strconv.Itoa(global.AgentId) + ".0.0.0"
33+
headerBase += "dt-traceid:" + tranceID
34+
if t.Kind() == reflect.Ptr {
35+
buf := t.
36+
Elem().
37+
FieldByName("src").
38+
Elem().Elem().
39+
FieldByName("R").
40+
Elem().Elem().
41+
FieldByName("buf").Bytes()
42+
buf = buf[:bytes.IndexByte(buf, 0)]
43+
reader := bufio.NewReader(bytes.NewReader(buf))
44+
var reqArr []string
45+
for {
46+
line, _, err := reader.ReadLine()
47+
if err != nil {
48+
break
49+
}
50+
reqArr = append(reqArr, string(line))
51+
}
52+
body = reqArr[len(reqArr)-1]
53+
}
54+
header := base64.StdEncoding.EncodeToString([]byte(headerBase))
55+
scheme := "http"
56+
if r.TLS != nil {
57+
scheme = "https"
58+
}
59+
onlyKey := int(worker.GetId())
60+
61+
HookGroup := &request.UploadReq{
62+
Type: 36,
63+
InvokeId: onlyKey,
64+
Detail: request.Detail{
65+
AgentId: global.AgentId,
66+
Function: request.Function{
67+
Method: r.Method,
68+
Url: scheme + "://" + r.Host + r.RequestURI,
69+
Uri: r.URL.Path,
70+
Protocol: r.Proto,
71+
ClientIp: r.RemoteAddr,
72+
Language: "GO",
73+
ReplayRequest: false,
74+
ReqHeader: header,
75+
ReqBody: body,
76+
QueryString: r.URL.RawQuery,
77+
Pool: []request.Pool{},
78+
TraceId: tranceID,
79+
},
80+
},
81+
}
82+
var resBody string
83+
var resH string
84+
res, ok := global.ResponseMap.Load(id)
85+
if ok {
86+
global.ResponseMap.Delete(id)
87+
resBody = res.(string)
88+
}
89+
value2, ok2 := global.ResponseHeaderMap.Load(id)
90+
if ok2 {
91+
global.ResponseHeaderMap.Delete(id)
92+
resH = value2.(string)
93+
}
94+
for k, v := range w.Header() {
95+
resH += k + ": " + strings.Join(v, ",") + "\n"
96+
}
97+
resHeader := base64.StdEncoding.EncodeToString([]byte(resH))
98+
HookGroup.Detail.ResHeader = resHeader
99+
HookGroup.Detail.ResBody = resBody
100+
goroutineIDs := make(map[string]bool)
101+
global.PoolTreeMap.Range(func(key, value interface{}) bool {
102+
if value.(*request.PoolTree).IsThisBegin(id) {
103+
global.PoolTreeMap.Delete(key)
104+
value.(*request.PoolTree).FMT(&HookGroup.Detail.Function.Pool, worker, goroutineIDs, HookGroup.Detail.Function.TraceId)
105+
return false
106+
}
107+
return true
108+
})
109+
api.ReportUpload(*HookGroup)
110+
request.RunMapGCbYGoroutineID(goroutineIDs)
111+
}()
112+
return
113+
}
114+
115+
func MyServerTemp(server *mux.Router, w http.ResponseWriter, r *http.Request) {
116+
for i := 0; i < 100; i++ {
117+
118+
}
119+
return
120+
}

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/StackExchange/wmi v1.2.1 // indirect
1717
github.com/elazarl/goproxy v0.0.0-20210801061803-8e322dfb79c4 // indirect
1818
github.com/go-chi/chi/v5 v5.0.7
19+
github.com/go-mux/mux v1.0.0
1920
github.com/gorilla/rpc v1.2.0
2021
github.com/jinzhu/now v1.1.3 // indirect
2122
github.com/json-iterator/go v1.1.11 // indirect
@@ -32,5 +33,3 @@ require (
3233
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
3334
moul.io/http2curl v1.0.0 // indirect
3435
)
35-
36-

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ github.com/gin-gonic/gin v1.7.7 h1:3DoBmSbJbZAWqXJC3SLjAPfutPJJRN1U5pALB7EeTTs=
3535
github.com/gin-gonic/gin v1.7.7/go.mod h1:axIBovoeJpVj8S3BwE0uPMTeReE4+AfFtqpqaZ1qq1U=
3636
github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8=
3737
github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
38+
github.com/go-mux/mux v1.0.0 h1:cjme5KED7XU2D1XPqfepZpr/ouRoa8St5/Q1Wn5/K7s=
39+
github.com/go-mux/mux v1.0.0/go.mod h1:WycrREmM6C8dXy3aKkr8IQ1XwEqXSsQUm1Hyvs8X0Co=
3840
github.com/go-ole/go-ole v1.2.5 h1:t4MGB5xEDZvXI+0rMjjsfBsD7yAgp/s9ZDkL1JndXwY=
3941
github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
4042
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=

hook/mux.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package hook
2+
3+
type Mux struct {
4+
}
5+
6+
func (h *Mux) GetHook() []string {
7+
return []string{
8+
"muxServeHTTP",
9+
}
10+
}
11+
12+
func (h *Mux) HookAll() {
13+
Hook(h.GetHook())
14+
}
15+
16+
func (h *Mux) UnHookAll() {
17+
UnHook(h.GetHook())
18+
}

run/mux/hookMux.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package http
2+
3+
import (
4+
_ "github.com/HXSecurity/DongTai-agent-go/core/mux/muxServerHTTP"
5+
"github.com/HXSecurity/DongTai-agent-go/global"
6+
"github.com/HXSecurity/DongTai-agent-go/hook"
7+
)
8+
9+
func init() {
10+
h := new(hook.Mux)
11+
global.AllHooks = append(global.AllHooks, h)
12+
h.HookAll()
13+
}

0 commit comments

Comments
 (0)