-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
150 lines (144 loc) · 5.48 KB
/
utils.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"encoding/json"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"time"
)
type AnydeskSession struct {
SessionId int `json:"sessionid"`
SessionStart bool `json:"sessionstart"`
SessionStartTime string `json:"sessionstarttime"`
SessionEnd bool `json:"sessionend"`
SessionEndTime string `json:"sessionendtime"`
SessionTime string `json:"sessiontime" default:"0"`
Username string `json:"username"`
Userid int `json:"userid"`
Srcip string `json:"scrip"`
Os string `json:"os"`
ConnectionFlags string `json:"connectionflags"`
Version string `json:"version"`
Authtype string `json:"authtype"`
Authprofile string `json:"authprofile"`
Authtokenattempted bool `json:"authtokenattempted" default:"false"`
Setuptoken bool `json:"setuptoken" default:"false"`
FileTransfer []LogEntry `json:"filetransfer"`
TextCopied []LogEntry `json:"textcopied"`
LogEntries []LogEntry `json:"logentries"`
}
type LogEntry struct {
LogLevel string `json:"loglevel"`
Datetime string `json:"datetime"`
AppName string `json:"appname"`
ThreadName string `json:"threadname"`
Pid int `json:"pid"`
Threadid int `json:"threadid"`
Fiberid int `json:"fiberid"`
FunctionName string `json:"functionname"`
Message string `json:"message"`
}
func (le *LogEntry) parseFunction(ads *AnydeskSession) {
switch le.FunctionName {
case "anynet.any_socket":
if strings.HasPrefix(le.Message, "Accept request from") {
tmp := regexp.MustCompile(`Accept\srequest\sfrom\s(?P<userid>\d+)\s\(via\s(?P<connectionmethod>[^\)]+)\)\.`)
match := tmp.FindStringSubmatch(le.Message)
fmt.Println("Found New Connection")
ads.SessionStart = true
ads.Userid, _ = strconv.Atoi(match[1]) //userid
ads.SessionStartTime = le.Datetime
}
if strings.HasPrefix(le.Message, "Logged in from") {
tmp := regexp.MustCompile(`Logged\sin\sfrom\s(?P<srcip>[^\:]+)\:(?P<srcport>\d+)\son\srelay\s(?P<relayname>.+)\.`)
match := tmp.FindStringSubmatch(le.Message)
ads.Srcip = match[1] // srcip
}
case "app.session":
if strings.HasPrefix(le.Message, "Session closed by") {
fmt.Println("Session Closed")
ads.SessionEnd = true
ads.SessionEndTime = le.Datetime
if ads.SessionStart {
tmpEnd, _ := time.Parse("2006-01-02 15:04:05.000", ads.SessionEndTime)
tmpStart, _ := time.Parse("2006-01-02 15:04:05.000", ads.SessionStartTime)
ads.SessionTime = tmpEnd.Sub(tmpStart).String()
}
}
if strings.HasPrefix(le.Message, "Connecting to current session") {
tmp := regexp.MustCompile(`Connecting\sto\scurrent\ssession\s(?P<sessionid>\d+)\.`)
match := tmp.FindStringSubmatch(le.Message)
ads.SessionId, _ = strconv.Atoi(match[1]) // sessionid
}
if le.Message == "Authenticated by local user." {
ads.Authtype = "user-approve"
}
if le.Message == "Authenticated with correct passphrase." {
ads.Authtype = "passphrase"
}
if le.Message == "Issuing a permanent token." {
ads.Setuptoken = true
}
if strings.HasPrefix(le.Message, "Profile was used") {
tmp := regexp.MustCompile(`Profile\swas\sused\:\s(?P<profilename>.+)`)
match := tmp.FindStringSubmatch(le.Message)
ads.Authprofile = match[1] // profilename
}
if le.Message == "Authenticated with permanent token." {
ads.Authtype = "token"
}
if le.Message == "The remote peer has sent a token." {
ads.Authtokenattempted = true
}
case "app.ctrl_clip_comp":
if strings.HasPrefix(le.Message, "Got a file offer") {
ads.FileTransfer = append(ads.FileTransfer, *le)
}
if strings.HasPrefix(le.Message, "Got a text offer") {
ads.TextCopied = append(ads.TextCopied, *le)
}
case "clipbrd.capture":
if le.Message != "Registered for clipboard notifications." {
ads.FileTransfer = append(ads.FileTransfer, *le)
}
case "app.prepare_task":
if strings.HasPrefix(le.Message, "Preparing files") {
ads.FileTransfer = append(ads.FileTransfer, *le)
}
case "app.backend_session":
if strings.HasPrefix(le.Message, "Incoming session request") {
tmp := regexp.MustCompile(`Incoming\ssession\srequest\:\s(?P<username>.+)\s\((?P<userid>\d+)\)`)
match := tmp.FindStringSubmatch(le.Message)
ads.Username = match[1] // username
}
if strings.HasPrefix(le.Message, "Remote OS") {
tmp := regexp.MustCompile(`Remote\sOS\:\s(?P<os>[^\,]+)\,\sConnection\sflags\:\s(?P<connectionflags>.+)`)
match := tmp.FindStringSubmatch(le.Message)
ads.Os = match[1] // os
ads.ConnectionFlags = match[2] //connectionflags
}
if strings.HasPrefix(le.Message, "Remote version") {
tmp := regexp.MustCompile(`Remote\sversion\:\s(?P<version>.+)`)
match := tmp.FindStringSubmatch(le.Message)
ads.Version = match[1] // version
}
case "winapp.gui.permissions_panel":
if ads.Authprofile == "" && strings.HasPrefix(le.Message, "Selecting Profile") {
tmp := regexp.MustCompile(`Selecting\sProfile\:\s(?P<profilename>.+)\,\shasPw\:\s(?P<haspw>.+)`)
match := tmp.FindStringSubmatch(le.Message)
if match[1] != "_previous_session" {
ads.Authprofile = match[1] // profilename
}
}
}
}
func (ads *AnydeskSession) printSession() {
body, _ := json.Marshal(ads)
fmt.Println(string(body))
}
func (ads *AnydeskSession) saveSession(outputDir string) {
f, _ := json.MarshalIndent(ads, "", " ")
_ = os.WriteFile(outputDir+"/ad_session_"+strconv.Itoa(ads.SessionId)+".json", f, 0644)
}