现在我在为工作安全项目编写一个简单的端口扫描器/密码检查器时遇到了一些重大问题。
我的基本目标是编写一个快速的小工具来打开一个文本文件,扫描端口 21、23、80、502 和 8080,然后简单地将返回的 http 状态写入文件(200、404 等)
到目前为止,我一直在尝试使用 httpclient 执行此操作,但结果非常糟糕。
我的代码是这样的
public static void doHosts() throws Exception{
String filename = "C:\\test.txt";
String ip = "";
String port[] = {"21", "23", "80", "502", "8080"};
FileInputStream fstream = new FileInputStream("c:\\scan.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ((ip = br.readLine()) != null) {
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 3000;
int timeoutSocket = 5000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpHost targetHost = new HttpHost(ip);
DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("blah", "blah");
httpclient.getCredentialsProvider().setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), creds);
NegotiateSchemeFactory nsf = new NegotiateSchemeFactory();
httpclient.getAuthSchemes().register(AuthPolicy.SPNEGO, nsf);
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
// Add AuthCache to the execution context
BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
httpclient = wrapClient(httpclient);
HttpGet get;
for (int i = 0; i < port.length; i++) {
get = new HttpGet("http://" + ip + ":" + port[i] + "/");
HttpResponse response = httpclient.execute(get);
try {
BufferedWriter out = new BufferedWriter(new FileWriter("filename", true));
out.write("ip: "+ ip + " : "+port+ " - " + response.getStatusLine().getStatusCode()); //HTTP status returned off request );
} catch (IOException e) {
}
}
}
}
到目前为止,我的问题是它打开了文本文件,遇到了一个错误的结果,没有对其进行序列化,然后就因异常而死了。
我不确定如何让它继续出现异常(我知道有些网站会说“未启动”,这就是它是端口扫描器的原因。
任何帮助都会很棒,并且可能会使一些可怜的实习生免于明天手动检查大约 6000 个 ip。