/
piculet.go
130 lines (121 loc) · 2.35 KB
/
piculet.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
package main
import (
"code.google.com/p/go.crypto/md4"
"code.google.com/p/go.crypto/ripemd160"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"flag"
"fmt"
"hash"
"log"
"time"
)
const (
version = "0.1"
)
var (
characters = []byte("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ ")
alg *string = flag.String("a", "md5", "hash algorithm")
throttle *int = flag.Int("c", 4, "concurrency level")
minLen *int = flag.Int("min", 1, "minimum password length")
maxLen *int = flag.Int("max", 10, "maximum password length")
gSalt *string = flag.String("s", "", "salt")
)
type Guesser struct {
Alg string
Salt string
Digest string
Min int
Max int
Result chan string
salt []byte
sem chan bool
}
func (g *Guesser) guess(h hash.Hash, width, pos int, b []byte) {
for _, v := range characters {
if pos < width-1 {
g.guess(h, width, pos+1, b)
}
b[pos] = v
if g.salt != nil {
h.Write(g.salt)
}
h.Write(b)
// Do []byte comparison instead
d := fmt.Sprintf("%x", h.Sum(nil))
// fmt.Println("Guessed", d, "against", g.Digest)
if d == g.Digest {
g.Result <- string(b)
return
}
h.Reset()
}
}
func (g *Guesser) Run() {
if g.Salt != "" {
g.salt = []byte(g.Salt)
}
for i := g.Min; i < g.Max; i++ {
g.sem <- true
h, err := GetHash(g.Alg)
if err != nil {
panic(err)
}
go func() {
g.guess(h, i, 0, make([]byte, g.Max))
<-g.sem
}()
}
}
func GetHash(alg string) (hash.Hash, error) {
var h hash.Hash
switch alg {
default:
return nil, fmt.Errorf("Invalid algorithm")
case "md4":
h = md4.New()
case "md5":
h = md5.New()
case "ripemd160":
h = ripemd160.New()
case "sha1":
h = sha1.New()
case "sha224":
h = sha256.New224()
case "sha256":
h = sha256.New()
case "sha384":
h = sha512.New384()
case "sha512":
h = sha512.New()
}
return h, nil
}
// init is called before main
func init() {
flag.Parse()
}
func main() {
if flag.NArg() == 0 {
flag.Usage()
return
}
ch := make(chan string)
sem := make(chan bool, *throttle)
g := Guesser{
Alg: *alg,
Salt: *gSalt,
Digest: flag.Arg(0),
Min: *minLen,
Max: *maxLen,
Result: ch,
sem: sem,
}
start := time.Now()
go g.Run()
res := <-g.Result
log.Println("Value:", res)
log.Println("Time taken:", time.Since(start))
}