-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
160 lines (142 loc) · 3.94 KB
/
client.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
151
152
153
154
155
156
157
158
159
160
package agave
import (
"io/ioutil"
"net"
"net/http"
"net/url"
"strconv"
"sync"
)
const Version = "v0.1.2"
type Client struct {
App string
Channel string
SensorGUID string
SensorIp string
SensorPort int
ipCache map[string]bool
ipCacheLock sync.RWMutex
}
// HTTPAttack normalizes the recieved request and allows for easy marshaling into JSON.
type HTTPAttack struct {
Protocol string `json:"protocol"`
App string `json:"app"`
AgaveApp string `json:"agave_app"`
Channel string `json:"channel"`
SensorGUID string `json:"sensor"`
DestPort int `json:"dest_port"`
DestIp string `json:"dest_ip"`
SrcPort int `json:"src_port"`
SrcIp string `json:"src_ip"`
Signature string `json:"signature"`
PrevSeen bool `json:"prev_seen"`
Request *RequestJson `json:"request_json"`
ClientVersion string `json:"agave_client_version"`
}
// CredentialAttack normalizes the recieved request and allows for easy marshaling into JSON.
type CredentialAttack struct {
Protocol string `json:"protocol"`
App string `json:"app"`
AgaveApp string `json:"agave_app"`
Channel string `json:"channel"`
SensorGUID string `json:"sensor"`
DestPort int `json:"dest_port"`
DestIp string `json:"dest_ip"`
SrcPort int `json:"src_port"`
SrcIp string `json:"src_ip"`
Username string `json:"agave_username"`
Password string `json:"agave_password"`
ClientVersion string `json:"agave_client_version"`
}
func NewClient(app string, channel string, guid string, ip string, port int) *Client {
return &Client{App: app, Channel: channel, SensorGUID: guid, SensorIp: ip, SensorPort: port}
}
func (c *Client) NewHTTPAttack(signature string, r *http.Request) (*HTTPAttack, error) {
ip, p, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return nil, err
}
port, err := strconv.Atoi(p)
if err != nil {
return nil, err
}
rj := TrimRequest(r)
return &HTTPAttack{
Protocol: r.Proto,
App: "agave",
AgaveApp: c.App,
Channel: c.Channel,
SensorGUID: c.SensorGUID,
DestPort: c.SensorPort,
DestIp: c.SensorIp,
SrcPort: port,
SrcIp: ip,
PrevSeen: c.SeenIP(ip),
Request: rj,
ClientVersion: Version,
}, nil
}
func (c *Client) NewCredentialAttack(r *http.Request, username string, password string) (*CredentialAttack, error) {
ip, p, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return nil, err
}
port, err := strconv.Atoi(p)
if err != nil {
return nil, err
}
return &CredentialAttack{
Protocol: r.Proto,
App: "agave",
AgaveApp: c.App,
Channel: c.Channel,
SensorGUID: c.SensorGUID,
DestPort: c.SensorPort,
DestIp: c.SensorIp,
SrcPort: port,
SrcIp: ip,
Username: username,
Password: password,
ClientVersion: Version,
}, nil
}
func (c *Client) SaveIP(ip string) {
c.ipCacheLock.Lock()
c.ipCache[ip] = true
c.ipCacheLock.Unlock()
}
func (c *Client) SeenIP(ip string) bool {
c.ipCacheLock.RLock()
seen := c.ipCache[ip]
c.ipCacheLock.RUnlock()
return seen
}
func TrimRequest(r *http.Request) *RequestJson {
body, _ := ioutil.ReadAll(r.Body)
r.ParseForm()
rj := &RequestJson{
Method: r.Method,
URL: r.URL,
Proto: r.Proto,
ProtoMajor: r.ProtoMajor,
ProtoMinor: r.ProtoMinor,
Header: r.Header,
Body: body,
TransferEncoding: r.TransferEncoding,
Host: r.Host,
PostForm: r.PostForm,
}
return rj
}
type RequestJson struct {
Method string
URL *url.URL
Proto string
ProtoMajor int
ProtoMinor int
Header http.Header
Body []byte
TransferEncoding []string
Host string
PostForm url.Values
}