Last active
June 1, 2020 20:18
-
-
Save roobre/b329024a4a68023617079d0c7fe0d02a to your computer and use it in GitHub Desktop.
Script marronero para buscar passwords en diccionarios
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"compress/gzip" | |
"os" | |
"fmt" | |
"strings" | |
"sync" | |
) | |
const WORKERS = 18 | |
func main() { | |
dictFile, err := os.Open(os.Args[1]) | |
if err != nil { | |
panic(err) | |
} | |
gzDict, err := gzip.NewReader(dictFile) | |
if err != nil { | |
panic(err) | |
} | |
dictReader := bufio.NewReader(gzDict) | |
dict := make(map[string]struct{}, 1024*1024) | |
var word string | |
for word, err = dictReader.ReadString('\n'); err == nil; word, err = dictReader.ReadString('\n') { | |
dict[strings.TrimSpace(word)] = struct{}{} | |
} | |
fmt.Printf("Loaded %d words from dictionary\n", len(dict)) | |
pwdFile, err := os.Open(os.Args[2]) | |
if err != nil { | |
panic(err) | |
} | |
gzPwd, err := gzip.NewReader(pwdFile) | |
if err != nil { | |
panic(err) | |
} | |
pwdReader := bufio.NewReader(gzPwd) | |
pwds := make([]string, 0, 1024*1024) | |
for word, err = pwdReader.ReadString('\n'); err == nil; word, err = pwdReader.ReadString('\n') { | |
pwds = append(pwds, strings.TrimSpace(word)) | |
} | |
fmt.Printf("Loaded %d passwords\n", len(pwds)) | |
var lookupWg sync.WaitGroup | |
var analyzeWg sync.WaitGroup | |
found := make(chan string) | |
notfound := make(chan string) | |
lookupWg.Add(WORKERS) | |
for i := 0; i < WORKERS; i++ { | |
lower := i * len(pwds) / WORKERS | |
upper := (i + 1) * len(pwds) / WORKERS | |
go func() { | |
lookup(dict, pwds[lower:upper], found, notfound) | |
lookupWg.Done() | |
}() | |
} | |
analyzeWg.Add(2) | |
go func() { | |
nFound := 0 | |
for _ = range found { | |
nFound++ | |
} | |
fmt.Printf("Found %d pwds in dict\n", nFound) | |
analyzeWg.Done() | |
}() | |
go func() { | |
for word = range notfound { | |
fmt.Printf("%s\n", word) | |
} | |
analyzeWg.Done() | |
}() | |
lookupWg.Wait() | |
close(found) | |
close(notfound) | |
analyzeWg.Wait() | |
} | |
func lookup(haystack map[string]struct{}, needles []string, found chan string, notfound chan string) { | |
for _, word := range needles { | |
if _, indict := haystack[word]; indict { | |
found <- word | |
} else { | |
notfound <- word | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment