Author: Stewart Moss How to create a simple brute forcing engine in a delphi class. Answer: 1 {-------------------------------------------------------------------------- 2 --- 3 Unit Name: classThreadBruteForce 4 5 Version: 1.0 6 7 Release Date: 21-Aug-2002 8 9 Compiler directives: 10 11 TINY - removes unnessecary error messages. test that output 12 is not empty 13 and 14 OPTIMIZE (less information is available) 15 16 Purpose: 17 18 Description: 19 20 A TThread which generates brute force combinations through the 21 onDo event. 22 23 Notes: 24 Charset contains the characters (these are sorted internally) 25 onFinished event provided 26 27 Not exactly fast but it does the job. 28 29 Use it like this:- 30 31 brute := TBruteThread.create(true); 32 brute.charset := 'abcdefghijklmnopqrstuvwxyz'; // Chars to brute 33 brute.numCharacters := 5; // Max chars 34 brute.onDo := Form1ThreadOnDo; 35 brute.resume; 36 37 Dependancies: 38 39 History: 40 41 Copyright 2002 by Stewart Moss 42 All rights reserved. 43 -----------------------------------------------------------------------------} 44 45 unit classBruteForce; 46 47 interface 48 uses classes, sysutils; 49 50 type 51 TBruteThread = class(TThread) 52 private 53 FNumChars: Integer; 54 FCharset: string; 55 FonDo: TNotifyEvent; 56 FonFinished: TNotifyEvent; 57 58 CharCount: string; 59 minChar: char; 60 maxChar: char; 61 imaxChar: integer; 62 63 incBruteLock: boolean; 64 // locks the incBrute function 65 66 procedure init; 67 function incBrute(posi: integer): integer; 68 function StringBubbleSort(StrIn: string): string; 69 70 public 71 {$IFNDEF OPTIMIZE} 72 BruteCount: integer; // not recommended on large bruteforce, 73 // use your own counter 74 {$ENDIF} 75 BruteResult: string; 76 77 procedure execute; override; 78 79 published 80 81 property onDo: TNotifyEvent read FonDo write FonDo; 82 property onFinished: TNotifyEvent read FonFinished write FonFinished; 83 84 property CharSet: string read FCharset write FCharset; 85 property numCharacters: Integer read FNumChars write FNumChars; 86 87 end; 88 89 implementation 90 91 { TBruteThread } 92 93 procedure TBruteThread.execute; 94 var 95 loop: integer; 96 tmpstr: string; 97 begin 98 if FNumChars <= 0 then 99 begin 100 {$IFNDEF TINY} 101 raise exception.create('invalid Numchars'); 102 {$ENDIF} 103 {$IFDEF TINY} 104 exit; 105 {$ENDIF} 106 end; 107 108 if FCharSet = '' then 109 begin 110 {$IFNDEF TINY} 111 raise exception.create('Charset is blank'); 112 {$ENDIF} 113 {$IFDEF TINY} 114 exit; 115 {$ENDIF} 116 end; 117 118 init; 119 120 while (not terminated) do 121 begin 122 if incbrute(1) > FNumChars then 123 break; 124 125 loop := 0; 126 bruteresult := ''; 127 while loop < FNumChars do 128 begin 129 inc(loop); 130 if charcount[loop] = #0 then 131 break; 132 133 // speed optimization 134 tmpstr := BruteResult; 135 BruteResult := tmpstr + charcount[loop]; 136 end; 137 {$IFNDEF OPTIMIZE} 138 inc(Brutecount); 139 {$ENDIF} 140 141 if assigned(onDo) then 142 onDo(Self); 143 end; 144 145 if assigned(onFinished) then 146 onFinished(Self); 147 end; 148 149 {----------------------------------------------------------------------------- 150 Procedure: incBrute 151 Arguments: posi: integer 152 Result: integer 153 154 Purpose: Recurive 155 156 Description: 157 This function brutes 158 159 Copyright 2002 by Stewart Moss 160 All rights reserved. 161 -----------------------------------------------------------------------------} 162 163 function TBruteThread.incBrute(posi: integer): integer; 164 var 165 tmpint: integer; 166 bufferpos: integer; 167 begin 168 result := posi; 169 bufferpos := pos(charcount[posi], FCharset); 170 charcount[posi] := FCharset[bufferpos + 1]; 171 if FCharset[Bufferpos] = maxchar then 172 begin 173 charcount[posi] := minchar; 174 tmpint := incBrute(posi + 1); 175 if tmpint > FnumChars then 176 result := tmpint; 177 end; 178 end; 179 180 procedure TBruteThread.init; 181 var 182 loop: integer; 183 begin 184 FCharSet := StringBubbleSort(FCharset); 185 minchar := FCharset[1]; 186 maxChar := FCharset[length(FCharset)]; 187 imaxchar := ord(MaxChar); 188 charcount := ''; 189 for loop := 1 to FNumChars do 190 begin 191 charcount := charcount + #0; 192 end; 193 {$IFNDEF OPTIMIZE} 194 Brutecount := 0; 195 {$ENDIF} 196 197 end; 198 199 function TBruteThread.StringBubbleSort(StrIn: string): string; 200 var 201 i, j: Integer; 202 temp: Char; 203 tmplen: integer; 204 begin 205 tmplen := length(StrIn); 206 for i := 1 to tmplen do 207 for j := 1 to tmplen do 208 if strIn[i] < StrIn[j] then 209 begin 210 temp := StrIn[i]; 211 StrIn[i] := StrIn[j]; 212 StrIn[j] := temp; 213 end; 214 Result := strIn; 215 end; 216 217 end.