开发者

in Java, how to achieve UDP port scanning?

开发者 https://www.devze.com 2023-03-02 02:56 出处:网络
new here, I\'m working on a program for port scanning, TCP works well, but I don know how to ahieve UDP ports scanning. Say I wanna know whether UDP port XXXX on another host in this LAN is open. will

new here, I'm working on a program for port scanning, TCP works well, but I don know how to ahieve UDP ports scanning. Say I wanna know whether UDP port XXXX on another host in this LAN is open. will this code do the work? if not, what's the problem?

protected String scanUDP(InetAddress IP, int port)
{
    try{
        byte [] bytes = new byte[128];
        DatagramSocket ds = new DatagramSocket();
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length, IP, port);
        ds开发者_如何学Go.setSoTimeout(1000);
        ds.send(dp);
        dp = new DatagramPacket(bytes, bytes.length);
        ds.receive(dp);
        ds.close();
    }
    catch(InterruptedIOException e){
        return "CLOSED";
    }
    catch(IOException e){
        return "CLOSED";
    }
    return "OPEN";
}

just a newbie, still learning. thanks!


UDP is connectionless, so you can't expect a response packet, necessarily. If the port is closed, you might get an ICMP error message, though there's no guarantee of this (e.g. a firewall could silently drop the packet).


UDP Port Scanning is possible, but it is harder than TCP scanning.

One method I have used in python is to slowly scan a host on three or four high-numbered UDP ports and check for ICMP Port Unreachable messages from that host. If you get any of those back, you know that ICMP messages are allowed on the path, so you can reliably infer that lack of a response is an open port. If all those high-numbered ports fail, you must resort to application-aware techniques to guarantee success. Be aware that this should be done slowly (maybe once every second or so) to reduce the probability of host-level ICMP rate-limiting.

0

精彩评论

暂无评论...
验证码 换一张
取 消