网络扫描程序设计

一、简介

目标主机扫描是网络功防的基础和前提,扫描探测一台目标主机包括:确定该目标主机是否活动、目标主机的操作系统、正在使用哪些端口、对外提供了哪些服务、相关服务的软件版本等等,对这些内容的探测就是为了“对症下药”,为攻防提供参考信息。对主机的探测工具非常多,比如大名鼎鼎的nmap、netcat、superscan,以及国内的x-scanner等等。

二、主机扫描(远程主机探测)

1、通过指定的IP地址范围,发现该范围钟活跃的主机,如192.168.233.0-192.168.233.255

网络扫描程序设计_第1张图片
扫描192.168.233.0~192.168.233.255,收到消息192.168.233.175的端口6060开放

更好的思路是将ip地址转换为整数形式,ip地址范围的遍历就转为在两个数字之间for循环,循环体钟将每一个数字换回ip地址进行处理即可,关键就在于实现ip与数字之间的互相转换:
网络扫描程序设计_第2张图片
最终结果=以上四行或运算

但是!在java钟,二进制最高位1是符号位使用,表示负数,所以我们需要用long类型来存储ip转换的数字,避免出现负数的情况。

ip和长整型互相转化:
public  long ipToLong(String ip) {
  String[] ipArray = ip.split("\\.");
  long num = 0;
  for (int i=0; i<ipArray.length; i++) {
    long valueOfSection = Long.parseLong(ipArray[i]);
    num = (valueOfSection << 8 * (3 - i)) | num;
  }
  return num;
}

/**
 * 长整型转ip  
 */
public  String longToIp(long i) {
//右移,并将高位置0
  return ((i >> 24 ) & 0xFF) + "." +
      ((i >> 16 ) & 0xFF) + "." +
      ((i >>  8 ) & 0xFF) + "." +
      ( i        & 0xFF);
}

三、端口扫描

在目标主机IP文本框中填入活跃主机的ip,再点击扫描。

多线程扫描:即便使用快速扫描的方式,如果扫描的端口范围较大,还是耗时较久。可以结合快速扫描,开启多线程加快速度:每一个线程负责扫描一段范围,分而治之加快速度。
网络扫描程序设计_第3张图片

四、完整代码

IpUtils.java
package chapter09;
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//


public class IpUtils {
    public IpUtils() {
    }

    public static int ipToInt(String ip) {
        String[] ipArray = ip.split("\\.");
        int num = 0;

        for(int i = 0; i < ipArray.length; ++i) {
            int valueOfSection = Integer.parseInt(ipArray[i]);
            num |= valueOfSection << 8 * (3 - i);
        }

        return num;
    }

    public static String intToIp(int ip) {
        StringBuffer sb = new StringBuffer("");
        sb.append(String.valueOf(ip >>> 24));
        sb.append(".");
        sb.append(String.valueOf((ip & 16777215) >>> 16));
        sb.append(".");
        sb.append(String.valueOf((ip & '\uffff') >>> 8));
        sb.append(".");
        sb.append(String.valueOf(ip & 255));
        return sb.toString();
    }

    public static long ipToLong(String strIp) {
        String[] s = strIp.split("\\.");
        long ip = (Long.parseLong(s[0]) << 24) + (Long.parseLong(s[1]) << 16) + (Long.parseLong(s[2]) << 8) + Long.parseLong(s[3]);
        return ip;
    }

    public static String longToIp(long longIp) {
        StringBuffer sb = new StringBuffer("");
        sb.append(String.valueOf(longIp >> 24)).append(".").append(String.valueOf((longIp & 16777215L) >> 16)).append(".").append(String.valueOf((longIp & 65535L) >> 8)).append(".").append(String.valueOf(longIp & 255L));
        return sb.toString();
    }

    public static void main(String[] args) {
        int intIP;
        System.out.println(intIP = ipToInt("192.168.0.1"));
        System.out.println(ipToInt("192.168.0.254"));
        System.out.println(intToIp(intIP));
        System.out.println(ipToLong("192.168.0.127"));
    }
}

HostScannerFX.java
package chapter09;
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.atomic.AtomicInteger;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class HostScannerFX extends Application {
    private TextArea taDisplay = new TextArea();
    private TextField tfStartIp = new TextField("192.168.0.1");
    private TextField tfEndIp = new TextField("192.168.0.158");
    private TextField tfCmd = new TextField();
    private Button btnScan = new Button("主机扫描");
    private Button btnExecute = new Button("执行命令");
    private Button btnStop = new Button("停止");
    private Button btnExit = new Button("退出");
    private ThreadGroup threadGroup = new ThreadGroup("scanThread");
    static AtomicInteger hostCount = new AtomicInteger(0);
    private long startIp;
    private long endIp;

    public HostScannerFX() {
    }

    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage primaryStage) throws Exception {
        BorderPane mainPane = new BorderPane();
        VBox display = new VBox();
        display.setSpacing(10.0D);
        display.setPadding(new Insets(10.0D, 20.0D, 10.0D, 20.0D));
        this.taDisplay.setWrapText(true);
        this.taDisplay.setEditable(false);
        display.getChildren().addAll(new Node[]{new Label("扫描结果:"), this.taDisplay});
        VBox.setVgrow(this.taDisplay, Priority.ALWAYS);
        mainPane.setCenter(display);
        this.btnScan.setOnAction((event) -> {
            this.startIp = IpUtils.ipToLong(this.tfStartIp.getText());
            this.endIp = IpUtils.ipToLong(this.tfEndIp.getText());
            int thread = 4;
            hostCount.set(0);

            for(int i = 0; i < thread; ++i) {
                HostScannerFX.ScanHandler scanHandler = new HostScannerFX.ScanHandler(i, thread);
                (new Thread(this.threadGroup, scanHandler, "MultiThread" + i)).start();
            }

        });
        this.btnExecute.setOnAction((event) -> {
            Thread scanThread = new Thread(this.threadGroup, () -> {
                try {
                    String cmd = this.tfCmd.getText();
                    Process process = Runtime.getRuntime().exec(cmd);
                    InputStream in = process.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(in, "gbk"));

                    String msg;
                    while((msg = br.readLine()) != null) {
                        String finalMsg = msg;
                        Platform.runLater(() -> {
                            this.taDisplay.appendText(finalMsg + "\n");
                        });
                    }
                } catch (IOException var7) {
                    System.err.println(var7.getMessage());
                }

            }, "scanThread");
            scanThread.start();
        });
        this.btnStop.setOnAction((event) -> {
            this.threadGroup.interrupt();
        });
        this.btnExit.setOnAction((event) -> {
            this.threadGroup.interrupt();
            System.exit(0);
        });
        HBox controls = new HBox();
        controls.setSpacing(10.0D);
        controls.setPadding(new Insets(10.0D, 20.0D, 10.0D, 20.0D));
        controls.setAlignment(Pos.CENTER);
        controls.getChildren().addAll(new Node[]{new Label("起始地址:"), this.tfStartIp, new Label("结束地址:"), this.tfEndIp, this.btnScan});
        HBox cmd = new HBox();
        cmd.setSpacing(10.0D);
        cmd.setPadding(new Insets(10.0D, 20.0D, 10.0D, 20.0D));
        cmd.setAlignment(Pos.CENTER);
        cmd.getChildren().addAll(new Node[]{new Label("输入命令格式:"), this.tfCmd, this.btnExecute, this.btnStop, this.btnExit});
        VBox vCmd = new VBox();
        vCmd.setAlignment(Pos.CENTER);
        vCmd.setPrefWidth(500.0D);
        vCmd.getChildren().addAll(new Node[]{controls, cmd});
        mainPane.setBottom(vCmd);
        Scene scene = new Scene(mainPane, 700.0D, 400.0D);
        primaryStage.setOnCloseRequest((event) -> {
            this.threadGroup.interrupt();
            System.exit(0);
        });
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public boolean isReachable(String host) throws IOException {
        int timeOut = 100;
        InetAddress address = InetAddress.getByName(host);
        return address.isReachable(timeOut);
    }

    class ScanHandler implements Runnable {
        private int totalThreadNum;
        private int threadNo;

        public ScanHandler(int threadNo) {
            this.totalThreadNum = 10;
            this.threadNo = threadNo;
        }

        public ScanHandler(int threadNo, int totalThreadNum) {
            this.totalThreadNum = totalThreadNum;
            this.threadNo = threadNo;
        }

        public void run() {
            for(long host = HostScannerFX.this.startIp + (long)this.threadNo; host <= HostScannerFX.this.endIp; host += (long)this.totalThreadNum) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("interrupted!");
                    break;
                }

                try {
                    boolean res = HostScannerFX.this.isReachable(IpUtils.longToIp(host));
                    if (res) {
                        long finalHost = host;
                        Platform.runLater(() -> {
                            HostScannerFX.this.taDisplay.appendText(IpUtils.longToIp(finalHost) + " is reachable.\n");

                            try {
                                Socket socket = new Socket();
                                socket.connect(new InetSocketAddress(String.valueOf(finalHost), 6060), 200);
                                socket.close();
                                String msg = IpUtils.longToIp(finalHost) + "的端口6060开放了\n";
                                Platform.runLater(() -> {
                                    HostScannerFX.this.taDisplay.appendText(msg);
                                });
                            } catch (IOException var5) {
                                var5.printStackTrace();
                            }

                        });
                    }
                } catch (IOException var6) {
                    var6.printStackTrace();
                }

                HostScannerFX.hostCount.incrementAndGet();
            }

            if ((long)HostScannerFX.hostCount.get() == HostScannerFX.this.endIp - HostScannerFX.this.startIp + 1L) {
                HostScannerFX.hostCount.incrementAndGet();
                Platform.runLater(() -> {
                    HostScannerFX.this.taDisplay.appendText("扫描完毕");
                });
            }

        }
    }
}

PortScannerFx.java
package chapter09;
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.atomic.AtomicInteger;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class PortScannerFx extends Application {
    private TextArea taDisplay = new TextArea();
    private TextField tfTargetIp = new TextField("192.168.0.1");
    private TextField tfStartPort = new TextField("1");
    private TextField tfEndPort = new TextField("443");
    private Button btnScan = new Button("扫描");
    private Button btnQuickScan = new Button("快速扫描");
    private Button btnMultiThreadScan = new Button("多线程扫描");
    private Button btnStop = new Button("停止");
    private Button btnClear = new Button("清空");
    private Button btnExit = new Button("退出");
    private ProgressBar progressBar = new ProgressBar(0.0D);
    private Label progressLabel = new Label("0%");
    private String ip;
    private int startPort;
    private int endPort;
    private ThreadGroup threadGroup = new ThreadGroup("scanThread");
    static AtomicInteger portCount = new AtomicInteger(0);

    public PortScannerFx() {
    }

    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage primaryStage) throws Exception {
        BorderPane mainPane = new BorderPane();
        VBox display = new VBox();
        display.setSpacing(10.0D);
        display.setPadding(new Insets(10.0D, 20.0D, 10.0D, 20.0D));
        this.taDisplay.setWrapText(true);
        this.taDisplay.setEditable(false);
        this.taDisplay.setPrefHeight(250.0D);
        this.progressBar.setPrefWidth(600.0D);
        HBox progressBox = new HBox();
        progressBox.setAlignment(Pos.CENTER);
        progressBox.setSpacing(10.0D);
        progressBox.getChildren().addAll(new Node[]{this.progressLabel, this.progressBar});
        display.getChildren().addAll(new Node[]{new Label("端口扫描结果:"), this.taDisplay, progressBox});
        VBox.setVgrow(this.taDisplay, Priority.ALWAYS);
        mainPane.setTop(display);
        HBox params = new HBox();
        params.setSpacing(10.0D);
        params.setPadding(new Insets(10.0D, 20.0D, 10.0D, 20.0D));
        params.setAlignment(Pos.CENTER);
        this.tfStartPort.setPrefWidth(40.0D);
        this.tfEndPort.setPrefWidth(40.0D);
        params.getChildren().addAll(new Node[]{new Label("目标主机IP:"), this.tfTargetIp, new Label("起始端口号:"), this.tfStartPort, new Label("结束端口号:"), this.tfEndPort});
        mainPane.setCenter(params);
        this.btnStop.setDisable(true);
        this.btnScan.setOnAction((event) -> {
            this.ip = this.tfTargetIp.getText();
            this.startPort = Integer.parseInt(this.tfStartPort.getText());
            this.endPort = Integer.parseInt(this.tfEndPort.getText());
            this.progressBar.setProgress(0.0D);
            this.progressLabel.setText("0%");
            int totalPorts = this.endPort - this.startPort + 1;
            Thread scanThread = new Thread(this.threadGroup, () -> {
                System.out.println("Scan start!");

                for(int i = this.startPort; i <= this.endPort; ++i) {
                    if (Thread.currentThread().isInterrupted()) {
                        System.out.println("interrupted!");
                        break;
                    }

                    System.out.println("Scanning port " + i);

                    String msg;
                    try {
                        Socket socket = new Socket(this.ip, i);
                        socket.close();
                        msg = "端口 " + i + " is open\n";
                    } catch (IOException var6) {
                        msg = "端口 " + i + " is not open\n";
                    }

                    this.taDisplay.appendText(msg);
                    double progress = (double)(i - this.startPort) / (double)totalPorts;
                    Platform.runLater(() -> {
                        this.progressBar.setProgress(progress);
                        this.progressLabel.setText((int)(progress * 100.0D) + "%");
                    });
                }

            }, "scanThread");
            this.btnStop.setDisable(false);
            this.btnScan.setDisable(true);
            this.btnQuickScan.setDisable(true);
            this.btnMultiThreadScan.setDisable(true);
            scanThread.start();
        });
        this.btnQuickScan.setOnAction((event) -> {
            this.ip = this.tfTargetIp.getText();
            this.startPort = Integer.parseInt(this.tfStartPort.getText());
            this.endPort = Integer.parseInt(this.tfEndPort.getText());
            this.progressBar.setProgress(0.0D);
            this.progressLabel.setText("0%");
            int totalPorts = this.endPort - this.startPort + 1;
            Thread scanThread = new Thread(this.threadGroup, () -> {
                System.out.println("Scan start!");

                for(int i = this.startPort; i <= this.endPort; ++i) {
                    if (Thread.currentThread().isInterrupted()) {
                        System.out.println("interrupted!");
                        break;
                    }

                    System.out.println("Scanning port " + i);

                    String msg;
                    try {
                        Socket socket = new Socket();
                        socket.connect(new InetSocketAddress(this.ip, i), 200);
                        socket.close();
                        msg = "端口 " + i + " is open\n";
                    } catch (IOException var6) {
                        msg = "端口 " + i + " is not open\n";
                    }

                    this.taDisplay.appendText(msg);
                    double progress = (double)(i - this.startPort) / (double)totalPorts;
                    Platform.runLater(() -> {
                        this.progressBar.setProgress(progress);
                        this.progressLabel.setText((int)(progress * 100.0D) + "%");
                    });
                }

            }, "quickScanThread");
            this.btnStop.setDisable(false);
            this.btnScan.setDisable(true);
            this.btnQuickScan.setDisable(true);
            this.btnMultiThreadScan.setDisable(true);
            scanThread.start();
        });
        this.btnMultiThreadScan.setOnAction((event) -> {
            this.ip = this.tfTargetIp.getText();
            this.startPort = Integer.parseInt(this.tfStartPort.getText());
            this.endPort = Integer.parseInt(this.tfEndPort.getText());
            this.btnStop.setDisable(false);
            this.btnScan.setDisable(true);
            this.btnQuickScan.setDisable(true);
            this.btnMultiThreadScan.setDisable(true);
            int thread = 4;
            portCount.set(0);

            for(int i = 0; i < thread; ++i) {
                PortScannerFx.ScanHandler scanHandler = new PortScannerFx.ScanHandler(i, thread);
                (new Thread(this.threadGroup, scanHandler, "MultiThread" + i)).start();
            }

        });
        this.btnStop.setOnAction((event) -> {
            this.btnStop.setDisable(true);
            this.btnScan.setDisable(false);
            this.btnQuickScan.setDisable(false);
            this.btnMultiThreadScan.setDisable(false);

            try {
                this.threadGroup.list();
                this.threadGroup.interrupt();
            } catch (Exception var3) {
            }

        });
        this.btnExit.setOnAction((event) -> {
            try {
                this.threadGroup.interrupt();
            } catch (Exception var3) {
            }

            System.exit(0);
        });
        this.btnClear.setOnAction((event) -> {
            this.taDisplay.clear();
            this.progressBar.setProgress(0.0D);
            this.progressLabel.setText("0%");
        });
        HBox buttons = new HBox();
        buttons.setAlignment(Pos.CENTER);
        buttons.setSpacing(10.0D);
        buttons.setPadding(new Insets(10.0D, 20.0D, 10.0D, 20.0D));
        buttons.getChildren().addAll(new Node[]{this.btnScan, this.btnQuickScan, this.btnMultiThreadScan, this.btnStop, this.btnClear, this.btnExit});
        mainPane.setBottom(buttons);
        Scene scene = new Scene(mainPane, 700.0D, 400.0D);
        primaryStage.setOnCloseRequest((event) -> {
            try {
                this.threadGroup.interrupt();
            } catch (Exception var3) {
            }

            System.exit(0);
        });
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    class ScanHandler implements Runnable {
        private int totalThreadNum;
        private int threadNo;

        public ScanHandler(int threadNo) {
            this.totalThreadNum = 10;
            this.threadNo = threadNo;
        }

        public ScanHandler(int threadNo, int totalThreadNum) {
            this.totalThreadNum = totalThreadNum;
            this.threadNo = threadNo;
        }

        public void run() {
            System.out.println("thread created");

            for(int port = PortScannerFx.this.startPort + this.threadNo; port <= PortScannerFx.this.endPort; port += this.totalThreadNum) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("interrupted!");
                    break;
                }

                double progress = (double)PortScannerFx.portCount.get() / (double)(PortScannerFx.this.endPort - PortScannerFx.this.startPort + 1);
                Platform.runLater(() -> {
                    PortScannerFx.this.progressBar.setProgress(progress);
                    PortScannerFx.this.progressLabel.setText((int)(progress * 100.0D) + "%");
                });

                try {
                    Socket socket = new Socket();
                    socket.connect(new InetSocketAddress(PortScannerFx.this.ip, port), 200);
                    socket.close();
                    String msg = "端口 " + port + " is open\n";
                    Platform.runLater(() -> {
                        PortScannerFx.this.taDisplay.appendText(msg);
                    });
                } catch (IOException var6) {
                }

                PortScannerFx.portCount.incrementAndGet();
            }

            if (PortScannerFx.portCount.get() == PortScannerFx.this.endPort - PortScannerFx.this.startPort + 1) {
                PortScannerFx.portCount.incrementAndGet();
                Platform.runLater(() -> {
                    PortScannerFx.this.taDisplay.appendText("\n----------------多线程扫描结束--------------------\n");
                    PortScannerFx.this.btnStop.setDisable(true);
                    PortScannerFx.this.btnScan.setDisable(false);
                    PortScannerFx.this.btnQuickScan.setDisable(false);
                    PortScannerFx.this.btnMultiThreadScan.setDisable(false);
                });
            }

        }
    }
}

你可能感兴趣的:(互联网程序设计,网络,java,idea,信息与通信)