From 24234e8775aec8fec55567ce8ff1a7ec201227bb Mon Sep 17 00:00:00 2001 From: Swan Date: Tue, 6 Aug 2024 15:58:24 -0400 Subject: [PATCH] Fix bug with libs --- handlers/ClientPong.go | 33 +++++++++++++++++++++++++++------ packets/ClientPong.go | 13 +++++++------ sessions/user.go | 19 +++++++++++++++++++ webhooks/webhook.go | 10 ++++++++++ 4 files changed, 63 insertions(+), 12 deletions(-) diff --git a/handlers/ClientPong.go b/handlers/ClientPong.go index d81ad37..766c25e 100644 --- a/handlers/ClientPong.go +++ b/handlers/ClientPong.go @@ -17,22 +17,25 @@ func handleClientPong(user *sessions.User, packet *packets.ClientPong) { user.SetLastPongTimestamp() - packetProcs := packet.ParseProcessList() + parsed := packet.Parse() - if packetProcs == nil || len(packetProcs) == 0 { - // webhooks.SendAntiCheatProcessLog(user.Info.Username, user.Info.Id, user.Info.GetProfileUrl(), user.Info.AvatarUrl.String, []string{"NO PROCESSES PROVIDED"}) - // log.Printf("[%v - %v] Sent a Pong packet without any process list\n", user.Info.Id, user.Info.Username) + checkProcesses(user, parsed.Processes) + checkLibraries(user, parsed.Libraries) +} + +func checkProcesses(user *sessions.User, processes []packets.Process) { + if processes == nil || len(processes) == 0 { return } - dbProcs, err := db.FetchProcesses() + dbProcesses, err := db.FetchProcesses() if err != nil { log.Printf("Failed to fetch process from database - %v\n", err) return } - detected := detectProcesses(dbProcs, packetProcs) + detected := detectProcesses(dbProcesses, processes) if len(detected) == 0 { user.SetLastDetectedProcesses([]string{}) @@ -44,11 +47,29 @@ func handleClientPong(user *sessions.User, packet *packets.ClientPong) { } user.SetLastDetectedProcesses(detected) + webhooks.SendAntiCheatProcessLog(user.Info.Username, user.Info.Id, user.Info.GetProfileUrl(), user.Info.AvatarUrl.String, detected) log.Printf("[%v - #%v] Detected %v flagged processes \n", user.Info.Username, user.Info.Id, len(detected)) } +func checkLibraries(user *sessions.User, libraries []string) { + if libraries == nil || len(libraries) == 0 { + return + } + + if slices.Equal(libraries, user.GetLastLibraries()) { + return + } + + user.SetLastLibraries(libraries) + + webhooks.SendAntiCheatLibraries(user.Info.Username, user.Info.Id, user.Info.GetProfileUrl(), + user.Info.AvatarUrl.String, libraries) + + log.Printf("[%v - #%v] Detected %v libraries \n", user.Info.Username, user.Info.Id, len(libraries)) +} + // Goes through both the db processes and packet processes and checks if any are found func detectProcesses(dbProcesses []*db.Process, packetProcesses []packets.Process) []string { detected := make([]string, 0) diff --git a/packets/ClientPong.go b/packets/ClientPong.go index d8bdcba..1a400cf 100644 --- a/packets/ClientPong.go +++ b/packets/ClientPong.go @@ -7,11 +7,12 @@ import ( type ClientPong struct { Packet - ProcessList string `json:"p"` + Data string `json:"p"` } -type Processes struct { +type PongPacketData struct { Processes []Process `json:"Processes"` + Libraries []string `json:"Libraries"` } type Process struct { @@ -20,15 +21,15 @@ type Process struct { FileName string `json:"FileName"` } -func (p *ClientPong) ParseProcessList() []Process { - var data Processes +func (p *ClientPong) Parse() *PongPacketData { + var data PongPacketData - err := json.Unmarshal([]byte(p.ProcessList), &data) + err := json.Unmarshal([]byte(p.Data), &data) if err != nil { log.Println(err) return nil } - return data.Processes + return &data } diff --git a/sessions/user.go b/sessions/user.go index 9bcc171..5988e86 100644 --- a/sessions/user.go +++ b/sessions/user.go @@ -40,6 +40,9 @@ type User struct { // The last detected processes that were discovered on the user lastDetectedProcesses []string + // Last libraries for the user + lastLibraries []string + // The current client status of the user status *objects.ClientStatus @@ -177,6 +180,22 @@ func (u *User) SetLastDetectedProcesses(processes []string) { u.lastDetectedProcesses = processes } +// GetLastLibraries Gets the user's last libraries +func (u *User) GetLastLibraries() []string { + u.Mutex.Lock() + defer u.Mutex.Unlock() + + return u.lastLibraries +} + +// SetLastLibraries Sets the user's last libraries +func (u *User) SetLastLibraries(libraries []string) { + u.Mutex.Lock() + defer u.Mutex.Unlock() + + u.lastLibraries = libraries +} + // GetClientStatus Gets the current user client status func (u *User) GetClientStatus() *objects.ClientStatus { u.Mutex.Lock() diff --git a/webhooks/webhook.go b/webhooks/webhook.go index 819da3f..716c380 100644 --- a/webhooks/webhook.go +++ b/webhooks/webhook.go @@ -89,6 +89,16 @@ func SendAntiCheatProcessLog(username string, userId int, url string, icon strin SendAntiCheat(username, userId, url, icon, "Detected Processes", formatted) } +func SendAntiCheatLibraries(username string, userId int, url string, icon string, libraries []string) { + formatted := "" + + for i, library := range libraries { + formatted += fmt.Sprintf("**%v. %v**\n", i+1, library) + } + + SendAntiCheat(username, userId, url, icon, "Detected Libraries", formatted) +} + // SendChatMessage Sends a chat message webhook to Discord func SendChatMessage(webhook webhook.Client, senderUsername string, senderProfileUrl string, senderAvatarUrl, receiverName string, message string) { if webhook == nil {