[Network security course design] JAVA-based system port scanning software design and implementation (java code + IDEA + UI graphical interface + experiment report)

Link:: l Design and implementation of system port scanning software based on JAVA (java code + IDEA + UI graphical interface + experiment report)

Design and Implementation of System Port Scanning Software

  • Design purpose and tasks

Referring to the operation of port scanning software such as superscan and nmap, design a simple port scanning software by yourself, which can detect the open ports of the target host according to detection methods such as TCP and IMCP.

Require:

(1) Use ping scan to test the connection status of the target host. If the ping fails, it will show that the host is not in the network.

(2) If the ping is reachable, design a program to scan the port of the target host, display the scan results of common ports, and identify the type of the target operating system.

(3) Use multi-threading to scan multiple hosts at the same time. The design program scans the IP address (single IP, a range of IP) and the port of the specified host name (specified port, all ports) to obtain relevant information.

(4) Friendly graphical user interface, which can display the scanning progress, scanning time, and abnormal alarm window (such as IP address range out of bounds, etc.) during the scanning process, and realize it in the LAN segment.

3. Experimental environment and overall design

Experimental environment: Window XP system, java virtual machine, JDK1.8.1 and IDEA.2021.1

Overall design: The port scanning system is divided into two interfaces, namely the user login interface and the function interface. In the user login interface, a login account password management verification mechanism is set up to make the use of the software more rigorous and reliable; the functional interface is a complete introduction composed of menu bar, IP scan, port scan and scan result controls, etc. Full-featured operation interface, fully starting from the user's experience, to maximize the satisfaction of the user's needs.

5. Result testing and analysis

Login interface:

  The first thing to open is the account login interface, which consists of account input text box, password input text box, interface title, minimize button, login button and close button. The background color is off-white, the text size is moderate, and the font is Microsoft Yahei, centered. . The user needs to enter the correct account number and password in the text box, and then click the "Login" button to compare it with the account password preset in the background. Only after the verification is correct can the next operation interface be entered. If the account number is entered incorrectly, an error warning window will pop up; if the password is entered incorrectly, a warning window will pop up. Only when the account number and password are entered correctly can the operation interface be entered.

   

Account input error: the correct account is "admin"

           Password input error: The correct password is "123456"

                 

           Authentication succeeded:  

Operation interface :

  After passing the security verification, the port scanning operation can be started. The operation interface consists of title, menu bar, scan mode selection (IP scan, port scan), ip address segment input box, port number input box, thread number input box, start scan button, exit button, clear button, scan result display area. Select the scan method according to the user's needs: scan the ip address or scan the port number of the destination host. If you choose to scan the ip address, you can continue to choose to scan a single ip address, or to scan a segment of ip addresses. In addition, it will also check the validity of the input ip address, otherwise it will display an ip input error warning; if you choose The most important thing is to scan the port. First, you need to input the ip address of the destination host. After passing the legality check of the ip address, you can choose to scan the common port or the specified port. The software supports multi-threaded scanning, you can customize the maximum number of threads, and use multi-threaded to improve the time efficiency of port scanning. The scan result will display the scan time, whether the ip address is reachable, the port opening status, and the operating system type of the destination host in the lower part of the operation page.

   

Scan IP address for possible exceptions: catch exceptions through try-catch

Ip address input legality:

           Ip address input out of bounds:

                     

           Ip address input logic:

Exceptions that may occur when scanning the port number of the destination host: catch exceptions through try-catch

       The ip address of the destination host is incorrectly entered:

           Port number input out of bounds:

                 

           Port number input logic:

                 

    Multi-threading: Generate a fixed number of thread pools through the java.util.concurrent.ExecutorService package to execute multi-threaded services:

ExecutorService executor = Executors.newFixedThreadPool(threadNum)

Then use executor.execute(new PingRunner()) and threadPool.execute(scanMethod) to start multi-threaded ping scanning and port number scanning.

The input legality of the number of threads:

4. Detailed design steps

Account login module: In the user login interface, you need to enter the "account" and "password". Only when the password is correct can you jump to the operation page, otherwise the scanning operation cannot be performed.

Key algorithm:

// The user name is correct, compare the password, equals(): compare whether the strings are equal

if (username.getText().trim().equals("admin")) {// getText (): Get the entered password

boolean isCorrect = true;// trim() removes the leading and trailing spaces of the string

char[] ch1 = pwd.getPassword();//ch1 is the password entered by the user

char[] ch2 = s.toCharArray();//ch2 is the default password 

if (ch1.length == ch2.length) { // If the lengths are equal, then compare

        for (int i = 0; i < ch1.length; i++) {

            if (clearText.indexOf(ch1[i])!= cipherText.indexOf(ch2[i])) {

                 isCorrect = false; // As long as there is unequal, it is wrong

                    break;

             } else {// the length is not equal, direct error

               isCorrect = false;}}

}

flow chart:        

IP scanning module: IP address scanning is divided into single address and multi-address. The IP address and the number of threads input by the user are used as parameters to scan the IP address in a multi-threaded manner; the PingTester class is called to realize the IP address scanning. The PingTester class contains 3 Part: constructor, startPing function, internal class PingRunner.

Key algorithm:

try { //InetAddress saves the host name and IP address

while ((taskIp = getIp()) != null) { // Call the getIp() method to get the IP address

    InetAddress addr = InetAddress.getByName(taskIp);// Get IP address

   //isReachable() tests whether the address is reachable. The implementation will make a best effort attempt to reach the host,

    if (addr.isReachable(5000)) { // The access timeout is 5s

       System.out.println("IP address ["+taskIp+"] is reachable");

            OwnTCPScan.Result.append("IP address ["+taskIp+"] is reachable"+"\n");

        } else { //System.out.println ("") to display scan results on the backend

       System.out.println("IP address ["+taskIp+"] is not in the network");

            OwnTCPScan.Result.append("IP address ["+taskIp+"] is not in the network"+"\n");

        } //OwnTCPScan.Result.append("") displays the scan result on the UI interface

  }

} catch (SocketException e) { // if the access times out

    System.out.println("IP address ["+taskIp+"] has no access rights");

    OwnTCPScan.Result.append("IP address["+taskIp+"] has no access rights"+"\n");}

    flow chart:

Port scanning module: Scanning according to the host and port number adopts multi-thread technology, and performs multi-thread scanning with the host name, thread number and port range input by the user as parameters, calls the scanPorts function to pass parameters, and the scanPorts function calls the ScanMethod class to realize port scanning . It is mainly divided into three parts: the scanPorts function, the constructor of the ScanMethod class, and the run function of the ScanMethod class.

key code:

try {//Java.net package has the definition of InetAddress class, used for IP address and domain name

InetAddress address = InetAddress.getByName(ip);

        //InetAddress can obtain the ip address of the host according to the host name, and also supports inputting the IP address

 Socket socket;// Define the socket, call the connect function to view the status of the port

 SocketAddress socketAddress;//SocketAddress is an abstract class that passes ip and port. In fact, what is actually implemented is not SocketAddress, but its subclass. This declaration is for the convenience of calling

 // You must use a subclass of SocketAddress to create a SocketAddress object

 if (ports.length < 1) {

        return;// If the array has no elements, return, do not execute

 }

  //serial is the thread number, thread 1 detects ports 1, 11, and 21, thread 2 detects ports 2, 12, and 22

 for (port = 0 + serial; port <= ports.length - 1; port += threadNumber) {

socket = new Socket();

socketAddress = new InetSocketAddress(address, ports[port]);

//InetSocketAddress is an implementation subclass of SocketAddress, which implements IP socket address (IP address + port number) and does not depend on any protocol

        try {// Connect to the specified port of the target host, and the connection will fail after timeout

        socket.connect(socketAddress, timeout);

socket.close(); // Close the port

System.out.println(" port" + ports[port] + ": open");

            OwnTCPScan.Result.append(" port" + ports[port] + ": open\n");

// update the message in the text area

         } catch (IOException e) {// An exception occurs indicating that the port is closed

 System.out.println(" port" + ports[port] + ": closed");

            OwnTCPScan.Result.append(" port" + ports[port] + ": closed\n");

        }

    }

 } catch (UnknownHostException e) {

    e.printStackTrace();

 }

}

flow chart::

Guess you like

Origin blog.csdn.net/dw1360585641/article/details/125460253
Recommended