简体   繁体   English

TCP扫描:意外的套接字异常

[英]TCP Scan: Unexpected Socket Exceptions

I'm writing a simple TCP Port scanner. 我正在编写一个简单的TCP端口扫描程序。 Here's my code: 这是我的代码:

for (int port : portList) {
    Socket socket = new Socket();

    try {
        socket.connect(new InetSocketAddress(targetIP, port), 5000);

        // If we get to this point, port is open
        Log.d(DEBUG_TAG, "Port " + port + " open");

    } catch (Exception e) {
        Log.d(DEBUG_TAG, e.toString());
    } finally {
        // Try to close
        try {
            socket.close();
        } catch (Exception e) {}
    }
}

Here's the debug output. 这是调试输出。 I've run the scan several times and get the exact same output with the same port ECONNREFUSED while others EHOSTUNREACH . 我已经运行了几次扫描,并使用相同的端口ECONNREFUSED获得了完全相同的输出,而其他端口则获得了EHOSTUNREACH

D/TCPSCAN﹕ java.net.SocketException: failed to connect to /192.168.0.10 (port 21) after 5000ms: isConnected failed: EHOSTUNREACH (No route to host)
D/TCPSCAN﹕ java.net.ConnectException: failed to connect to /192.168.0.10 (port 22) after 5000ms: isConnected failed: ECONNREFUSED (Connection refused)
D/TCPSCAN﹕ java.net.SocketException: failed to connect to /192.168.0.10 (port 23) after 5000ms: isConnected failed: EHOSTUNREACH (No route to host)
D/TCPSCAN﹕ java.net.SocketException: failed to connect to /192.168.0.10 (port 80) after 5000ms: isConnected failed: EHOSTUNREACH (No route to host)
D/TCPSCAN﹕ java.net.SocketException: failed to connect to /192.168.0.10 (port 443) after 5000ms: isConnected failed: EHOSTUNREACH (No route to host)

The Android device I'm running on can successfully ping -c 1 -W 1 192.168.0.10 . 我正在运行的Android设备可以成功ping -c 1 -W 1 192.168.0.10 My phone detects the remote host as online, which is why I don't understand why I'm getting EHOSTUNREACH . 我的手机将远程主机检测为在线,这就是为什么我不明白为什么我得到EHOSTUNREACH More curious is the fact that port 22 - which is not open on the target - gets a ECONNREFUSED . 更奇怪的是,端口22(在目标上未打开)得到ECONNREFUSED的事实。

The only port that is actually open on the target is FTP port 21 (confirmed with nmap ). 目标上实际打开的唯一端口是FTP端口21(已通过nmap确认)。

I've read some answers to similar problems. 我已经阅读了类似问题的答案。 The main fix seems to be restarting the phone's WiFi connection, which didn't work for me. 主要修复措施似乎是重新启动手机的WiFi连接,这对我不起作用。

Note 注意

For the sake of clarity I'm not posting my whole code here but I am checking that the target host 192.168.0.10 is online with: 为了清楚起见,我没有在此处发布整个代码,但是我在检查目标主机192.168.0.10是否与以下设备联机:

Process process = runtime.exec("/system/bin/ping -c 1 -W 1 192.168.0.10");
// Wait until the process finishes and check exit code (0 for success)
int exitCode = process.waitFor();
if (exitCode == 0)
    Log.d(DEBUG_TAG, "Online");

The device I'm running on is a Samsung Galaxy S2, I'm targetting my laptop, a Macbook Pro running Fedora. 我正在运行的设备是三星Galaxy S2,我的目标是笔记本电脑,即运行Fedora的Macbook Pro。 Both devices are on the same WiFi network. 两个设备都在同一WiFi网络上。

From my laptop I can detect and run an nmap scan on the Android phone ( 192.168.0.3 ). 通过我的笔记本电脑,我可以在Android手机( 192.168.0.3 )上检测并运行nmap扫描。 It's definitely reachable. 绝对是可以达到的。

Could you please provide more details about your test? 您能否提供有关测试的更多详细信息?

  • What is your source device (android phone)? 您的源设备是什么(Android手机)?
  • What destination are you trying to reach and scan? 您试图到达并扫描哪个目的地? Computer on local or outside network? 本地或外部网络上的计算机?
  • Are both source and destination devices on the same network (probably WiFi)? 源设备和目标设备是否都在同一网络(可能是WiFi)上?

EHOSTUNREACH - no route to host should say it is either EHOSTUNREACH-没有通往主机的路线应该说是

  • ISO/OSI Layer 3 issue (devices on the different private IP networks without routes to each other) ISO / OSI第3层问题(不同私有IP网络上的设备之间没有路由)

  • destination device is down 目标设备已关闭

  • ICMP protocol/ECHO replies are blocked somewhere in between (or firewall on source or destination device) ICMP协议/ ECHO答复介于两者之间(或源设备或目标设备上的防火墙)之间

ECONNREFUSED - should be even more clear - this particular port is not opened on destination device ECONNREFUSED-应该更清楚-此特定端口未在目标设备上打开

Maybe you can split your code logic into two parts: 也许您可以将代码逻辑分为两部分:

  1. Check if device is up and running (PING - ICMP protocol) 检查设备是否已启动并正在运行(PING-ICMP协议)
  2. Then perform port scan 然后执行端口扫描
    • if isConnected == TRUE (port is open) else (port is closed) 如果isConnected == TRUE(端口打开),否则(端口关闭)

Output could be: 输出可能是:

  1. PING is Successful && isConnected == TRUE (device is UP and port is OPEN) PING成功&& isConnected == TRUE(设备已启动,端口已打开)
  2. PING is Successful && isConnected == FALSE (device is UP but port is CLOSED) PING成功&& isConnected ==否(设备已启动,但端口已关闭)
  3. PING is Unsuccessful && isConnected == TRUE (probably ICMP protocol/ECHO request is BLOCKED - device is UP and port is OPENED) PING不成功&& isConnected == TRUE(可能是ICMP协议/ ECHO请求被阻止-设备已启动,端口已打开)
  4. PING is Unsuccessful && isConnected == FALSE (device is probably DOWN or ICMP is BLOCKED and port is CLOSED) PING不成功&& isConnected == FALSE(设备可能已关闭或ICMP处于阻塞状态,并且端口已关闭)

Try to give it a try. 尝试一下。

Thank you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM