Skip to content

Commit

Permalink
Merge pull request #34 from isacaya/test
Browse files Browse the repository at this point in the history
Add logic related to 'max' flag
  • Loading branch information
hahwul authored May 21, 2024
2 parents 6b6c920 + b13c58b commit 814d1ea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 34 deletions.
59 changes: 26 additions & 33 deletions pkg/crack/brute.go
Original file line number Diff line number Diff line change
@@ -1,65 +1,58 @@
package crack

import "sync"

func GenerateBruteforcePayloads(chars string) []string {
func GenerateBruteforcePayloads(chars string, max int) []string {
var payloads []string
for str := range generate(chars) {
for str := range generate(chars, max) {
payloads = append(payloads, str)
}
return payloads

}

func generate(alphabet string) <-chan string {
c := make(chan string, len(alphabet))
func generate(alphabet string, max int) <-chan string {
c := make(chan string)

go func() {
defer close(c)

if len(alphabet) == 0 {
if max <= 0 {
return
}

// Use a sync.WaitGroup to spawn permutation
// goroutines and allow us to wait for them all
// to finish.
var wg sync.WaitGroup
wg.Add(len(alphabet))

for i := 1; i <= len(alphabet); i++ {
go func(i int) {
// Perform permutation.
Word(alphabet[:i]).Permute(c)

// Signal Waitgroup that we are done.
wg.Done()
}(i)
word := make(Word, max)
for i := 1; i <= max; i++ {
generateRecursive(c, alphabet, i, word, 0)
}

// Wait for all routines to finish.
wg.Wait()
}()

return c
}

type Word []rune

// Permute generates all possible combinations of
// the current word. This assumes the runes are sorted.
func (w Word) Permute(out chan<- string) {
if len(w) <= 1 {
func (w Word) Permute(out chan<- string, max int) {
if len(w) <= max {
out <- string(w)
return
}

// Write first result manually.
out <- string(w)

// Find and print all remaining permutations.
for w.next() {
out <- string(w)
if len(w) <= max {
out <- string(w)
}
}
}

//Generating strings recursively
func generateRecursive(out chan<- string, alphabet string, max int, current Word, index int) {
if index == max {
out <- string(current)
return
}

for _, char := range alphabet {
current[index] = char
generateRecursive(out, alphabet, max, current, index+1)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/crack/crack.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Crack(mode, token, data string, concurrency, max int, power bool, verbose b
log.Out = os.Stdout
fmt.Println("[*] Start " + mode + " cracking mode")
if mode == "brute" {
bf := GenerateBruteforcePayloads(data)
bf := GenerateBruteforcePayloads(data, max)
RunTestingJWT(token, bf, concurrency, verbose)
} else { // if dict
var words []string
Expand Down

0 comments on commit 814d1ea

Please sign in to comment.