先上代码
package com.raikou.webScan; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UdpPort { public UdpPort() { super(); // TODO Auto-generated constructor stub } public void TestUdpPort() { String ipAddress = "10.243.132.12"; int port = 4 ; DatagramSocket connection = null; byte[] myByte = ipAddress.getBytes(); String myStr = new String(myByte); try { //connection = new DatagramSocket(port, InetAddress.getByName(ipAddress));//(ipAddress, port); connection = new DatagramSocket();//(ipAddress, port); //connection.setReceiveTimeout(20*1000); connection.setSoTimeout(120*1000); connection.connect( InetAddress.getByName(ipAddress), port); System.out.println("连结创建完成..."); connection.send((new DatagramPacket(myByte, myByte.length))); System.out.println(" 数据发送完成..."); while(true) { byte[] newByte = new byte[4096]; DatagramPacket dp = new DatagramPacket(newByte, 4096); connection.setSoTimeout(9000); connection.receive(dp); System.out.println("11"); if (dp != null && dp.getData() != null) { System.out.println(dp.getLength()); System.out.println("#####"); byte[] rslt = dp.getData(); for (int i = 0;i < dp.getLength(); i++) { System.out.println(rslt[i]); //System.out.println(); } System.out.println("#####"); } //String myDump = buffer.getHexDump(); break; } connection.close(); } catch(Exception ex1) { ex1.printStackTrace(); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub UdpPort up = new UdpPort(); up.TestUdpPort(); } }
今天在机房运行了一下程序,意外发现在机房的xp系统下,程序是可以得出正确结果的,和nmap扫描出的结果一样。
但在win7上,扫描其他主机的UDP端口都会出现超时的情况,关闭杀软和防火墙都没用。
之后又在强大的google上搜到了另一个代码:
package com.raikou.test; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; /** * UDP/IP阻塞模式 * */ public class UDPBIO { /** * 服务器端和客户端类似 * @param ipStr IP地址 * @param portNum 端口号 * */ public void udpListen(String ipStr, int portNum) { try { //若希望是全双工模式,则启动一个监听端口,承担服务器的职责 //若不能绑定到指定端口,则抛出SocketException DatagramSocket serverSocket = new DatagramSocket(portNum); InetAddress server = InetAddress.getByName(ipStr); byte[] buffer = new byte[65507]; DatagramPacket receivePacket = new DatagramPacket(buffer,buffer.length); DatagramSocket socket = new DatagramSocket(); DatagramPacket packet = new DatagramPacket(buffer,buffer.length,server,portNum); //阻塞发送packet到指定的服务器和端口 //网络IO异常,抛出IOExceprion //连不上IP和端口,抛出PortUnreachableException socket.send(packet); serverSocket.setSoTimeout(9000); //阻塞并同步读取流信息,如接收到的信息流比packet长,则删除更长的信息 serverSocket.receive(receivePacket); /* byte[] rslt = receivePacket.getData(); for (int i = 0;i < receivePacket.getLength(); i++) { System.out.println(rslt[i]); //System.out.println(); } System.out.println("#####");*/ } catch (SocketException e) { // 若不能绑定到指定端口 e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch(Exception ex1) { ex1.printStackTrace(); } } public static void main(String[] args) { // TODO Auto-generated method stub UDPBIO p = new UDPBIO(); p.udpListen("10.243.132.73", 137); } }
结果只成功了一次,之后被卡巴识别成网络攻击,然后就杯具了
一直就不成功了。
剩下的明天继续。