-
Notifications
You must be signed in to change notification settings - Fork 5
/
remoteClient.go
199 lines (179 loc) · 4.09 KB
/
remoteClient.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"container/list"
"errors"
"fmt"
"sync"
"time"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
const (
defaultTimeout = time.Second * 2
)
type datagram struct {
Login string `json:"login"`
Logout string `json:"logout"`
Src string `json:"src"`
Dst string `json:"dst"`
User string `json:"user"`
Pass string `json:"pass"`
ShellActivity []command `json:"shell_activity"`
}
type command struct {
TS string `json:"ts"`
Cmd string `json:"cmd"`
Resp string `json:"resp"`
}
type attempt struct {
TS string `json:"ts"`
Pass string `json:"pass"`
User string `json:"user"`
Origin string `json:"origin"`
}
type shellActivityClient struct {
user string
pass string
dst string
db string
pwnColl string
attemptColl string
session *mgo.Session
status bool
headers map[string]string
mtx *sync.Mutex
cache *list.List
}
func NewShellActivityClient(dst, db, pwnColl, attemptColl, user, pass string) (*shellActivityClient, error) {
return &shellActivityClient{
user: user,
pass: pass,
dst: dst,
db: db,
pwnColl: pwnColl,
attemptColl: attemptColl,
headers: make(map[string]string, 1),
mtx: &sync.Mutex{},
cache: list.New(),
}, nil
}
//do the actual login work
func (sac *shellActivityClient) login() error {
var err error
uri := fmt.Sprintf("mongodb://%s:%s@%s/%s", sac.user, sac.pass, sac.dst, sac.db)
sac.session, err = mgo.Dial(uri)
if err != nil {
return err
}
sac.status = true
return nil
}
//Login will renew creds with the remote server
//it is totally ok to hit this over and over
func (sac *shellActivityClient) Login() error {
sac.mtx.Lock()
defer sac.mtx.Unlock()
return sac.login()
}
func (sac *shellActivityClient) Close() error {
sac.mtx.Lock()
defer sac.mtx.Unlock()
if sac.status && sac.session != nil {
//attempt to send everything in the cache
for {
dg, err := sac.popCache()
if err != nil || dg == nil {
break
}
if err = sac.sendDatagram(dg); err != nil {
break
}
}
}
sac.session.Close()
sac.status = false
return nil
}
func (sac *shellActivityClient) sendAttempt(at attempt) error {
collection := sac.session.DB(sac.db).C(sac.attemptColl)
if err := collection.Insert(at); err != nil {
return err
}
return nil
}
func (sac *shellActivityClient) WriteAttempt(at attempt) error {
sac.mtx.Lock()
defer sac.mtx.Unlock()
if !sac.status {
return errors.New("Failed to push attempt")
}
return sac.sendAttempt(at)
}
func (sac *shellActivityClient) sendDatagram(dg *datagram) error {
collection := sac.session.DB(sac.db).C(sac.pwnColl)
if err := collection.Insert(dg); err != nil {
return err
}
return nil
}
func (sac *shellActivityClient) Write(dg datagram) error {
sac.mtx.Lock()
defer sac.mtx.Unlock()
if !sac.status {
sac.pushCache(dg)
return errors.New("Client closed")
}
for {
d, err := sac.popCache()
if err != nil {
return err
}
//cache is empty
if d == nil {
break
}
if err := sac.sendDatagram(d); err != nil {
sac.pushCache(*d)
return err
}
}
return sac.sendDatagram(&dg)
}
func (sac *shellActivityClient) Cache(dg datagram) error {
sac.mtx.Lock()
defer sac.mtx.Unlock()
if !sac.status {
return errors.New("Client closed")
}
return sac.pushCache(dg)
}
func (sac *shellActivityClient) pushCache(dg datagram) error {
if sac.cache.PushBack(dg) == nil {
return errors.New("Failed push")
}
return nil
}
func (sac *shellActivityClient) popCache() (*datagram, error) {
if sac.cache.Len() <= 0 {
return nil, nil
}
e := sac.cache.Front()
if e == nil {
return nil, nil
}
dg, ok := e.Value.(datagram)
if !ok {
return nil, errors.New("Invalid item in cache")
}
return &dg, nil
}
func (dg *datagram) AddCommand(cmd, resp string) error {
ts := time.Now().Format(time.RFC3339Nano)
dg.ShellActivity = append(dg.ShellActivity, command{ts, cmd, resp})
return nil
}
type msg struct {
Id bson.ObjectId `bson:"_id"`
Msg string `bson:"msg"`
Count int `bson:"count"`
}