-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwep_crack.go
252 lines (206 loc) · 6.72 KB
/
wep_crack.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"fmt"
"os"
"strings"
"strconv"
"io/ioutil"
"flag"
"math/rand"
)
type IV_encbyte_pair struct {
IV []int
encByte int
}
// RCC4 cipher state
type RC4 struct {
S []int
i int
j int
key []int
}
// Sets RC4 in state KSA algorithm upto "rounds" number of rounds
func (rc4 *RC4) KSA (rounds int) {
rc4.S = make([]int, 256) // Reset S
for rc4.i = 0 ; rc4.i<256 ; rc4.i++ {
rc4.S[rc4.i] = int(rc4.i)
}
rc4.j = 0
for rc4.i = 0 ; rc4.i<rounds ; rc4.i++ {
rc4.j = (rc4.j + rc4.S[rc4.i] + rc4.key[rc4.i % len(rc4.key)]) % 256
// Swap S[i] and S[j]
tmp := rc4.S[rc4.i]
rc4.S[rc4.i] = rc4.S[rc4.j]
rc4.S[rc4.j] = tmp
}
}
func (rc4 *RC4) PRGA_Init ( key []int ) {
rc4.key = key
rc4.KSA(256) // Initialise S
rc4.i = 0
rc4.j = 0
}
func (rc4 *RC4) PRGA_NextByte () int {
rc4.i = (rc4.i + 1) % 256
rc4.j = (rc4.j + rc4.S[rc4.i]) % 256
// Swap values of S[i] and S[j]
tmp := rc4.S[rc4.i]
rc4.S[rc4.i] = rc4.S[rc4.j]
rc4.S[rc4.j] = tmp
K := rc4.S[(rc4.S[rc4.i] + rc4.S[rc4.j]) % 256]
return K
}
func (rc4 *RC4) getNthKeyByte(IVList []IV_encbyte_pair, n int, guessedKey []int, knownFirstByte int) int {
voteCount := make([]int, 256) // vote count to bytes to be the key byte
for _, iv := range IVList {
if iv.IV[0] == 3+n && iv.IV[1] == 255 { // Weak IV
rc4.key = append(iv.IV,guessedKey...)
rc4.KSA(3+n)
//fmt.Println("S after 3 rounds : %v", S)
byteToSearch := knownFirstByte ^ iv.encByte
for index, b := range rc4.S {
if b==byteToSearch {
keyByte := (index-rc4.j-rc4.S[rc4.i]+256+256) % 256
voteCount[keyByte]++
break
}
}
}
}
maxVote:=0
predictedKey := 0
for index, x := range voteCount {
if x>maxVote {
maxVote=x
predictedKey = index
}
}
//fmt.Println("Predicted key: %v with vote %v", predictedKey, maxVote)
return predictedKey
}
func EncryptBytes (data []byte, passwdBytes []int) []int {
rc4 := RC4{}
rc4.PRGA_Init(passwdBytes)
encBytes := []int{}
for _,b := range []byte(data) {
e := int(b)^rc4.PRGA_NextByte()
encBytes = append(encBytes, e)
}
return encBytes
}
func encryptFile(filename string, passwd string, outfile string) {
writeToFile := func(line string){
f, err := os.OpenFile(outfile, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
defer f.Close()
if _, err = f.WriteString(line); err != nil {
panic(err)
}
}
writeToFile(strconv.Itoa(len(passwd)) + "\n")
//ioutil.WriteFile(*outFileFlag, []byte(strconv.Itoa(len(*passwdFlag))+"\n"), os.ModeAppend)
file, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println("Input file does not exist : ", err.Error())
os.Exit(1)
}
keySize := len(passwd)
passwdBytes := make([]int, keySize)
for i, b := range []byte(passwd) {
passwdBytes[i] = int(b)
}
fmt.Printf("Key is : %v\n", passwdBytes)
fileContent := string(file)
fileContent = strings.Trim(fileContent, " \n\r")
lines := strings.Split(fileContent, "\n")
for i, line := range lines {
iv := []int{ 3 + i%keySize, 255, rand.Int()%256}
encLine := EncryptBytes([]byte(line), append(iv,passwdBytes...)) // Encrypting with iv+passwd
encLineStr := fmt.Sprintf("%v %v %v", iv[0], iv[1], iv[2]) // Prepending IV
for _, encb := range encLine {
encLineStr += " " + strconv.Itoa(encb)
}
//fmt.Printf("Plain line : %v\n", line)
//fmt.Printf("%v Encrypted line : %v\n", passwdBytes, append(iv,passwdBytes...))
encLineStr = strings.Trim(encLineStr, " \n\r")
encLineStr += "\n"
writeToFile(encLineStr)
//ioutil.WriteFile(outfile, []byte(encLineStr), os.ModeAppend)
}
}
func getIvList (encrypted_file string) (IVList []IV_encbyte_pair, keysize int) {
file, err := ioutil.ReadFile(encrypted_file)
if err != nil {
fmt.Println("Input file does not exist : ", err.Error())
os.Exit(1)
}
fileContent := string(file)
fileContent = strings.Trim(fileContent, " \n\r")
IVListStr := strings.Split(fileContent, "\n")
signedInt, _ := strconv.Atoi(IVListStr[0])
keysize = int(signedInt)
IVListStr = IVListStr[1:]
IVList = []IV_encbyte_pair{}
for _, IVpairStr := range IVListStr {
// Convert all (IV and first enc byte in uint and append to iv
iv := []int{}
for _, bStr := range strings.Split(IVpairStr, " ") {
bStr = strings.Trim(bStr," \n\r")
if bStr=="" {
continue
}
b, err := strconv.Atoi(bStr)
if err!=nil {
fmt.Println("Error in converting %v to int", bStr)
os.Exit(2)
}
iv = append(iv, b)
}
IVEncPair := IV_encbyte_pair{IV:iv[:3], encByte:iv[3]}
IVList = append(IVList,IVEncPair)
}
return IVList, keysize
}
func main() {
encryptFlag := flag.Bool("e", false, "Encrypt an input file")
inFileFlag := flag.String("i", "", "Input file")
outFileFlag := flag.String("o", "out.enc", "Output file")
passwdFlag := flag.String("p", "", "Password")
//decryptFlag := flag.Bool("d", true, "Decrypt an input file")
knownFirstByte := flag.Int("pt", -1, "First byte of known plain text")
flag.Parse()
if *inFileFlag == "" {
fmt.Println("Please provide input file\n")
flag.Usage()
os.Exit(1)
}
if *encryptFlag {
if *passwdFlag=="" {
fmt.Println("Please provide password\n")
flag.Usage()
os.Exit(1)
}
encryptFile(*inFileFlag, *passwdFlag, *outFileFlag)
fmt.Printf("File %v encrypted with passwd %v; output:%v\n", *inFileFlag, *passwdFlag, *outFileFlag)
} else {
if *knownFirstByte == -1 {
fmt.Println("Please first plain text byte\n")
flag.Usage()
os.Exit(1)
}
IVList, keysize := getIvList(*inFileFlag)
rc4State := RC4{}
guessedKey := []int{}
for i:=0 ; i<keysize ; i++ {
key := rc4State.getNthKeyByte(IVList, i, guessedKey, *knownFirstByte)
guessedKey = append(guessedKey, key)
}
guessedKeyByte := []byte{}
for _, i := range guessedKey {
guessedKeyByte = append(guessedKeyByte, byte(i))
}
fmt.Println("The key is : ", string(guessedKeyByte))
}
}