-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·163 lines (136 loc) · 4.48 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
// detect type of encryption used given salted hash and decrypt using provided salt
// path to word list must be provided as command-line parameter
/*
MIT License
Copyright (c) 2017 Dan O'Day (d@4n68r.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/* todo:
- allow command-line parameter to specify order of salt:hash (is salt first or second?)
- accept salted hashes in file parameter where each salted hash is on a new line in the file
- limit number of concurrent goroutines to runtime.numCPU() * 2 or something sensible
*/
package main
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"fmt"
"strings"
"os"
"github.com/danzek/ctf-crypto-challenge/wordlist"
"bufio"
// "time" // debug only
)
var hashSizes = map[int]string {
md5.Size: "MD5",
sha1.Size: "SHA1",
sha256.Size: "SHA256",
sha512.Size: "SHA512",
}
var words []string
func main() {
// debug only (determine how long program execution took
// start := time.Now()
// establish path to word list
var wordListPath string
if len(os.Args) > 2 {
wordListPath = os.Args[1]
// for testing
if wordListPath == "crackme" {
wordlist.Generate10()
return
}
fileInfo, err := os.Stat(wordListPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error with path to word list: %q\n", err)
return
} else if fileInfo.IsDir() {
fmt.Fprint(os.Stderr, "Word list path must point to a file, not a directory.\n")
showUsage()
}
} else {
fmt.Fprint(os.Stderr, "Missing required argument(s).\n")
showUsage()
}
// populate slice with words from word list
f, err := os.Open(wordListPath)
if err != nil {
fmt.Fprint(os.Stderr, "Error opening word list\n")
panic(err)
}
scanner := bufio.NewScanner(f)
defer f.Close()
for scanner.Scan() {
words = append(words, strings.TrimSpace(scanner.Text()))
}
// create channel for results
resultChannel := make(chan string)
// crack passwords
// todo -- limit number of goroutines to avoid blocking
for _, sh := range os.Args[2:] {
// iterate over word list and crack password
go crack(sh, words, resultChannel)
}
// print results received from channel ch
for range os.Args[2:] {
fmt.Print(<-resultChannel)
}
// debug only (print program execution time)
// fmt.Printf("%.2fs elapsed\n", time.Since(start).Seconds())
}
func showUsage() {
fmt.Fprint(os.Stderr, "Usage: go run main.go /path/to/word/list.txt salt:hash [salt:hash ...]\n")
}
func crack(saltedHash string, words []string, ch chan<- string) {
splitString := strings.Split(saltedHash, ":")
if len(splitString) == 2 {
salt := splitString[0]
hash := splitString[1]
if hashType, ok := hashSizes[len(hash)/2]; ok {
// iterate over word list
for _, word := range words {
saltedWord := []byte(fmt.Sprintf("%s:%s", salt, word))
// calculate hashResult
var hashResult string
switch hashType {
case "MD5":
hashResult = fmt.Sprintf("%x", md5.Sum(saltedWord))
case "SHA1":
hashResult = fmt.Sprintf("%x", sha1.Sum(saltedWord))
case "SHA256":
hashResult = fmt.Sprintf("%x", sha256.Sum256(saltedWord))
case "SHA512":
hashResult = fmt.Sprintf("%x", sha512.Sum512(saltedWord))
}
// compare hashResult to hash
if hashResult == hash {
ch <- fmt.Sprintf("CRACKED PASSWORD: %s\tsaltedHash: %s\n", word, saltedHash)
return
}
}
} else {
ch <- fmt.Sprintf("UNRECOGNIZED HASH TYPE: %s\n", saltedHash)
return
}
} else {
ch <- fmt.Sprintf("INVALID SALT:HASH: %s\n", saltedHash)
return
}
ch <- fmt.Sprintf("WORD NOT FOUND FOR %s\n", saltedHash)
}