-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReverseHashForm.cs
239 lines (215 loc) · 7.68 KB
/
ReverseHashForm.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace ReverseHash
{
public partial class ReverseHashForm : Form, IAppendText
{
public ReverseHashForm()
{
InitializeComponent();
}
protected override void OnHandleCreated(EventArgs e)
{
//https://stackoverflow.com/questions/57124243/winforms-dark-title-bar-on-windows-10 (thank you:)
if (DwmSetWindowAttribute(this.Handle, 19, new[] { 1 }, 4) != 0)
DwmSetWindowAttribute(this.Handle, 20, new[] { 1 }, 4);
base.OnHandleCreated(e);
}
[System.Runtime.InteropServices.DllImport("DwmApi")] //System.Runtime.InteropServices
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, int[] attrValue, int attrSize);
#region IAppendText Members
public void AppendText(string s)
{
if (IsHandleCreated && InvokeRequired)
{
// Invoke an anonymous method on the thread of the form.
this.Invoke((MethodInvoker)delegate
{
AppendText(s);
});
}
else
{
try
{
txtOutput.AppendText(s);
}
catch {
Console.WriteLine("ReverseHashForm.AppendText: Error Appending Text");
}
}
}
private void SetGoButtonText(string s)
{
if (IsHandleCreated && InvokeRequired)
{
// Invoke an anonymous method on the thread of the form.
this.Invoke((MethodInvoker)delegate {
SetGoButtonText(s);
});
}
else
{
btnGo.Text = s;
}
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
try
{
if (workerThread != null && workerThread.IsAlive)
{
workerThread.Abort();
workerThread = null;
}
}
catch
{
Console.WriteLine("ReverseHashForm.OnClosing: Error closing worker thread");
}
}
#endregion
Thread workerThread = null;
private void btnGo_Click(object sender, EventArgs e)
{
if (workerThread == null)
{
txtOutput.Clear();
_prefix = txtPrefix.Text;
string hashStr = txtHash.Text;
if (hashStr.StartsWith("0x"))
_hashToLookUp = Convert.ToUInt32(hashStr.Substring(2), 16);
else
_hashToLookUp = Convert.ToUInt32(hashStr);
_maxLen = (int)spinLen.Value;
_possibleChars = txtPossibleCharacters.Text;
btnGo.Text = "Stop";
workerThread = new Thread(new ThreadStart(DoWork));
workerThread.Start();
}
else
{
workerThread.Abort();
workerThread = null;
SetGoButtonText("&Go");
AppendText("Worker Thread killed");
}
}
private string _prefix = "";
private string _possibleChars = "";
private UInt32 _hashToLookUp = 0;
int _maxLen = 0;
private void DoWork()
{
HashReverse hr = new HashReverse();
hr.AppendObj = this;
//AppendText( string.Format("Running command:\n ReverseHash 0x{0} -p:{1} -c:{2} -l:{3}\n",
// _hashToLookUp, _prefix, _possibleChars, _maxLen));
hr.PrintMatches(_hashToLookUp, _possibleChars,
_prefix.Length + 1, _prefix.Length + _maxLen, _prefix);
SetGoButtonText("&Go");
}
private void bthShowCmdLine_Click(object sender, EventArgs e)
{
_prefix = txtPrefix.Text;
string hashStr = txtHash.Text;
if (hashStr.StartsWith("0x"))
_hashToLookUp = Convert.ToUInt32(hashStr.Substring(2), 16);
else
_hashToLookUp = Convert.ToUInt32(hashStr);
_maxLen = (int)spinLen.Value;
_possibleChars = txtPossibleCharacters.Text;
AppendText(string.Format(" ReverseHash.exe 0x{0} -p:{1} -c:{2} -l:{3}\n",
_hashToLookUp, _prefix, _possibleChars, _maxLen));
}
}
public interface IAppendText
{
void AppendText(string s);
}
public class HashReverse
{
private IAppendText appendObj = null;
public IAppendText AppendObj
{
get { return appendObj; }
set { appendObj = value; }
}
void Print(string s)
{
if (!s.EndsWith("\n"))
s += "\n";
if (appendObj == null)
Console.Write(s);
else
appendObj.AppendText(s);
}
uint HashString(char[] input)
{
uint FNV_prime = 16777619;
uint offset_basis = 2166136261;
uint hash = offset_basis;
byte c = 0;
foreach (char c_ in input)
{
c = (byte)c_;
c |= 0x20;
hash ^= c;
hash *= FNV_prime;
}
return hash;
}
public void PrintMatches(
UInt32 hash,
string possibleCharacters,
int targetStringSizeBegin,
int targetStringSizeEnd,
string prefix
)
{
DateTime start = DateTime.Now;
Print(string.Format("Searching matches for 0x{0:x}\n with prefix '{1}'\n using char set '{2}'\n startLen: {3}\n endLen: {4} (range: {5})\n",
hash, prefix, possibleCharacters, targetStringSizeBegin, targetStringSizeEnd, (targetStringSizeEnd - targetStringSizeBegin)));
for (int i = targetStringSizeBegin; i < targetStringSizeEnd; i++)
{
Print(string.Format("searching string length = {0}; guess_len:{1}", i, i - prefix.Length));
PrintMatches(hash, possibleCharacters, i, prefix);
}
DateTime end = DateTime.Now;
var timeTaken = end - start;
Print(string.Format("Finished Searching matches for 0x{0:x}; time: {1}ms ", hash, timeTaken.TotalMilliseconds));
}
void PrintMatches(uint hash, string possibleCharacters, int targetStringSize, string prefix)
{
char[] bufferToUse = new char[targetStringSize];
prefix.CopyTo(0, bufferToUse, 0, prefix.Length);
GenerateStringsAndCheckHash(prefix.Length, targetStringSize, hash, possibleCharacters, bufferToUse);
}
void GenerateStringsAndCheckHash(int index, int targetStringSize, uint targetHash, string possibleCharacters, char[] bufferToUse)
{
if (index == targetStringSize)
{
if (HashString(bufferToUse) == targetHash)
{
Print(string.Format("Match found: '{0}'", new string(bufferToUse)));
}
}
else
{
foreach (char c in possibleCharacters)
{
bufferToUse[index] = c;
GenerateStringsAndCheckHash(index + 1, targetStringSize, targetHash, possibleCharacters, bufferToUse);
}
}
}
}
}