IP范围和端口扫描器 [英] ip range and port scanner

查看:291
本文介绍了IP范围和端口扫描器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我检查与使用命令行的IP地址的特定范围的端口扫描器。我怎样才能确保该端口与IP的范围进行扫描。

ARGS [0]和args [1] IP地址范围,而ARGS [2]和args [3]端口扫描程序。

例如我运行的Java测试1.1.1.1 5 0 10
该程序将检查从1.1.1.1 IP对1.1.1.5在端口0 - 10

下面我code:

 进口java.util.concurrent中*。
进口的java.util。*;
进口java.net *。//源http://stackoverflow.com/questions/11547082/fastest-way-to-scan-ports-with-java公共类测试
{
公共静态无效的主要(字串[] args)抛出异常
{
    最后ExecutorService的ES = Executors.newFixedThreadPool(20);
    INT bRange = 0; //声明字符串名为bRange以0初始值。
    INT ERANGE = 0; //声明字符串名为ERANGE以0初始值。
    最终诠释超时= 200;    //字节[] = IPADDR InetAddress.getByName(参数[0])的getAddress()。
    //字节[] = IP地址1 InetAddress.getByName(参数[1])的getAddress()。
    串IPADDR = ARGS [0];
    串IP地址1 = ARGS [1];
    如果(args.length!= 3)
    {
        尝试
        {
            bRange =的Integer.parseInt(参数[2]);第二个参数bRange的//指定值。
            ERANGE =的Integer.parseInt(参数[3]);第三个参数为ERANGE的//指定值。
        }
        赶上(NumberFormatException的E)//如果用户输入无效数据。
        {
            的System.out.println(你没有输入一个有效的数字为论据2和3。请有效整数再试一次。);
            System.exit(0);
        }        布尔F确认= TRUE; // DEBUG只能删除
        最终名单<&未来LT;布尔>>期货=新的ArrayList<>();        ArrayList的<整数GT;其他协议的=新的ArrayList<整数GT;((ERANGE - bRange)); //声明整数值称为其他协议的一个ArrayList和设置其初始大小。        // for循环随机扫描。
        对于(INT端口= bRange;港口< = ERANGE;港口++)
        {
            portRange.add(端口);            其他协议的的//使用ArrayList和洗牌要经过每个端口号一次。
            Collections.shuffle(其他协议的); //洗牌其他协议的。            INT大小= portRange.size(); //声明整数称为大小,并指定其他协议的ArrayList的大小值的价值。
            INT randPort = portRange.get(大小-1); //指定其他协议的ArrayList中的最后一个索引整型变量randPort。            的System.out.println(randPort); //显示所有端口            // INT randTimeout = randInt(30,2000); //运行方法randInt分配随机值randTimeout()。 PartD            futures.add(portIsOpen(ES,IP地址,randPort,超时));            portRange.remove(尺寸 - 1); //在其他协议的ArrayList中删除最后一个元素。
        }        es.shutdown(); //告诉执行者服务,它不能接受新的任务,但已经提交的任务继续运行。
        INT openPorts = 0; //声明整数称为openPorts并指定值为0。
        对于(最终未来<布尔> F:期货)
        {
            如果(f.get())
            {
                openPorts ++;
            }
        }
        的System.out.println(有+ openPorts +主机开放端口​​+ IPADDR +到+ IP地址1 + + +超时MS同一个超时探测);
        //打印报表显示,许多端口开放如何根据特定的IP地址和超时。
    }
    其他
    {
        的System.out.println(参数个数错误);
        的System.out.println(请重试);
    }
}公共静态未来<布尔> portIsOpen(最终的ExecutorService ES,最后弦乐IPADDR,最终诠释港口,最终诠释超时)
{
    返回es.submit(新赎回<布尔>()
    {
        @覆盖公共布尔调用()
        {
            尝试//尝试块,每个For循环递增Try块被调用的时间。
            {
                Socket套接字=新的Socket(); // try块创建Socket类的一个实例。
                socket.connect(新的InetSocketAddress(IPADDR,口),超时); //创建一个流套接字并将其指定IP地址连接到指定的端口号。
                socket.close(); //关闭套接字连接。
                的System.out.println(开放端口发现+端口); //结果表明许多港口如何开放。
                返回true;
            }
            赶上(异常前)
            {
                返回false;
            }
        }
    });
}
}


解决方案

我想,这是更好的使用通常的 CIDR notaion确定的IP地址范围。在这种情况下,你需要使用精心一个额外的参数,如 1.1.1.1/29 ,这意味着IP-地址范围从1.1.1.1 1.1.1.6到和1.1.1.7是广播地址。您可以使用Apache共享网络类<一href=\"https://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/util/SubnetUtils.html\"相对=nofollow> SubnetUtils ,以确定的范围内,并得到所有包括地址:

 公共静态无效的主要(字串[] args){
    字符串子网=1.1.1.1/29;
    SubnetUtils utils的=新SubnetUtils(子网);
    的String [] =地址utils.getInfo()getAllAddresses()。
    对于(字符串IP:地址){
      的System.out.println(IP);
    }
}

但作为现在看来,你的例子仅检查指定范围的第一个地址,因为你不提供任何逻辑来遍历地址范围你给。

所以,如果您仍然希望前的方式来设置的范围,你需要你的范围符号转换成具体的地址列表,然后通过端口添加一个额外的循环在这个名单,包括它这个循环的循环。

要获得此列表,您可以在 getIpList 方法使用类似的逻辑,只需要提供exceptoin处理等。

 公共静态列表&LT;串GT; getIpList(字符串startIp,诠释号){
    清单&LT;串GT;结果=新的ArrayList&LT;&GT;();
    字符串currentIp = startIp;
    的for(int i = 0; I&LT;数;我++){
        字符串nextIp = nextIpAddress(currentIp);
        result.add(nextIp);
        currentIp = nextIp;
    }
    返回结果;
}公共静态最后弦乐nextIpAddress(最终字符串输入){
    最终的String []标记= input.split(\\\\);
    如果(tokens.length!= 4)
        抛出新抛出:IllegalArgumentException();
    的for(int i = tokens.length - 1; I&GT; = 0;我 - ){
        最终诠释项目=的Integer.parseInt(令牌[I]);
        如果(项&lt; 255){
            令牌由[i] =将String.valueOf(项目+ 1);
            为(中间体J = + 1; J&下; 4; J ++){
                令牌[J] =0;
            }
            打破;
        }
    }
    返回新的StringBuilder()
            .append(令牌[0])。追加('。')
            .append(令牌[1])。追加('。')
            .append(令牌[2])。追加('。')
            .append(令牌[3])
            的ToString();
}

然后就可以的范围内获取IP列表,添加一个循环:

 列表&LT;串GT; ipRange = getIpList(IPADDR,IP地址1);
对于(字符串IPADDR:ipRange){
  对于(INT端口= bRange;港口&LT; = ERANGE;港口++){
    portRange.add(端口);    其他协议的的//使用ArrayList和洗牌要经过每个端口号一次。
    Collections.shuffle(其他协议的); //洗牌其他协议的。    INT大小= portRange.size(); //声明整数称为大小,并指定其他协议的ArrayList的大小值的价值。
    INT randPort = portRange.get(大小 - 1); //指定其他协议的ArrayList中的最后一个索引整型变量randPort。    的System.out.println(randPort); //显示所有端口    // INT randTimeout = randInt(30,2000); //运行方法randInt分配随机值randTimeout()。 PartD    futures.add(portIsOpen(ES,IP地址,randPort,超时));    portRange.remove(尺寸 - 1); //在其他协议的ArrayList中删除最后一个元素。
  }
}

Can someone help me to checking a port scanner with specific range of IP addresses using command line. How can i make sure that the port scan with that IP's range.

args[0] and args[1] for IP Address range, while args[2] and args[3] for a port scanner.

For example I run "java Test 1.1.1.1 5 0 10" the program will check IP's from 1.1.1.1 to 1.1.1.5 on ports 0 - 10

Here my code:

import java.util.concurrent.*;
import java.util.*;
import java.net.*;

// source http://stackoverflow.com/questions/11547082/fastest-way-to-scan-ports-with-java

public class Test 
{   
public static void main(String [] args) throws Exception
{
    final ExecutorService es = Executors.newFixedThreadPool(20);
    int bRange = 0; //Declare String called bRange with initial value of 0.
    int eRange = 0; //Declare String called eRange with initial value of 0.
    final int timeout = 200;

    //byte[] ipAddr = InetAddress.getByName(args[0]).getAddress();
    //byte[] ipAddr1 = InetAddress.getByName(args[1]).getAddress();
    String ipAddr = args[0];
    String ipAddr1 = args[1];


    if (args.length != 3)
    {   
        try
        {               
            bRange = Integer.parseInt(args[2]); //Assign value of second argument to bRange.
            eRange = Integer.parseInt(args[3]); //Assign value of third argument to eRange.
        }
        catch(NumberFormatException E) //If user enter invalid data.
        {
            System.out.println("You did not enter a valid number for arguments 2 and 3. Please try again with valid Integers.");
            System.exit(0);
        }

        boolean fcheck = true; //DEBUG ONLY CAN DELETE
        final List<Future<Boolean>> futures = new ArrayList<>(); 

        ArrayList<Integer> portRange = new ArrayList<Integer>((eRange - bRange)); //Declare an ArrayList of Integer values called portRange and set its initial size.

        //For loop to randomize scans.
        for(int port = bRange; port <= eRange; port++) 
        {
            portRange.add(port);

            //Use ArrayList of portRange and shuffle to go through each port number once.
            Collections.shuffle(portRange); //Shuffle portRange.

            int size = portRange.size(); //Declare Integer called size and assign value of portRange ArrayList size value.
            int randPort = portRange.get(size-1); //Assign the last index of portRange ArrayList to Integer variable randPort.

            System.out.println(randPort); //Show all the ports

            //int randTimeout = randInt(30, 2000); //Assign random value to randTimeout by running method randInt(). PartD

            futures.add(portIsOpen(es, ipAddr, randPort, timeout));

            portRange.remove(size - 1); //Remove last element in portRange ArrayList.
        }

        es.shutdown(); //Tell the executor service that it can't accept new tasks, but the already submitted tasks continue to run.
        int openPorts = 0; //Declare Integer called openPorts and assign value of 0.


        for(final Future<Boolean> f : futures)
        {
            if(f.get()) 
            {
                openPorts++;
            }
        }
        System.out.println("There are " + openPorts + " open ports on host " + ipAddr + " to " + ipAddr1 + " probed with a timeout of " + timeout + "ms");
        //Print statement show how many ports open based on the particular IP Address and the timeout.
    }       
    else
    {
        System.out.println("Wrong number of arguments");
        System.out.println("Please try again");
    }   
}

public static Future<Boolean> portIsOpen(final ExecutorService es, final String ipAddr, final int port, final int timeout)
{
    return es.submit(new Callable<Boolean>() 
    {
        @Override public Boolean call()
        {
            try //Try block, each time the For loop increments the Try block gets invoked.
            {
                Socket socket = new Socket(); //The Try block creates an instance of the Socket Class.
                socket.connect(new InetSocketAddress(ipAddr, port), timeout); //Create a stream socket and connects it to specified port number at the specified IP Address.
                socket.close(); //Close the socket connection.
                System.out.println("open port found " + port); //Result show how many ports are open.
                return true;
            }
            catch (Exception ex) 
            {
                return false;
            }
        }
    });
}
}

解决方案

I suppose, it's much better to use a usual CIDR notaion to determine ip-addresses range. In that case you'll need to use inly one additional parameter, like 1.1.1.1/29, which means the ip-addresses range from 1.1.1.1 to 1.1.1.6 and 1.1.1.7 is broadcasting address. You can use Apache Commons Net class SubnetUtils to determine the range and get all included addresses:

public static void main(String[] args) {    
    String subnet = "1.1.1.1/29";
    SubnetUtils utils = new SubnetUtils(subnet);
    String[] addresses = utils.getInfo().getAllAddresses();
    for (String ip : addresses) {
      System.out.println(ip);
    }
}

But as now, it seems, that your example is checking only the first one address of the specified range, because you do not provide any logic to iterate the addresses range you give.

So, if you still wish to set the range the way before, you need to convert your range notation to the list of concrete addresses and then add an additional loop over this list and include it this loop the loop over ports.

To get this list you can use the logic like in getIpList method, just need to provide exceptoin handling etc.:

public static List<String> getIpList(String startIp, int number) {
    List<String> result = new ArrayList<>();
    String currentIp = startIp;
    for (int i = 0; i < number; i++) {
        String nextIp = nextIpAddress(currentIp);
        result.add(nextIp);
        currentIp = nextIp;
    }
    return result;
}

public static final String nextIpAddress(final String input) {
    final String[] tokens = input.split("\\.");
    if (tokens.length != 4)
        throw new IllegalArgumentException();
    for (int i = tokens.length - 1; i >= 0; i--) {
        final int item = Integer.parseInt(tokens[i]);
        if (item < 255) {
            tokens[i] = String.valueOf(item + 1);
            for (int j = i + 1; j < 4; j++) {
                tokens[j] = "0";
            }
            break;
        }
    }
    return new StringBuilder()
            .append(tokens[0]).append('.')
            .append(tokens[1]).append('.')
            .append(tokens[2]).append('.')
            .append(tokens[3])
            .toString();
}

And then you can get the list of IP within the range, add a loop:

List<String> ipRange = getIpList(ipAddr, ipAddr1);
for(String ipaddr : ipRange) {
  for (int port = bRange; port <= eRange; port++) {
    portRange.add(port);

    //Use ArrayList of portRange and shuffle to go through each port number once.
    Collections.shuffle(portRange); //Shuffle portRange.

    int size = portRange.size(); //Declare Integer called size and assign value of portRange ArrayList size value.
    int randPort = portRange.get(size - 1); //Assign the last index of portRange ArrayList to Integer variable randPort.

    System.out.println(randPort); //Show all the ports

    //int randTimeout = randInt(30, 2000); //Assign random value to randTimeout by running method randInt(). PartD

    futures.add(portIsOpen(es, ipaddr, randPort, timeout));

    portRange.remove(size - 1); //Remove last element in portRange ArrayList.
  }
}

这篇关于IP范围和端口扫描器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆