Developing multithreaded port scanning tools with Java

Source: Internet
Author: User
Tags set set

Preface: Here is only the command-line version of the scanning tool, the subsequent may be to write a separate interface, or integration into other tools up. 650) this.width=650; "Src=" /images/73.gif "border=" 0 "style=" border:0px;vertical-align:middle;height:auto; "/>

A scanning principle

In fact, the principle is very simple, is to use the socket to connect to the target IP or domain name of the designated port, if you can connect on the port is open. Conversely, if there are no connections before the connection times out, the port is judged to be off. Here I will explain the two basic scanning methods:(1) scan a continuous port segment, (2) scan only one specified port collection

Two use multithreading to scan

/** *  Multi-threaded scan destination host port open for one segment  *  *  @param  ip *              IP or domain name to be scanned, eg: * @ param startport *             Start Port  *   @param  endPort *             End Port  *  @param  threadNumber *              Threads  *  @param  timeout *              connection time-out  * */public void scanlargeports (String ip, int startport,  int endport,int threadnumber, int timeout)  {executorservice threadpool  = executors.newcachedthreadpool ();for  (int i = 0; i <  threadnumber; i++)  {Scanmethod1 scanmethod1 = new scanmethod1 (Ip, startport, endport,threadnumber,  i, timeout); Threadpool.execute (SCANMETHOD1);} Threadpool.shutdown ();//  to see if the scan has ended while  (true)  {if  (threadpool.isterminated ())   {System.out.println ("scan End"); Try {thread.sleep (1000);}  catch  (interruptedexception e)  {e.printstacktrace ();}}

then an inner class scanmethod1 implements the Runnable interface:

/** *  scan mode One: Scan for the starting end port individually  *  * */class ScanMethod1 implements  runnable {private string ip; //  Target Ipprivate int startport, endport,  threadNumber, serial, timeout; //  Start and end ports, number of threads, this is the first few threads, time-out/** *  initialize  *   *  @param  ip *              IP or domain names to be scanned  *  @param  startPort *              start Port  *  @param  endPort *              End Ports  *  @param  threadNumber *              Threads  *  @param  serial *      The         tag is the first few threads  *  @param  timeout *              connection time-out  * */public scanmethod1 (string ip, int startport ,  int endport,int threadnumber, int serial, int timeout)  {this.ip  = ip;this.startport = startport;this.endport = endport;this.threadnumber =  threadnumber;this.serial = serial;this.timeout = timeout;} Public void run ()  {int port = 0;try {InetAddress address =  Inetaddress.getbyname (IP); socket socket; socketaddress socketaddress;for  (port = startport + serial; port <=  endport; port += threadnumber)  {socket = new socket (); socketAddress  = new inetsocketaddress (Address, port); Try {socket.connect (socketAddress,  Timeout); //  timeout time socket.close (); SYSTEM.OUT.PRINTLN ("Ports  "  + port +  "&NBSP;: Open");} catch  (ioexception e)  {// system.out.println ("Port  "  + port +   "&NBSP;: Close");}}}  catch  (unknownhostexception e)  {e.printstacktrace ();}}

III using multi-threaded scanning target host

/** *  multi-threaded scan target host Specifies the opening of set port set  *  *  @param  ip *              IP or domain name to be scanned, eg: * @ param portset *             set set of ports to be scanned  *  @param  threadNumber *              Threads  *  @param  timeout *              connection time-out  * */public void scanlargeports (string ip, set<integer>  portset,int threadnumber, int timeout)  {ExecutorService threadPool =  Executors.newcachedthreadpool ();for  (int i = 0; i < threadnumber; i + +)  {scanmethod2 scanmethod2 = new scanmethod2 (Ip, portset,threadnumber, i,  timeout); Threadpool.execute (SCANMETHOD2);} Threadpool.shutdown ();while  (True)  {if  (threadpool.isterminated ())  {system.out.println (" Scan End "); Try {thread.sleep (1000);}  catch  (interruptedexception e)  {e.printstacktrace ();}}

The specific thread inner class is similar to the above, with the following code:

/** *  Scan mode Two: Scan for a set set of ports to be scanned &NBSP;*&NBSP;&NBSP;*&NBSP;*/PRIVATE&NBSP;CLASS&NBSP;SCANMETHOD2  implements Runnable {private String ip; //  Target Ipprivate set<integer > portSet; //  set set of ports to be scanned private int threadnumber, serial, timeout;  //  threads, this is the number of threads, time-out public scanmethod2 (String ip, set<integer> portset,  int threadnumber,int serial, int timeout)  {this.ip = ip;this.portset  = portset;this.threadnumber = threadnumber;this.serial = serial;this.timeout  = timeout;} Public void run ()  {int port = 0;integer[] ports = portset.toarray (New integer[portset.size ()]);  // set to array try {inetaddress address =  Inetaddress.getbyname (IP); socket socket; socketaddress socketaddress;if  (ports.length < 1) return;for  (port = 0 + serial; port <= ports.length - 1;  Port += threadnumber)  {socket = new socket ();socketaddress = new  Inetsocketaddress (Address, ports[port]); Try {socket.connect (socketaddress, timeout); Socket.close (); System.out.println ("Port  "  + ports[port] +  "&NBSP;: Open");}  catch  (ioexception e)  {// system.out.println ("Port  "  + ports[port)  +  "&NBSP;: Close");}}}  catch  (unknownhostexception e)  {e.printstacktrace ();}}

four   test case for two scanning methods

public static void main (String[] args)  { Portscandemo portscandemo = new portscandemo ();//Mode 1// portscandemo.scanlargeports (" ",  20, 10000, 5,800);// portscandemo.scanlargeports (" ",  1, &NBSP;100,&NBSP;5);//mode 2set<integer> portset = new linkedhashset<integer> (); I nteger[] ports = new integer[] { 21, 22, 23, 25, 26, 69 ,  80, 110, 143,443, 465, 995, 1080, 1158, 1433, 1521, 2100,  3128, 3306, 3389,7001, 8080, 8081, 9080, 9090,43958};p Ortset.addall ( Arrays.aslist (ports));p Ortscandemo.scanlargeports ("",  portset, 5, 800);} 

five   test result

650) this.width=650; "src=" Http:// "style=" float: none; "title=" 20151220233317_98732.png "alt=" Wkiol1z4oyxb_3cyaafdqjgrsfq285.png "/>

650) this.width=650; "src=" Http:// "style=" float: none; "title=" 20151220233342_95589.png "alt=" Wkiol1z4oywxtyktaabnvxk7sl4968.png "/>

Note: 1 time-out is in milliseconds, where if you scan the domestic IP can be set to a lower time appropriate, 200~500 around. On the other hand, if you scan a foreign IP, you need to set the time to a larger size, or you may have left open the port also missed

2 Complete Test File download Link: Http://

(PS: Welcome everyone to visit my personal blog site:

This article is from "Zifangsky's personal blog" blog, make sure to keep this source

Developing multithreaded port scanning tools with Java

Related Article

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: 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.