您好,登錄后才能下訂單哦!
使用Java怎么實現一個端口掃描器?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; import java.net.UnknownHostException; import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class ScanPort extends JFrame { /** * 端口掃描啟動主程序 */ private static final long serialVersionUID = 1L; String str1 = " 常見端口有:"; String str2 = "ftp:21,22,telnet:23,smtp:25,http:80"; String str3 = "dns:53,tftp:69,snmp:161,162"; String str4 = "1158,1433,1521,2100,3128,26,69"; String str5 = "3306,3389,7001,8080,8081,110,143"; String str6 = "9080,9090,43958,443,465,995,1080"; JButton jb1 = new JButton("strat"); JTextArea jta = new JTextArea(); JScrollPane jsp = new JScrollPane(jta); JTextField jtf = new JTextField(17); String IP = "";//待掃描IP或域名 List <Integer>portList = new LinkedList<Integer>();// 定義一個List容器表示掃描的團口的List集合 Integer[] ports = new Integer[] { 21, 22, 23, 25, 26, 53,69, 80, 110, 143, 443,465,69,161,162,135,995,1080,1158,1433,1521,2100, 3128, 3306, 3389, 7001, 8080, 8081, 9080, 9090, 43958 ,135,445,1025,1026,1027,1028,1055,5357}; // 常見端口集合 public ScanPort() { this.add(getPanel(), BorderLayout.SOUTH); jsp.setBorder(BorderFactory.createEtchedBorder()); jsp.setBackground(Color.cyan); this.add(jsp, BorderLayout.CENTER); this.add(getPanel2(), BorderLayout.NORTH); this.add(getPanel3(), BorderLayout.WEST); this.setBounds(300, 60, 600, 600); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("ScanPort"); jta.setTabSize(4); jta.setFont(new Font("標楷體", Font.BOLD, 16)); jta.setLineWrap(true);// 激活自動換行功能 jta.setWrapStyleWord(true);// 激活斷行不斷字功能 portList.addAll(Arrays.asList(ports)); //將ports中的值加入到set中,并去掉重復的 jb1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub IP = jta.getText(); //ip為文本框內輸入的字符串 scanPorts(IP, portList, 10, 800); //超時時間設定為800,線程數設定為10 } }); this.setVisible(true); } /** * 多線程掃描目標主機指定List端口集合的開放情況 * * @param ip * 待掃描IP或域名 * @param portList * 待掃描的端口的List集合 * @param threadNumber * 線程數 * @param timeout * 連接超時時間 * */ public void scanPorts(String ip, List<Integer> portSet,int threadNumber, int timeout) { ExecutorService threadPool = Executors.newCachedThreadPool(); //線程池 for (int i = 0; i < threadNumber; i++) { //10個線程 加入到線程池里 ScanMethod scanMethod2 = new ScanMethod(ip, portSet,threadNumber, i, timeout); threadPool.execute(scanMethod2); } threadPool.shutdown(); while (true) { if (threadPool.isTerminated()) { System.out.println("掃描結束"); break; } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }// end of while } /* * 掃描方式:針對一個待掃描的端口的List集合進行掃描 */ private class ScanMethod implements Runnable { private String ip; // 目標IP private List<Integer> portList; // 待掃描的端口的List集合 private int threadNumber, serial, timeout; // 線程數,這是第幾個線程,超時時間 public ScanMethod(String ip, List<Integer> portList, int threadNumber,int serial, int timeout) { this.ip = ip; this.portList = portList; this.threadNumber = threadNumber; this.serial = serial; this.timeout = timeout; } public void run() { int port = 0; Integer[] ports = portList.toArray(new Integer[portList.size()]); // List轉數組 try { InetAddress address = InetAddress.getByName(ip); //如果輸入的是主機名,嘗試獲取ip地址 Socket socket;//定義套接字 SocketAddress socketAddress;//傳遞ip和端口 if (ports.length < 1) //若數組沒有元素,返回,不執行 return; for (port = 0 + serial; port <= ports.length - 1; port += threadNumber) { //每次運行10個線程 socket = new Socket(); //為對象分配內存空間 socketAddress = new InetSocketAddress(address, ports[port]); try { socket.connect(socketAddress, timeout); //對目標主機的指定端口進行連接,超時后連接失敗 socket.close(); //關閉端口 System.out.println("端口 " + ports[port] + " :開放"); jta.append("端口 " + ports[port] + " :開放\n"); //在文本區域里更新消息 } catch (IOException e) { System.out.println("端口 " + ports[port] + " :關閉"); jta.append("端口 " + ports[port] + " :關閉\n"); //產生異常表示端口關閉 } } } catch (UnknownHostException e) { e.printStackTrace(); } } //end of run() }//end of ScanMethod public JPanel getPanel() { JPanel jp = new JPanel(); jp.add(jb1, BorderLayout.CENTER); jp.setBorder(BorderFactory.createRaisedBevelBorder()); jp.setBackground(Color.lightGray); return jp; } public JPanel getPanel2() { JPanel jp = new JPanel(); JLabel jl = new JLabel(); jl.setText("請輸入主機名或ip地址,將掃描該主機的常見端口號:"); jp.add(jl); jp.add(jtf); jp.setBorder(BorderFactory.createRaisedBevelBorder()); jp.setBackground(Color.LIGHT_GRAY); return jp; } public JPanel getPanel3() { JLabel jl1 = new JLabel(str1); JLabel jl2 = new JLabel(str2); JLabel jl3 = new JLabel(str3); JLabel jl4 = new JLabel(str4); JLabel jl5 = new JLabel(str5); JLabel jl6 = new JLabel(str6); JPanel jp = new JPanel(); jp.setLayout(new GridLayout(6, 1)); jp.add(jl1); jp.add(jl2); jp.add(jl3); jp.add(jl4); jp.add(jl5); jp.add(jl6); jp.setBorder(BorderFactory.createEtchedBorder()); //蝕刻邊框 return jp; } public static void main(String[] args) { new ScanPort(); } }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。