[英]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? 您能否提供有关测试的更多详细信息?
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: 也许您可以将代码逻辑分为两部分:
Output could be: 输出可能是:
Try to give it a try. 尝试一下。
Thank you. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.