这是我第一次在这里发帖,所以我不太确定该说/问什么。
无论如何,我正在尝试制作一个简单的Java程序,从该程序运行命令提示符命令,主要用于ping洪水攻击(ping flooding myself)。
以下是我的当前代码:
尽管现在当我尝试使用那个IP和数据包数量时,它返回“null - 0”(从exec方法),我甚至不确定它是否与ping有关。
我想要实现的是如我所说的那样,对自己进行ping flood,然后输出我得到的任何响应,虽然我不知道这段代码是否与此相关,我在编写Java代码时主要使用逻辑思维。
有人能解释一下为什么我的代码不按照我想要的那样工作吗?请不要批评,因为我已经说过了,我对java编程还很陌生。
编辑:以下是我试图实现的简要“信息”说明。
1. 我输入一个IP地址和要发送的数据包数量。在这个说明中,我使用本地主机IP和5个数据包。
2. 我开始攻击。在这部分中,我希望程序运行cmd提示符命令
ping 127.0.0.1 -t -n 5
其中,127.0.0.1是我在程序的文本框中输入的IP,5是我在文本框中输入的数据包数量。
3. 我开始攻击,所以在命令提示符中应该发生以下事情:
语言是芬兰语,但仍然是同样的东西。
这是我尝试实现的基本说明,希望有人理解并能够帮助/告诉我为什么我的代码不起作用,或者正在工作但没有在eclipse控制台中打印正确的行。
以下是我的当前代码:
public class Core extends JFrame {
JTextField ipTextField;
int packets = 0;
boolean running = false;
public Core() {
super("Fatique");
Container container = getContentPane();
JButton bAttack = new JButton("Start Attack");
JButton bStop = new JButton("Stop Attack");
JPanel jPanel = new JPanel();
container.setLayout(new FlowLayout());
ipTextField = new JTextField("IP Address", 30);
container.add(ipTextField);
bAttack.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String input = ipTextField.getText();
String[] value = input.split(":");
int amountOfPackets = Integer.parseInt(value[1]);
exec("cmd /c" + input + " -t -n " + amountOfPackets);
running = true;
}
});
bStop.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
stop();
}
});
if(!running) {
jPanel.add(bAttack);
} else {
jPanel.add(bStop);
}
add(jPanel);
}
public void exec(String cmd) {
try {
Process p = Runtime.getRuntime().exec(cmd);
System.out.println(getOutput(p) + " - " + getPacketsSent());
} catch (IOException e) {
e.printStackTrace();
}
}
public String getOutput(Process p) {
String output = null;
try {
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
output = line;
packets++;
}
return output;
} catch (IOException e) {
System.err.println(e.getStackTrace());
}
return null;
}
public int getPacketsSent() {
return packets;
}
public void stop() {
exec("cmd /c break");
running = false;
}
public static void main(String[] args) {
Core c = new Core();
c.setSize(500, 300);
c.setVisible(true);
c.setResizable(false);
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setLocationRelativeTo(null);
}
我对Java还很陌生,所以可能无法按照我想要的方式运行。 我想要做的是在文本框中输入IP地址,并用“:”分割它,然后输入数据包数量,例如:
127.0.0.1:100
尽管现在当我尝试使用那个IP和数据包数量时,它返回“null - 0”(从exec方法),我甚至不确定它是否与ping有关。
我想要实现的是如我所说的那样,对自己进行ping flood,然后输出我得到的任何响应,虽然我不知道这段代码是否与此相关,我在编写Java代码时主要使用逻辑思维。
public String getOutput(Process p) {
String output = null;
try {
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
output = line;
packets++;
}
return output;
} catch (IOException e) {
System.err.println(e.getStackTrace());
}
return null;
}
有人能解释一下为什么我的代码不按照我想要的那样工作吗?请不要批评,因为我已经说过了,我对java编程还很陌生。
编辑:以下是我试图实现的简要“信息”说明。
1. 我输入一个IP地址和要发送的数据包数量。在这个说明中,我使用本地主机IP和5个数据包。
![About to send 5 packets to localhost ip](https://istack.dev59.com/xKXbl.webp)
3. 我开始攻击,所以在命令提示符中应该发生以下事情:
![5 packets sent to locahost](https://istack.dev59.com/SC3wz.webp)