Repository navigation
Expand file tree
/
Copy pathPotater.java
More file actions
355 lines (303 loc) · 13.6 KB
/
Copy pathPotater.java
File metadata and controls
355 lines (303 loc) · 13.6 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.*;
import java.util.Enumeration;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.ArrayList;
// Error formatting: e[error #][exception initials]
public class Potater {
public static void styleTextField(JTextField textField, Integer x, Integer y, Integer width, Integer height, String tipText, String placeHolder) {
textField.setBounds(x, y, width, height);
textField.setBackground(new java.awt.Color(43, 70, 204));
textField.setForeground(java.awt.Color.GRAY);
textField.setBorder(BorderFactory.createLineBorder(java.awt.Color.BLACK));
textField.setCaretColor(java.awt.Color.WHITE);
textField.setSelectionColor(java.awt.Color.YELLOW);
textField.setText(placeHolder);
textField.setToolTipText(tipText);
textField.addFocusListener(new java.awt.event.FocusListener() {
@Override
public void focusGained(java.awt.event.FocusEvent e) {
if (textField.getText().equals(placeHolder)) {
textField.setForeground(java.awt.Color.WHITE);
textField.setText("");
}
}
@Override
public void focusLost(java.awt.event.FocusEvent e) {
if (textField.getText().equals("")) {
textField.setForeground(java.awt.Color.GRAY);
textField.setText(placeHolder);
}
}
});
}
public static void UDP(String ip, Integer PKSize, Integer Time, Integer Delay, JLabel Terminal) throws IOException, InterruptedException {
try {
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
InetAddress localAddress = networkInterface.getInetAddresses().nextElement();
try (DatagramSocket door = new DatagramSocket(0, localAddress)) {
// UPDATE: Make it already have the bytes with a comment of what it means
// Avoids .getBytes() each time it starts up
byte[] buffer = "Hello From Potater :) 🥔🥔🥔🥔".getBytes();
InetAddress address;
address = InetAddress.getByName(ip);
if (address instanceof Inet6Address) {
Inet6Address ipv6Address = (Inet6Address) address;
if (ipv6Address.isLinkLocalAddress() && !ip.contains("%")) {
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
if (ni != null) {
address = Inet6Address.getByAddress(null, address.getAddress(), ni.getIndex());
} else {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
if (interfaces.hasMoreElements()) {
ni = interfaces.nextElement();
address = Inet6Address.getByAddress(null, address.getAddress(), ni.getIndex());
}
}
}
} else if (address != null && address.isLinkLocalAddress()) {
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
if (ni != null) {
if (address instanceof Inet6Address) {
address = Inet6Address.getByAddress(null, address.getAddress(), ni.getIndex());
} else {
address = InetAddress.getByAddress(null, address.getAddress());
}
} else {
// Try to find any suitable network interface
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
if (interfaces.hasMoreElements()) {
ni = interfaces.nextElement();
address = InetAddress.getByAddress(ni.getName(), address.getAddress());
} else {
throw new SocketException("No suitable network interface found for link-local address");
}
}
}
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, 42069);
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
Runnable task = () -> {
try {
long endTime = System.currentTimeMillis() + Time;
while (System.currentTimeMillis() < endTime) {
door.send(packet);
addText(Terminal, "Sent packet!");
}
} catch (IOException ex) {
addText(Terminal, "Error e1IO: " + ex + "<br>Please fill out a bug report here:<br>github.com/TokynBlast/Potater/issues");
}
};
if (Delay >= 0) {
executor.scheduleAtFixedRate(task, 0, Delay, TimeUnit.MILLISECONDS);
executor.awaitTermination(Time + (Delay / 1000), TimeUnit.SECONDS);
} else {
throw new IllegalArgumentException("Delay must be non-negative and non-zero");
}
executor.shutdown();
} finally {}
} catch (SocketException ex) {
addText(Terminal, "Error e2SE: " + ex + "<br>Please fill out a bug report here:<br>github.com/TokynBlast/Potater/issues");
} finally {}
}
public static void TCP(String ip, Integer PKSize, Integer Time, Integer Delay, JLabel Terminal) {
try {
// Create a socket to connect to the specified IP and port
try (Socket socket = new Socket(ip, 42069)) {
byte[] buffer = new byte[PKSize * 1024]; // Convert kB to bytes
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
Runnable task = () -> {
try {
long endTime = System.currentTimeMillis() + Time * 1000; // Convert seconds to milliseconds
OutputStream outputStream = socket.getOutputStream();
while (System.currentTimeMillis() < endTime) {
outputStream.write(buffer);
outputStream.flush();
addText(Terminal, "Sent TCP packet!");
Thread.sleep(Delay);
}
} catch (IOException ex) {
addText(Terminal, "Error e3IO: " + ex + "<br>Please fill out a bug report here:<br>github.com/TokynBlast/Potater/issues");
} catch (InterruptedException ex) {
Thread.currentThread().interrupt(); // Restore interrupted status
}
};
executor.schedule(task, 0, TimeUnit.MILLISECONDS);
executor.awaitTermination(Time + (Delay * 1000), TimeUnit.MILLISECONDS);
executor.shutdown();
}
} catch (IOException ex) {
addText(Terminal, "Error e2IO: " + ex + "<br>Please fill out a bug report here:<br>github.com/TokynBlast/Potater/issues");
}
}
public static void ICMP(String ip, Integer PKSize, Integer Time, Integer Delay, JLabel Terminal){
}
public static void Start(String ip, Integer PKSize, Integer Time, Integer Delay, String Type, JLabel Terminal) throws IOException, InterruptedException {
addText(Terminal, "Beginning attack.");
if (Type.equals("UDP")) {
UDP(ip, PKSize, Time, Delay, Terminal);
} else if (Type.equals("TCP")) {
TCP(ip, PKSize, Time, Delay, Terminal);
} else if (Type.equals("ICMP")) {
ICMP(ip, PKSize, Time, Delay, Terminal);
}
}
public static void Stop() {
// Stop the network stress test
}
private static ArrayList<String> textLines = new ArrayList<>();
public static void addText(JLabel label, String newText) {
textLines.add(newText);
while (textLines.size() > 11) {
textLines.remove(0);
}
StringBuilder sb = new StringBuilder("<html>");
for (String line : textLines) {
sb.append(line).append("<br>");
}
sb.append("</html>");
label.setText(sb.toString());
}
public static void main(String[] args) {
System.out.println("Creating Window");
JFrame window = new JFrame();
window.setTitle("Potater - Java Network Stressor - v0.1.1");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
window.setSize(500, 500);
window.setLayout(null);
System.out.println("Creating GUI");
JComboBox<String> attack = new JComboBox<>();
JComboBox<String> dataType = new JComboBox<>();
JTextField IP = new JTextField();
JTextField PacketSize = new JTextField();
JTextField Time = new JTextField();
JTextField Delay = new JTextField();
JButton BeginEnd = new JButton();
JLabel PacketSent = new JLabel();
JLabel PacketReceived = new JLabel();
JLabel PacketLoss = new JLabel();
JLabel TimeLeft = new JLabel();
JCheckBox Fragging = new JCheckBox();
JCheckBox TermDebugging = new JCheckBox(); // Print lines to the terminal, as they go inside the terminal inside the Potater window.
JLabel term = new JLabel();
System.out.println("Styling GUI");
term.setOpaque(true);
term.setBounds(50, 244, 400, 194);
term.setBackground(Color.BLACK);
term.setForeground(Color.WHITE);
term.setFont(new Font("Monospaced", Font.PLAIN, 14));
term.setVerticalAlignment(SwingConstants.BOTTOM);
term.setHorizontalAlignment(SwingConstants.LEFT);
Fragging.setText("Allow fraggmenting?");
PacketSent.setText("Packets Sent: 0");
PacketReceived.setText("Packets Received: 0");
PacketLoss.setText("Packet Loss: 0%");
TimeLeft.setText("Time Left: --:--:--");
BeginEnd.setText("Begin");
BeginEnd.setBounds(385, 100, 90, 25);
attack.setBounds(0, 0, 90, 40);
attack.addItem("Custom");
attack.addItem("Self");
attack.addItem("WiFi");
dataType.setBounds(100, 0, 90, 40);
dataType.addItem("UDP");
dataType.addItem("TCP");
// dataType.addItem("ICMP");
styleTextField(IP, 10, 100, 140, 25, "IP address to send packets to", "192.168.1.1");
styleTextField(PacketSize, 165, 100, 50, 25, "Size of packets to send in kB", "65"); // NEED TO ADD java.net.getMTU() TO PREVENT FRAGMENTING!! (With option for fragmenting if user wants it)
styleTextField(Time, 230, 100, 50, 25, "Amount of time to send packets for in seconds", "60");
styleTextField(Delay, 295, 100, 70, 25, "Delay between sending packets in seconds", "1000");
System.out.println("Adding actions to GUI");
BeginEnd.addActionListener((ActionEvent e) -> {
if (BeginEnd.getText().equals("Begin")) {
BeginEnd.setText("Stop");
SwingWorker<Void, Void> worker = new SwingWorker<>() {
@Override
protected Void doInBackground() throws Exception {
try {
Start(IP.getText(),
Integer.parseInt(PacketSize.getText()),
Integer.valueOf(Time.getText()), Integer.valueOf(Delay.getText()),
dataType.getSelectedItem().toString(), term);
} catch (IOException ex) {
addText(term, "Error e4IO: " + ex + "<br>Please fill out a bug report here:<br>github.com/TokynBlast/Potater/issues");
} catch (InterruptedException ex) {
addText(term, "Error e5IE: " + ex + "<br>Please fill out a bug report here:<br>github.com/TokynBlast/Potater/issues");
}
return null;
}
@Override
protected void done() {
BeginEnd.setText("Begin");
addText(term, "Attack over.");
}
};
worker.execute();
} else if (BeginEnd.getText().equals("Stop")) {
BeginEnd.setText("Begin");
Stop();
}
});
attack.addActionListener((ActionEvent e) -> {
if (attack.getSelectedItem().equals("Custom")) {
IP.setEditable(true);
IP.setText("192.168.1.1");
}
else if (attack.getSelectedItem().equals("WiFi")) {
try {
// String name_ = "";
Enumeration<NetworkInterface> Net = NetworkInterface.getNetworkInterfaces();
while (Net.hasMoreElements()) {
NetworkInterface currentInterface = Net.nextElement();
Enumeration<InetAddress> addresses = currentInterface.getInetAddresses();
if (currentInterface.isUp() && !currentInterface.isLoopback() && !currentInterface.isVirtual() && !currentInterface.isPointToPoint()) {
String displayName = currentInterface.getDisplayName().toLowerCase();
if (!displayName.contains("virtual") && !displayName.contains("vmware") && !displayName.contains("vbox")) {
if (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
String ipAddress = address.getHostAddress();
// name_ = currentInterface.getName(); // Needs to get WiFi Name, not type.
if (address instanceof Inet6Address) {
ipAddress = ipAddress.split("%")[0]; // Remove anything after %
ipAddress = ipAddress.replaceAll("[^0-9a-fA-F:]", ""); // Remove anything not a colon, number or character
} else if (address instanceof Inet4Address) {
ipAddress = ipAddress.replaceAll("[^0-9.]", ""); // Remove anything not a number or dot
}
IP.setText(ipAddress);
}
}
}
}
IP.setEditable(false);
addText(term, "Successfully Grabbed WiFi IP: "); // + name_
} catch (SocketException ex) {
IP.setText("No WiFi");
IP.setEditable(false);
addText(term, "Couldn't find WiFi IP.");
} finally {}
}
else if (attack.getSelectedItem().equals("Self")) {
IP.setText("::1");
IP.setEditable(false);
}
});
System.out.println("Applying GUI");
window.getContentPane().setBackground(new java.awt.Color(247, 183, 21));
window.add(PacketSize);
window.add(Time);
window.add(Delay);
window.add(IP);
window.add(attack);
window.add(dataType);
window.add(BeginEnd);
window.add(term);
addText(term, "Successfully started Potater!");
window.setVisible(true);
System.out.println("Potater started! Have fun! :)");
}
}