Java Write black Soft-port scanner article __java

Source: Internet
Author: User

Java write black Soft-port scanner article
Tsunami days (ansty)
Last we wrote a "file last modified Time editor" of the small black soft, now we use Java to write our usual use of the port scanner. This time to facilitate and avoid GUI programming trouble, we directly into the command line below the tool, with parameters to start it. Let's call it the "Java version of the simple port scanning Tool", because this article is only to provide Java writing black soft ideas, many algorithmic optimization and feature attachment is not discussed in this article, the use is also a single thread.
Program Interface:



Principle:
Use the Java.net.Socket class to establish a socket connection and throw IOException If a connection cannot be established with the specified IP and port. We use Try-catch to capture this IOException exception to determine whether the connection to the specified IP port was successful. If a connection is successfully established, the specified port for the specified IP is open, and if the program throws a IOException exception that is captured by us, the specified IP is not open to the specified port. Scanning the specified port segment uses loops to continuously connect to the server's specified port for us to determine whether it is open.

I have always believed that all the problems in the world as long as there is a clear algorithm, we will be able to use the program language to achieve it, no matter what language. Now, we have the principle is equal to the algorithm, you say we have nothing but technology. There is only a shortage of hands.

Because we want to get the server address, the starting port, and the termination port information from the parameters of the program startup, we need to use the following code:
IP = args[0];//Get the server address we specified
Startport = Integer.parseint (Args[1]); Gets the starting port number, because args[] is a string type, so you want to cast to the int type.
Endport = Integer.parseint (args[2]);//Get the end port number, Ibid.
Be sure to determine the legality of the port before you get the port and establish the socket because the range of the ports is in 1~65535, It is not necessary, and not feasible, to establish a connection to the outer ports of the range. Since it is the starting and terminating ports, it is necessary to have a size order problem, that is, to determine their size. Copy Code

if (startport<1| | startport>65535| | endport<1| | endport>65535) {    //Check whether the port is within the legal range 1~65535
System.out.printf ("Port range must be within 1~65535!");
Return
}else if (startport>endport) { //compare starting and terminating ports
SYSTEM.OUT.PRINTLN ("Port input error!/n start port must be less than terminating port");
Return
}

The


establishes a connection to the specified port on the server, where the Java.net.Socket class is used, and first we look at how it is constructed:
Socket () create a disconnected socket via the system default type SocketImpl
The socket (inetaddress address, int port) creates a stream socket and connects it to the specified port number for the specified IP addresses. The
socket (inetaddress address, int port, inetaddress localaddr, int localport) creates a socket and connects it to the specified remote location on the specified remote port. The
socket proxy creates an disconnected socket based on the specified proxy type (if any) that should be used regardless of the other settings. The
socket (SocketImpl impl) creates an disconnected socket with the user-specified SocketImpl. The
Socket (String host, int port) creates a stream socket and connects it to the specified port number on the specified host. The
socket (String host, int port, inetaddress localaddr, int localport) creates a socket and connects it to the specified remote port on the specified remote host.
As you can see, we have a lot of constructs in it, and now we just have to care about the second constructor socket (inetaddress address, int port) because we don't need to interact with the service that the server is running on the port, so we just have to establish the connection, Then close the connection. That is,
Socket s = new socket (address,port);
Before establishing a connection, we first convert the IP to the inetaddress type, not that the string type is not available, so as to reduce the likelihood of more exceptions. The
Static inetaddress Getbyname (String host) determines the IP address of the host, given the host name. This is a static method, direct Inetaddress.getbyname () on the line.
Copy code,

try{
InetAddress address = inetaddress.getbyname (IP); Conversion type
}catch (Unknownhostexception e) {
SYSTEM.OUT.PRINTLN ("Cannot find" + IP);
Return
}


Here's our core algorithm.
Loops the specified port segment for all ports and establishes a connection to all ports. After the connection is successful, we complete the current loop task and then call the Close () method to turn off the connection. Because when the socket s = new socket (Address,nport) executes, if the connection is successfully established, it is not executed into the catch and can be executed to the following Result.add ("+nport") statement ; If you can't connect up, you throw an exception and we catch it, the program runs into the catch, executes the statement in the catch, and then it goes on to the next loop.
Copy code

for (int nport=startport;nport<=endport;nport++) {    //loop from start port to terminating port
try{
System.out.print ("scanning" +nport); Print Scan Progress
Socket s=new socket (address,nport); Establish a connection
S.close (); Close connection
Result.add ("" +nport); Add an open port to ArrayList result
System.out.println (": Open"); Print status
}catch (IOException e) {
System.out.println (": Close"); Print status
}
}


Print the result, because we use the ArrayList to store the scan result, Java inside does not have the C language sense the pointer, therefore we are accessing ArrayList inside the element to use Listiterator.
Listiterator li = Result.listiterator (); Get the ArrayList listiterator
while (Li.hasnext ()) {//If there are elements inside Li
System.out.println (Li.next (). toString () + "/topen"); Prints out the pointed element and points to the next element
}
OK, now we have introduced the main function of the program code, I believe that readers can write their own Java version of the Black soft. As I said earlier, this article only provides a way of thinking. If friends are interested, you can implement multithreading on the basis of this article, expand some useful functions, GUI interface made, or make imitation Superscan is more powerful. I in the Black Defense Forum ID is ansty, everybody to this article has the question may to the forum m i

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.