-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
piexlMax(奇淼
committed
Dec 30, 2024
1 parent
03f8ac6
commit 5a20e93
Showing
6 changed files
with
184 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package muxServeHTTP | ||
|
||
import ( | ||
"fmt" | ||
"github.com/HXSecurity/DongTai-agent-go/model" | ||
"github.com/brahma-adshonor/gohook" | ||
"github.com/go-mux/mux" | ||
) | ||
|
||
func init() { | ||
model.HookMap["muxServeHTTP"] = new(MuxServeHTTP) | ||
} | ||
|
||
type MuxServeHTTP struct { | ||
} | ||
|
||
func (h *MuxServeHTTP) Hook() { | ||
mx := &mux.Router{} | ||
err := gohook.HookMethod(mx, "ServeHTTP", MyServer, MyServerTemp) | ||
if err != nil { | ||
fmt.Println(err, "HttpServeHTTP") | ||
} else { | ||
fmt.Println("HttpServeHTTP") | ||
} | ||
} | ||
|
||
func (h *MuxServeHTTP) UnHook() { | ||
mx := &mux.Router{} | ||
gohook.UnHookMethod(mx, "ServeHTTP") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package muxServeHTTP | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"encoding/base64" | ||
"github.com/HXSecurity/DongTai-agent-go/api" | ||
"github.com/HXSecurity/DongTai-agent-go/global" | ||
"github.com/HXSecurity/DongTai-agent-go/model/request" | ||
"github.com/HXSecurity/DongTai-agent-go/utils" | ||
"github.com/go-mux/mux" | ||
"net/http" | ||
"reflect" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func MyServer(server *mux.Router, w http.ResponseWriter, r *http.Request) { | ||
worker, _ := utils.NewWorker(global.AgentId) | ||
|
||
TraceId := global.TraceId + "-" + strconv.Itoa(int(worker.GetId())) | ||
global.TargetTraceId = TraceId | ||
MyServerTemp(server, w, r) | ||
id := utils.CatGoroutineID() | ||
go func() { | ||
t := reflect.ValueOf(r.Body) | ||
var headerBase string | ||
body := "" | ||
for k, v := range r.Header { | ||
headerBase += k + ": " + strings.Join(v, ",") + "\n" | ||
} | ||
tranceID := TraceId + "." + strconv.Itoa(global.AgentId) + ".0.0.0" | ||
headerBase += "dt-traceid:" + tranceID | ||
if t.Kind() == reflect.Ptr { | ||
buf := t. | ||
Elem(). | ||
FieldByName("src"). | ||
Elem().Elem(). | ||
FieldByName("R"). | ||
Elem().Elem(). | ||
FieldByName("buf").Bytes() | ||
buf = buf[:bytes.IndexByte(buf, 0)] | ||
reader := bufio.NewReader(bytes.NewReader(buf)) | ||
var reqArr []string | ||
for { | ||
line, _, err := reader.ReadLine() | ||
if err != nil { | ||
break | ||
} | ||
reqArr = append(reqArr, string(line)) | ||
} | ||
body = reqArr[len(reqArr)-1] | ||
} | ||
header := base64.StdEncoding.EncodeToString([]byte(headerBase)) | ||
scheme := "http" | ||
if r.TLS != nil { | ||
scheme = "https" | ||
} | ||
onlyKey := int(worker.GetId()) | ||
|
||
HookGroup := &request.UploadReq{ | ||
Type: 36, | ||
InvokeId: onlyKey, | ||
Detail: request.Detail{ | ||
AgentId: global.AgentId, | ||
Function: request.Function{ | ||
Method: r.Method, | ||
Url: scheme + "://" + r.Host + r.RequestURI, | ||
Uri: r.URL.Path, | ||
Protocol: r.Proto, | ||
ClientIp: r.RemoteAddr, | ||
Language: "GO", | ||
ReplayRequest: false, | ||
ReqHeader: header, | ||
ReqBody: body, | ||
QueryString: r.URL.RawQuery, | ||
Pool: []request.Pool{}, | ||
TraceId: tranceID, | ||
}, | ||
}, | ||
} | ||
var resBody string | ||
var resH string | ||
res, ok := global.ResponseMap.Load(id) | ||
if ok { | ||
global.ResponseMap.Delete(id) | ||
resBody = res.(string) | ||
} | ||
value2, ok2 := global.ResponseHeaderMap.Load(id) | ||
if ok2 { | ||
global.ResponseHeaderMap.Delete(id) | ||
resH = value2.(string) | ||
} | ||
for k, v := range w.Header() { | ||
resH += k + ": " + strings.Join(v, ",") + "\n" | ||
} | ||
resHeader := base64.StdEncoding.EncodeToString([]byte(resH)) | ||
HookGroup.Detail.ResHeader = resHeader | ||
HookGroup.Detail.ResBody = resBody | ||
goroutineIDs := make(map[string]bool) | ||
global.PoolTreeMap.Range(func(key, value interface{}) bool { | ||
if value.(*request.PoolTree).IsThisBegin(id) { | ||
global.PoolTreeMap.Delete(key) | ||
value.(*request.PoolTree).FMT(&HookGroup.Detail.Function.Pool, worker, goroutineIDs, HookGroup.Detail.Function.TraceId) | ||
return false | ||
} | ||
return true | ||
}) | ||
api.ReportUpload(*HookGroup) | ||
request.RunMapGCbYGoroutineID(goroutineIDs) | ||
}() | ||
return | ||
} | ||
|
||
func MyServerTemp(server *mux.Router, w http.ResponseWriter, r *http.Request) { | ||
for i := 0; i < 100; i++ { | ||
|
||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package hook | ||
|
||
type Mux struct { | ||
} | ||
|
||
func (h *Mux) GetHook() []string { | ||
return []string{ | ||
"muxServeHTTP", | ||
} | ||
} | ||
|
||
func (h *Mux) HookAll() { | ||
Hook(h.GetHook()) | ||
} | ||
|
||
func (h *Mux) UnHookAll() { | ||
UnHook(h.GetHook()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package http | ||
|
||
import ( | ||
_ "github.com/HXSecurity/DongTai-agent-go/core/mux/muxServerHTTP" | ||
"github.com/HXSecurity/DongTai-agent-go/global" | ||
"github.com/HXSecurity/DongTai-agent-go/hook" | ||
) | ||
|
||
func init() { | ||
h := new(hook.Mux) | ||
global.AllHooks = append(global.AllHooks, h) | ||
h.HookAll() | ||
} |