-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathSmartWordlistProducer.java
280 lines (237 loc) · 9.05 KB
/
SmartWordlistProducer.java
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package AndroidKeystoreBrute;
/** ravensbane
*
* @version 1.0 on 20.2.2016
* @author
*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.concurrent.LinkedTransferQueue;
import java.util.HashMap;
import java.util.Map;
public class SmartWordlistProducer implements Runnable {
private final LinkedTransferQueue<String> queueRef;
private static String[] comboToTest;
private static Map<String, String> words = new HashMap<String, String>();
private static String wordsFirst = null;
private static String wordsLast;
private static ArrayList<String> LetterCombos = new ArrayList<String>(); // for permutations
static ArrayList<String> resumePoint = new ArrayList<String>();
static String[] lastComboProduced;
public SmartWordlistProducer(LinkedTransferQueue<String> queue, String dict) throws Exception {
this.queueRef = queue;
// load words from the dictionary with variations and store it in a map for speed
words = loadWordList(dict);
// get the resume point if one exists and set up the string array with the first password to test
resumePoint = SmartWordlistResume.getResumePoint(words);
comboToTest = new String[Math.max(resumePoint.size(), AndroidKeystoreBrute.minpieces) + 1];
// the first slot in the string array is reserved for characters specified by the firstchars arg
if (AndroidKeystoreBrute.firstchars != null) {
comboToTest[0] = AndroidKeystoreBrute.firstchars;
} else {
comboToTest[0] = "";
}
if (resumePoint.isEmpty()) {
// start at the beginning
for (int i = 1; i <= AndroidKeystoreBrute.minpieces; ++i) {
comboToTest[i] = wordsFirst;
}
} else {
if (resumePoint.size() < AndroidKeystoreBrute.minpieces) {
throw new IOException("numpieces arg specifies a larger min number of pieces than resume data");
} else {
System.out.println("Resuming where we left off...");
for (int i = 1; i <= resumePoint.size(); ++i) {
comboToTest[i] = resumePoint.get(i - 1);
}
}
}
// set the initial value of last tested based on the resume data (for benchmark display)
lastComboProduced = comboToTest;
}
@Override
public void run() {
while (!SmartWordlistPasswd.found && !SmartWordlistPasswd.allPwdsTested) {
comboToTest = getNextCombo(comboToTest, comboToTest.length - 1);
// if the next combo to check exceeds our limit, stop (wait a bit for consumers to finish current checks)
if (comboToTest.length > AndroidKeystoreBrute.maxpieces + 1) {
SmartWordlistPasswd.allPwdsTested = true;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
SmartWordlistPasswd.complete("");
break;
}
// array to string
StringBuilder builder = new StringBuilder();
for (String str : comboToTest) {
builder.append(str);
}
String strCombo = builder.toString();
// add a combo to the queue
try {
queueRef.transfer(strCombo);
} catch (InterruptedException e) {
}
lastComboProduced = comboToTest;
}
}
public static String[] getNextCombo(String[] combo, int stelle) {
// if we were on the last word in the dictionary
if (combo[stelle].equals(wordsLast)) {
// set it to the first word, then check the previous word
combo[stelle] = wordsFirst;
if (stelle > 1) {
return getNextCombo(combo, stelle - 1);
} else {
// if all words in the combo were the last word, extend the combo length by one word
String[] longerCombo = new String[combo.length + 1];
System.arraycopy(combo, 0, longerCombo, 0, combo.length);
longerCombo[longerCombo.length - 1] = wordsFirst;
return longerCombo;
}
} else {
// if we aren't on the last word, just move to the next word in the list
// (in our map, the key is the current word, and the value is the next word)
// this means that the last entry in our map contains <secondToLastWord, lastWord>
combo[stelle] = words.get(combo[stelle]);
return combo;
}
}
private static Map<String, String> loadWordList(String dict) throws Exception {
Map<String, String> localWords = new HashMap<String, String>();
BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(dict)));
String word = "";
String previousWord = null;
float maxBytes = (float) Runtime.getRuntime().maxMemory();
boolean memoryWarning = false;
while ((word = file.readLine()) != null) {
// skip empty String
if (word.equals(""))
continue;
if (!memoryWarning && Runtime.getRuntime().totalMemory() / maxBytes > 0.45) {
System.out.println("Warning: Available memory low. Application may not be able to allocate enough memory "
+ "to process wordlist. If application hangs try not using the -p argument or use a "
+ "smaller wordlist.\r\n");
memoryWarning = true;
}
// don't add duplicate words; they will screw up our map
if (localWords.containsKey(word)) {
continue;
}
// store the first key in the map
if (wordsFirst == null) {
wordsFirst = word;
}
if (previousWord == null) {
previousWord = word;
}
if (!AndroidKeystoreBrute.permutations) {
localWords.put(previousWord, word);
previousWord = word;
if (AndroidKeystoreBrute.onlyLowerCase == false) {
// Capitalize first Letter
char[] stringArray = word.toCharArray();
// skip if already uppercase
if (Character.isUpperCase(stringArray[0]))
continue;
stringArray[0] = Character.toUpperCase(stringArray[0]);
word = new String(stringArray);
localWords.put(previousWord, word);
previousWord = word;
}
} else {
// permutations don't work on single character words
if (word.length() < 2)
continue;
// use common replacements
// let's get some common replacement letters
LetterCombos.add("aA@4^á�");
LetterCombos.add("bB8");
LetterCombos.add("cC(");
LetterCombos.add("dD");
LetterCombos.add("eE3?éÉ");
LetterCombos.add("fF");
LetterCombos.add("gG");
LetterCombos.add("hH");
LetterCombos.add("iIl1!|ÃÃ?");
LetterCombos.add("jJ");
LetterCombos.add("kK");
LetterCombos.add("lL1");
LetterCombos.add("mM");
LetterCombos.add("nNñÑ");
LetterCombos.add("oO0óÓ");
LetterCombos.add("pP");
LetterCombos.add("qQ");
LetterCombos.add("rR");
LetterCombos.add("sS5$");
LetterCombos.add("tT+7");
LetterCombos.add("uUúÚ");
LetterCombos.add("vV");
LetterCombos.add("wW");
LetterCombos.add("xX");
LetterCombos.add("yY");
LetterCombos.add("zZ2");
for (String p : getPermutations(word)) {
localWords.put(previousWord, p);
previousWord = p;
}
}
}
file.close();
// add Numbers
for (int i = 1; i < 10; i++) {
String number = String.valueOf(i);
localWords.put(previousWord, number);
previousWord = number;
}
// store the last key in the map
wordsLast = "9";
// add special chars
if (AndroidKeystoreBrute.disableSpecialChars == false) {
char[] specialChars = {
'!', '"', '@', '#', '$', '%', '&', '/', '{', '>',
'}', '(', ')', '[', ']', '=', '?', '+', '`', '|',
'^', '~', '*', '-', '_', '.', ':', ',', ';', '<',
'\'', '\\',
};
for (int i = 0; i < specialChars.length; i++) {
String character = String.valueOf(specialChars[i]);
localWords.put(previousWord, character);
previousWord = character;
}
wordsLast = "\\";
}
return localWords;
}
// Word permutation methods by Jeff Lauder
private static ArrayList<String> getPermutations(String word) {
ArrayList<String> returnPerms = new ArrayList<String>();
for (String letter : getLetterPermutations(Character.toString(word.charAt(0))))
if (word.length() > 1)
for (String permutation : getPermutations(word.substring(1)))
returnPerms.add(letter + permutation);
else
returnPerms.add(letter);
return returnPerms;
}
private static ArrayList<String> getLetterPermutations(String letter) {
ArrayList<String> returnLetters = new ArrayList<String>();
// then we'll apply them
for (String letterCombo : LetterCombos) {
if (letterCombo.contains(letter)) {
for (char returnletter : letterCombo.toCharArray())
returnLetters.add(Character.toString(returnletter));
return returnLetters;
}
}
// and we'll default to the upper and lower case if not otherwise specified
returnLetters.add(letter.toLowerCase());
returnLetters.add(letter.toUpperCase());
return returnLetters;
}
}