Skip to content

Instantly share code, notes, and snippets.

@jorrizza
Last active August 29, 2015 14:05
Show Gist options
  • Save jorrizza/bcbaa9d98e6185eae27a to your computer and use it in GitHub Desktop.
Save jorrizza/bcbaa9d98e6185eae27a to your computer and use it in GitHub Desktop.
Bcrypt (useless) dictionary attack
package main
import (
"bufio"
"code.google.com/p/go.crypto/bcrypt"
"encoding/csv"
"fmt"
"io"
"log"
"os"
"runtime"
)
var wordsList []string
func usage() {
fmt.Fprintf(os.Stderr, "Simple bcrypt dictionary attack\nUsage: %s wordlist users.csv\n", os.Args[0])
os.Exit(1)
}
func worker(i int, ch chan []string) {
for {
user := <-ch
log.Printf("Worker %d: started %s", i, user[0])
for i := range wordsList {
if bcrypt.CompareHashAndPassword([]byte(user[1]), []byte(wordsList[i])) == nil {
fmt.Printf("%s\t%s\n", user[0], wordsList[i])
break
}
}
}
}
func main() {
if len(os.Args) != 3 {
usage()
}
wordsFile, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
defer wordsFile.Close()
usersFile, err := os.Open(os.Args[2])
if err != nil {
log.Fatal(err)
}
defer usersFile.Close()
wordsScanner := bufio.NewScanner(wordsFile)
for wordsScanner.Scan() {
wordsList = append(wordsList, wordsScanner.Text())
}
if wordsScanner.Err() != nil {
log.Fatal(wordsScanner.Err())
}
log.Printf("Read %d words\n", len(wordsList))
usersCsv := csv.NewReader(usersFile)
runtime.GOMAXPROCS(runtime.NumCPU())
log.Printf("Starting %d workers\n", runtime.NumCPU())
ch := make(chan []string)
for i := 0; i < runtime.NumCPU(); i++ {
go worker(i, ch)
}
for {
user, err := usersCsv.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
ch <- user
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment