在Java中通過調(diào)用Runtime這個(gè)類可以執(zhí)行其他的可執(zhí)行程序,執(zhí)行后返回一個(gè)進(jìn)程(Process),利用Process這個(gè)類我們可以取得程序執(zhí)行的回顯,因此在Java中調(diào)用nmap進(jìn)行主機(jī)探測的原理就很清晰了。通過給函數(shù)傳遞nmap所在路徑和我們需要執(zhí)行的命令即可。
具體實(shí)現(xiàn)代碼:
/**
* 調(diào)用nmap進(jìn)行掃描
* @param nmapDir nmap路徑
* @param command 執(zhí)行命令
*
* @return 執(zhí)行回顯
* */
public String getReturnData(String nmapDir,String command){
Process process = null;
StringBuffer stringBuffer = new StringBuffer();
try {
process = Runtime.getRuntime().exec(nmapDir + " " + command);
System.out.println("請稍等。。。");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(),"UTF-8"));
String line = null;
while((line = reader.readLine()) != null){
stringBuffer.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return stringBuffer.toString();
}
測試:
NmapTest1 nmapTest1 = new NmapTest1();
String str = nmapTest1.getReturnData("D:\\nmap\\nmap.exe","-sS -P0 -A -v www.zifangsky.cn");
System.out.println(str);
返回結(jié)果:
請稍等。。。
Starting Nmap 7.00 ( https://nmap.org ) at 2015-11-30 21:00 ?D1��������?����??
NSE: Loaded 132 scripts for scanning.
NSE: Script Pre-scanning.
Initiating NSE at 21:00
Completed NSE at 21:00, 0.00s elapsed
Initiating NSE at 21:00
Completed NSE at 21:00, 0.00s elapsed
Initiating Parallel DNS resolution of 1 host. at 21:01
Completed Parallel DNS resolution of 1 host. at 21:01, 0.32s elapsed
Initiating SYN Stealth Scan at 21:01
Scanning www.zifangsky.cn (121.42.81.9) [1000 ports]
Discovered open port 21/tcp on 121.42.81.9
Completed SYN Stealth Scan at 21:01, 9.01s elapsed (1000 total ports)
Initiating Service scan at 21:01
Scanning 1 service on www.zifangsky.cn (121.42.81.9)
Completed Service scan at 21:01, 9.10s elapsed (1 service on 1 host)
Initiating OS detection (try #1) against www.zifangsky.cn (121.42.81.9)
Initiating Traceroute at 21:01
Completed Traceroute at 21:01, 9.06s elapsed
Initiating Parallel DNS resolution of 1 host. at 21:01
Completed Parallel DNS resolution of 1 host. at 21:01, 16.50s elapsed
NSE: Script scanning 121.42.81.9.
Initiating NSE at 21:01
Completed NSE at 21:02, 13.32s elapsed
Initiating NSE at 21:02
Completed NSE at 21:02, 0.00s elapsed
Nmap scan report for www.zifangsky.cn (121.42.81.9)
Host is up (0.047s latency).
Not shown: 999 filtered ports
PORT STATE SERVICE VERSION
21/tcp open ftp vsftpd (before 2.0.8) or WU-FTPD
Warning: OSScan results may be unreliable because we could not find at least 1 open and 1 closed port
Device type: specialized|WAP
Running: iPXE 1.X, Linux 2.4.X|2.6.X
OS CPE: cpe:/o:ipxe:ipxe:1.0.0%2b cpe:/o:linux:linux_kernel:2.4.20 cpe:/o:linux:linux_kernel:2.6.22
OS details: iPXE 1.0.0+, Tomato 1.28 (Linux 2.4.20), Tomato firmware (Linux 2.6.22)
Service Info: Host: www.net.cn
TRACEROUTE (using port 21/tcp)
HOP RTT ADDRESS
1 3.00 ms 192.168.0.1
2 … 30
NSE: Script Post-scanning.
Initiating NSE at 21:02
Completed NSE at 21:02, 0.00s elapsed
Initiating NSE at 21:02
Completed NSE at 21:02, 0.00s elapsed
Read data files from: D:\nmap
OS and Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 73.93 seconds
Raw packets sent: 2158 (97.246KB) | Rcvd: 33 (2.050KB)
感謝zifangsky的個(gè)人博客提供內(nèi)容。