-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
424 lines (340 loc) · 9.64 KB
/
main.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package main
import (
"bufio"
"database/sql"
"flag"
"fmt"
"io/fs"
"net"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/hirochachacha/go-smb2"
log "github.com/sirupsen/logrus"
)
var MaxDepth int
var timeout int
var db *sql.DB
type ShareFile struct {
ServerName string
ShareName string
File fs.FileInfo
Folder string
}
var (
// filled during compile time
commitHash string
commitDate string
filesCount uint64
foldersCount uint64
sharesCount uint64
serversCount uint64
)
// go start(MaxDepth, worker, timeout, *dbname, *server, *user, *pass, *ldapServer, *ldapDn, *ldapFilter, excludeShares)
type Options struct {
MaxDepth int
Worker int
Timeout int
DbName string
Server string
User string
Pass string
LdapServer string
LdapDn string
LdapFilter string
ExcludeShares []string
ExcludeExtensions []string
}
func main() {
var worker int
var debugMode bool
var excludeShares []string
var excludeExtensions []string
server := flag.String("server", "", "smb server (add multiple servers comma separated like 127.0.0.1,127.0.0.2")
user := flag.String("user", "", "NTLM user")
pass := flag.String("pass", "", "NTLM pass")
dbname := flag.String("dbname", "sqlite.db", "sqlite filename")
disableTui := flag.Bool("disableTui", false, "disable TUI")
// ldap specific options
ldapServer := flag.String("ldapServer", "", "ldap server to get smb server list")
ldapDn := flag.String("ldapDn", "", "ldap distinguished name")
ldapFilter := flag.String("ldapFilter", "(OperatingSystem=*server*)", "ldap filter to search for shares")
excludeSharesList := flag.String("excludeShares", "", "share names to exclude, separated by a comma. Example: foo,bar")
excludeExtList := flag.String("excludeExtensions", "", "extensions to exclude, separated by a comma. Example: dll,exe")
flag.IntVar(&MaxDepth, "maxdepth", 3, "max recursion depth when retrieving files")
flag.IntVar(&worker, "worker", 8, "amount of parallel worker")
flag.IntVar(&timeout, "timeout", 5, "smb server connect timeout")
flag.BoolVar(&debugMode, "debug", false, "debug mode")
flag.Parse()
if *pass == "" {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter password: ")
pw, _ := reader.ReadString('\n')
*pass = strings.TrimSpace(pw)
}
log.Debugf("max depth: %v", MaxDepth)
log.Debugf("worker: %v", worker)
log.Debugf("timeout: %v", timeout)
log.Debugf("excluding shares: %v", excludeShares)
if debugMode {
log.SetLevel(log.DebugLevel)
}
if *excludeSharesList != "" {
excludeShares = strings.Split(*excludeSharesList, ",")
}
if *excludeExtList != "" {
for _, val := range strings.Split(*excludeExtList, ",") {
excludeExtensions = append(excludeExtensions, "."+val)
}
}
opts := Options{
MaxDepth: MaxDepth,
Worker: worker,
Timeout: timeout,
DbName: *dbname,
Server: *server,
User: *user,
Pass: *pass,
LdapServer: *ldapServer,
LdapDn: *ldapDn,
LdapFilter: *ldapFilter,
ExcludeShares: excludeShares,
ExcludeExtensions: excludeExtensions,
}
if *disableTui {
// start stat logger
go func() {
for {
time.Sleep(5 * time.Second)
log.WithFields(log.Fields{
"filesCount": filesCount,
"foldersCount": foldersCount,
"sharesCount": sharesCount,
"serversCount": serversCount,
}).Info("statistics")
}
}()
start(opts)
return
}
// setup TUI app and connect logger
tuiApp, logWriter := renderTui()
// prepare logger for tui
log.SetFormatter(&log.TextFormatter{
DisableColors: true,
FullTimestamp: false,
})
log.SetOutput(logWriter)
// run main procedure in goroutine because of TUI
go start(opts)
if err := tuiApp.Run(); err != nil {
panic(err)
}
}
func start(options Options) {
var err error
var serverlist []string
db, err = connectAndSetup(options.DbName)
if err != nil {
log.WithField("error", err).Fatal("unable to create db")
}
defer func() {
log.Info("quit")
db.Close()
}()
if options.Server != "" {
// check for comma separated list of servers from cmd line args
if strings.Contains(options.Server, ",") {
for _, s := range strings.Split(options.Server, ",") {
serverlist = append(serverlist, s)
}
} else {
serverlist = append(serverlist, options.Server)
}
}
if options.LdapDn != "" {
if options.LdapDn == "" {
log.Error("please specify a -ldapDn")
return
}
splitted := strings.SplitN(options.LdapDn, "DC", 2)
if len(splitted) <= 1 {
log.Error("invalid DN, could not extract baseDn")
return
}
baseDn := fmt.Sprintf("DC%v", splitted[1])
log.WithFields(log.Fields{
"ldapServer": options.LdapServer,
"ldapDn": options.LdapDn,
"ldapBaseDn": baseDn,
"ldapFilter": options.LdapFilter,
}).Info("querying LDAP")
servers, err := getLdapServers(options.LdapServer, options.LdapDn, options.Pass, baseDn, options.LdapFilter)
if err != nil {
log.Errorf("failed getting servers via ldaps: %v", err)
return
}
log.WithField("serverCount", len(servers)).Info("retrieved serverlist from LDAP")
serverlist = append(serverlist, servers...)
}
// semaphore for concurrency
semaphore := make(chan int, options.Worker)
// writer goroutine for synchronous sqlite writes
writer := make(chan ShareFile)
// go routine for writing results to db
go func() {
for sf := range writer {
if err = addFile(db, sf); err != nil {
log.WithField("error", err).Error("unable to save to sqlite")
}
}
}()
// waitgroup to wait for all goroutines to finish
var wg sync.WaitGroup
for _, s := range serverlist {
semaphore <- 1
wg.Add(1)
go func(s string, user string, pass string) {
defer wg.Done()
defer func() {
atomic.AddUint64(&serversCount, 1)
<-semaphore
}()
log.WithField("server", s).Infof("starting enumeration")
if err := enumerateServer(s, options, writer); err != nil {
log.WithFields(log.Fields{
"error": err,
"server": s,
}).Warn("stopped enumeration")
return
}
log.WithField("server", s).Info("finished enumeration")
}(s, options.User, options.Pass)
}
wg.Wait()
close(writer)
log.Infof("finished all enumerations")
}
func smbSession(server, user, password string) (net.Conn, *smb2.Session, error) {
dialer := net.Dialer{Timeout: time.Duration(timeout) * time.Second}
conn, err := dialer.Dial("tcp", fmt.Sprintf("%v:445", server))
if err != nil {
return nil, nil, err
}
d := &smb2.Dialer{
Initiator: &smb2.NTLMInitiator{
User: user,
Password: password,
},
}
s, err := d.Dial(conn)
if err != nil {
return conn, nil, err
}
return conn, s, nil
}
func smbGetShareFiles(smbShare *smb2.Share, folder, shareName, serverName string, excludeExtensions []string, writer chan ShareFile) error {
if strings.Count(folder, `\`) > MaxDepth {
return fmt.Errorf("max depth %v reached", MaxDepth)
}
f, err := smbShare.ReadDir(folder)
if err != nil {
return fmt.Errorf("could not open folder %v: %v", folder, err)
}
for _, file := range f {
// check if file type should be excluded
if !file.IsDir() && len(excludeExtensions) > 0 {
if contains(excludeExtensions, filepath.Ext(strings.ToLower(file.Name()))) {
continue
}
}
if file.IsDir() {
atomic.AddUint64(&foldersCount, 1)
path := filepath.Join(folder, file.Name())
path = strings.ReplaceAll(path, "/", `\`)
log.Debugf("folder: %v", path)
if err := smbGetShareFiles(smbShare, path, shareName, serverName, excludeExtensions, writer); err != nil {
log.WithFields(log.Fields{
"error": err,
"folder": path,
}).Debug("could not read folder")
}
} else {
atomic.AddUint64(&filesCount, 1)
}
writer <- ShareFile{
ServerName: serverName,
ShareName: shareName,
File: file,
Folder: folder,
}
}
return nil
}
func smbGetFiles(s *smb2.Session, serverName string, excludeShares, excludeExtensions []string, writer chan ShareFile) error {
names, err := s.ListSharenames()
if err != nil {
return fmt.Errorf("unable to list shares: %v", err)
}
for _, name := range names {
if contains(excludeShares, name) {
log.WithFields(log.Fields{
"sharename": name,
"server": serverName,
}).Debugf("skipping excluded share")
continue
}
isScanned, err := shareScanned(db, serverName, name)
if err != nil {
return fmt.Errorf("error getting scan count: %v", err)
}
if isScanned {
log.WithFields(log.Fields{
"share": name,
"server": serverName,
}).Info("skipping share, already indexed")
continue
}
atomic.AddUint64(&sharesCount, 1)
log.WithField("share", name).Debug("indexing share")
if err := addShare(db, serverName, name); err != nil {
log.Errorf("error saving share: %v", err)
}
fs, err := s.Mount(name)
if err != nil {
log.Debugf("could not mount %v: %v", name, err)
updateShare(db, serverName, name, "failed")
continue
}
err = smbGetShareFiles(fs, ".", name, serverName, excludeExtensions, writer)
fs.Umount()
if err != nil {
log.Debugf("could not get share files from %v: %v", name, err)
updateShare(db, serverName, name, "failed")
continue
}
updateShare(db, serverName, name, "finished")
}
return nil
}
func enumerateServer(server string, options Options, writer chan ShareFile) error {
var err error
conn, smbSession, err := smbSession(server, options.User, options.Pass)
if err != nil {
return fmt.Errorf("unable to connect to %v: %v", server, err)
}
defer conn.Close()
defer smbSession.Logoff()
return smbGetFiles(smbSession, server, options.ExcludeShares, options.ExcludeExtensions, writer)
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}