-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
265 lines (247 loc) * 11.5 KB
/
Main.java
File metadata and controls
265 lines (247 loc) * 11.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
import java.io.*;
import java.nio.file.*;
import java.nio.*;
import java.net.Socket;
import java.util.Scanner;
public class Main {
public static String host = "localhost";
public static int port = 31337;
public static String character = "A";
public static void main(final String[] args) {
final Scanner scanner = new Scanner(System.in);
if(args.length < 2) {
System.out.print("ip:");
host = scanner.nextLine();
System.out.print("port:");
port = Integer.parseInt(scanner.nextLine());
} else {
host = args[0];
port = Integer.parseInt(args[1]);
if(args.length == 3) {
System.out.println("Starting 1337 mode...");
try{specialMode();}catch(Exception e){e.printStackTrace();}
return;
}
}
try {
final Socket socket = new Socket(host, port);
System.out.println("Established connection with " + host + ":" + port + "...");
Thread read = new Thread(new Runnable() {public void run() {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e2) {
try {
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
System.exit(1);
}
scanner.close();
System.err.println("[Error] Failed to open input stream.");
System.exit(1);
}
String str = "";
try {
while ((str = br.readLine()) != null) {
System.out.println(str);
}
} catch (IOException e1) {
e1.printStackTrace();
}
try {
br.close();
} catch (IOException e) {
System.err.println("[Error] Socket closed.");
System.exit(1);
}
}});
Thread write = new Thread(new Runnable() {public void run() {
PrintWriter writer = null;
try {
writer = new PrintWriter(socket.getOutputStream(), true);
} catch (IOException e) {
try {
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
scanner.close();
System.err.println("[Error] Failed to open output stream.");
System.exit(1);
}
while(true) {
String x = scanner.nextLine();
if(x.startsWith("^")) {
if(x.equalsIgnoreCase("^re")) {
Main.main(args);
} else {
System.out.println("Unknown command. Try ^re to reconnect.");
}
} else {
writer.println(x);
System.out.println("[Sent!]");
}
}
}});
read.start();
write.start();
} catch(Exception e) {
System.err.println("[Error] Unable to establish connection.");
System.exit(1);
}
}
public static void specialMode() throws Exception {
final Scanner scanner = new Scanner(System.in);
try {
Thread write = new Thread(new Runnable() {
BufferedReader br = null;
public void run(){
try {
Socket socket = new Socket(host, port);
PrintWriter writer = null;
try {
writer = new PrintWriter(socket.getOutputStream(), true);
} catch (IOException e) {
try {
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
scanner.close();
System.err.println("[Error] Failed to open output stream.");
System.exit(1);
}
int i = 1;
while(true) {
System.out.print("rwsocket>");
String cmd = scanner.nextLine();
if(cmd.equalsIgnoreCase("payload"))
System.out.println(new String(new char[i]).replaceAll("\0", character) + "\n");
else if(cmd.equalsIgnoreCase("strlen"))
System.out.println(i + "\n");
else if(cmd.equalsIgnoreCase("help") || cmd.equalsIgnoreCase("?"))
System.out.println("[Commands]\nhelp - Displays this menu\nstrlen - Display the length of the current payload\npayload - Display current payload\njump - set length of payload\n" );
else if(cmd.toLowerCase().startsWith("jump")) {
String[] args = cmd.split(" ");
if(args.length == 1)
System.out.println("Number argument missing.\n");
else {
try {
i = Integer.parseInt(args[1]);
System.out.println("Payload length is now " + args[1] + "\n");
} catch(Exception e) {
System.out.println("Invalid number.\n");
}
}
} else if(cmd.toLowerCase().startsWith("file")) {
String[] args = cmd.split(" ");
if(args.length == 1)
System.out.println("String argument missing.\n");
else {
try {
String content = new String(Files.readAllBytes(Paths.get(new File(args[1]).getAbsolutePath())));
writer.println(content);
socket.close();
socket = new Socket(host, port);
try {
writer = new PrintWriter(socket.getOutputStream(), true);
} catch (IOException e) {
try {
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
scanner.close();
System.err.println("[Error] Failed to open output stream.");
System.exit(1);
}
try {
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e2) {
try {
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
System.exit(1);
}
scanner.close();
System.err.println("[Error] Failed to open input stream.");
System.exit(1);
}
String str = "";
try {
while (br.ready()) {
str = br.readLine();
/*if(str.contains("Closing")) {
System.out.println("[debug] skipping");
break;}*/
System.out.println(str);
}
} catch (IOException e1) {
e1.printStackTrace();
}
i++;
} catch(Exception e) {
e.printStackTrace();
System.out.println("Unknown error.\n");
}
}
} else if(cmd.length() > 0)
System.out.println("Unknown command. Try typing `help` for the help menu.\n");
else {
System.out.print(String.format("\033[%dA",1));
System.out.print("\033[2K"); // Erase line content
String payload = new String(new char[i]).replaceAll("\0", character);
writer.println(payload);
socket.close();
socket = new Socket(host, port);
try {
writer = new PrintWriter(socket.getOutputStream(), true);
} catch (IOException e) {
try {
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
scanner.close();
System.err.println("[Error] Failed to open output stream.");
System.exit(1);
}
try {
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e2) {
try {
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
System.exit(1);
}
scanner.close();
System.err.println("[Error] Failed to open input stream.");
System.exit(1);
}
String str = "";
try {
while (br.ready()) {
str = br.readLine();
/*if(str.contains("Closing")) {
System.out.println("[debug] skipping");
break;}*/
System.out.println(str);
}
} catch (IOException e1) {
e1.printStackTrace();
}
i++;
}
}
}catch(Exception e) {e.printStackTrace();}
}});
write.start();
} catch(Exception e) {
System.err.println("[Error] Unable to establish connection.");
System.exit(1);
}
}
}