1 import java.net.*; 2 import java.io.*; 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 7 public class TcpThread extends Thread{ 8 9 //定义变量 10 public static InetAddress hostAddress;//主机IP地址 11 public static int MIN_port; //The minimal number of port 12 public static int MAX_port; //The maximal number of port 13 private int NUM_thread; //线程总数 14 public static int type; //查询方式,0为IP方式1为主机名查询方式 15 16 public static int ip1;//IP前三位 17 public static int ip2;//IP4~6位 18 public static int ip3;//IP7~9位 19 public static int ipstart;//起始IP地址最后四位 20 public static int ipend;//结束IP地址最后四位 21 public static String ipAll;//完整的IP地址 22 23 String nameHost = "";//扫描的主机名称或IP地址 24 String typeport = "0";//端口类别 25 26 /** 27 * 构造函数 28 */ 29 public TcpThread( String name, int numthread ){ 30 super(name); 31 this.NUM_thread = numthread; 32 } 33 34 /** 35 * run()运行函数 36 */ 37 public void run( ) { 38 39 int h = 0;//IP address 40 int i = 0;//port number 41 42 Socket theTcpSocket; 43 44 //根据IP地址进行扫描 45 if( 0 == type ){ 46 //IP地址循环扫描 47 for( h = ipstart; h <= ipend; h++){ 48 ipAll = "" + ip1 + "." + ip2 + "." + ip3 + "." + h; 49 nameHost = ipAll; 50 51 try{ 52 hostAddress = InetAddress.getByName( ipAll ); 53 }catch( UnknownHostException e){} 54 55 //不同端口循环扫描 56 for( i = MIN_port; i < MAX_port + Integer.parseInt( ThreadScan.maxThread.getText() ); 57 i += Integer.parseInt(ThreadScan.maxThread.getText() ) ){ 58 try{ 59 theTcpSocket = new Socket(hostAddress, i); 60 theTcpSocket.close(); 61 62 ThreadScan.Result.append( nameHost +":"+i); 63 64 switch( i ){ //其实这儿可以不用switch,直接用个hash表记录就可以 65 case 21: 66 typeport = "(FTP)"; 67 break; 68 case 23: 69 typeport = "(TELNET)"; 70 break; 71 case 25: 72 typeport = "SMTP"; 73 break; 74 case 80: 75 typeport = "HTTP"; 76 break; 77 case 110: 78 typeport = "POP"; 79 break; 80 case 139: 81 typeport = "netBIOS"; 82 break; 83 case 1433: 84 typeport = "SQL Server"; 85 break; 86 case 3389: 87 typeport = "Terminal Service"; 88 break; 89 case 443: 90 typeport = "HTTPS"; 91 break; 92 case 1521: 93 typeport = "Oracle"; 94 break; 95 } 96 97 //端口没有特定类型 98 if( typeport.equals("0")){ 99 ThreadScan.Result.append("\n");100 }else{101 ThreadScan.Result.append(":" + typeport + "\n");102 } 103 }catch( IOException e){}104 }105 }106 if( i == MAX_port + Integer.parseInt(ThreadScan.maxThread.getText())){107 ThreadScan.Result.append("\n" + "扫描完成...");108 //请"确定"按钮设置为可用109 if( !ThreadScan.Submit.setEnable( true ) );110 }111 }112 113 //按主机名进行端口扫描114 if( 1 == type ){115 for( i = MIN_port + NUM_thread; i < MAX_port + Integer.parseInt(ThreadScan.maxThread.getText());116 i += Integer.parseInt( ThreadScan.maxThread.getText( ) ) ) {117 try{118 theTcpSocket = new Socket( hostAddress, i );119 theTcpSocket.close();120 ThreadScan.Result.append(" " + i);121 switch( i ){ //其实这儿可以不用switch,直接用个hash表记录就可以122 case 21:123 typeport = "(FTP)";124 break;125 case 23:126 typeport = "(TELNET)";127 break;128 case 25:129 typeport = "SMTP";130 break;131 case 80:132 typeport = "HTTP";133 break;134 case 110:135 typeport = "POP";136 break;137 case 139:138 typeport = "netBIOS";139 break;140 case 1433:141 typeport = "SQL Server";142 break;143 case 3389:144 typeport = "Terminal Service";145 break;146 case 443:147 typeport = "HTTPS";148 break;149 case 1521:150 typeport = "Oracle";151 break;152 }153 if( typeport.equals("0") ){154 ThreadScan.Result.append("\n");155 }else{156 ThreadScan.Result.append(":" + typeport + "\n");157 }158 }catch( IOException e){159 160 }161 }162 if( i == MAX_port + Integer.parseInt(ThreadScan.maxThread.getText())){163 ThreadScan.Result.append("\n" + "扫描完成...");164 if( !ThreadScan.Submit.isEnable()){165 ThreadScan.Submit.setEnabled( true );166 }167 } 168 }//End of if169 }170 }
1 import java.net.*; 2 import java.io.*; 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 /** 7 * 实现扫描的主体程序,TreadScan 8 * @author Administrator 9 * 10 */ 11 12 public class ThreadScan { 13 14 public static JFrame main = new JFrame("Scaner(V1.0)By Nevermore"); //注册框架类 15 public static JTextArea Result = new JTextArea("", 4, 40); //显示扫描结果 16 public static JScrollPane resultPane = new 17 JScrollPane( Result, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 18 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 19 20 21 public static JTextField nameHost = new JTextField("localhost", 8 );//输入主机名文本框 22 public static JTextField fromip1 = new JTextField("0", 3); //输入IP地址前三位的文本框 23 public static JTextField fromip2 = new JTextField("0", 3); //输入IP地址4~6位的文本框 24 public static JTextField fromip3 = new JTextField("0", 3); //输入IP地址7~9位的文本框 25 public static JTextField fromip4 = new JTextField("0", 3); //输入IP地址后三位的文本框 26 27 public static JTextField toip = new JTextField("0", 3); //输入目标IP地址后四位 28 29 public static JTextField minPort = new JTextField("0", 4); //最小端口输入框 30 public static JTextField maxPort = new JTextField("1000", 4); //最大端口输入框 31 32 public static JTextField maxThread = new JTextField("100", 3); //最大线程数 33 public static JDialog DLGError = new JDialog(main, "错误!"); //错误提示框 34 public static JLabel DLGINFO = new JLabel(""); 35 public static JLabel type = new JLabel("请选择:"); 36 //扫描类型 37 public static JRadioButton radioIP = new JRadioButton("IP地址:"); 38 public static JRadioButton radioHost = new JRadioButton("主机名:", true); 39 //单选按钮组 40 public static ButtonGroup group= new ButtonGroup(); 41 public static JLabel p1 = new JLabel("端口范围:"); 42 public static JLabel p2 = new JLabel("~"); 43 public static JLabel p3 = new JLabel("~"); 44 public static JLabel Pdot1 = new JLabel("."); 45 public static JLabel Pdot2 = new JLabel("."); 46 public static JLabel Pdot3 = new JLabel("."); 47 public static JLabel TNUM = new JLabel("线程数:"); 48 public static JLabel RST = new JLabel("扫描结果:"); 49 public static JLabel con = new JLabel(""); 50 //定义按钮 51 public static JButton Ok = new JButton("确定"); 52 public static JButton Submit = new JButton("开始扫描"); 53 public static JButton Cancel = new JButton("退出"); 54 public static JButton saveButton = new JButton("保存扫描结果"); 55 //菜单栏设计:这一块好好学习学习 56 public static JMenuBar myBar = new JMenuBar(); 57 public static JMenu myMenu = new JMenu("文件(F)"); 58 public static JMenuItem saveItem = new JMenuItem("保存扫描结果(S)"); 59 public static JMenuItem exitItem = new JMenuItem("退出(Q)"); 60 public static JMenu myMenu2 = new JMenu("帮助"); 61 public static JMenuItem helpItem = new JMenuItem("阅读"); 62 63 /** 64 * 主方法 65 */ 66 public static void main( String[] argcs ){ 67 main.setSize(500, 400); 68 main.setLocation(400, 400); 69 main.setResizable(false); 70 main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 71 72 DLGError.setSize(300, 100); 73 DLGError.setLocation(400, 400); 74 //添加"菜单栏" 75 myMenu.add(saveItem); 76 myMenu.add(exitItem); 77 78 myMenu2.add(helpItem); 79 80 myBar.add(myMenu);//将菜单条目添加到菜单 81 myBar.add(myMenu2); 82 83 main.setJMenuBar(myBar);//将菜单添加到框架 84 //设置热键 85 myMenu.setMnemonic('F'); 86 saveItem.setMnemonic('S'); 87 //为"另存为"组建设置快捷键CTRL + S 88 saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK)); 89 //采用表格包模式布局 90 Container mPanel = main.getContentPane(); 91 GridBagConstraints c = new GridBagConstraints(); 92 c.insets = new Insets(10, 0, 0, 10); 93 94 c.gridx = 0;// 设置表格坐标 95 c.gridy = 0; 96 c.gridwidth = 10; 97 c.fill = GridBagConstraints.BOTH; 98 c.anchor = GridBagConstraints.CENTER; 99 mPanel.add(type, c);100 101 group.add(radioIP);102 group.add(radioHost);103 104 c.gridx = 0;105 c.gridy = 1;106 c.gridwidth = 1;107 c.fill = GridBagConstraints.BOTH;108 c.anchor = GridBagConstraints.CENTER;109 mPanel.add(radioIP, c);110 111 c.gridx = 1;112 c.gridy = 1;113 c.gridwidth = 1;114 c.fill = GridBagConstraints.CENTER;115 mPanel.add(fromip1, c);116 117 c.gridx = 2;118 c.gridy = 1;119 c.gridwidth = 1;120 c.fill = GridBagConstraints.BOTH;121 mPanel.add(Pdot1, c);122 123 c.gridx = 3;124 c.gridy = 1;125 c.gridwidth = 1;126 c.fill = GridBagConstraints.BOTH;127 c.anchor = GridBagConstraints.CENTER;128 mPanel.add(fromip2, c);129 130 c.gridx = 4;131 c.gridy = 1;132 c.gridwidth = 1;133 c.fill = GridBagConstraints.BOTH;134 c.anchor = GridBagConstraints.CENTER;135 mPanel.add(Pdot2, c);136 137 c.gridx = 5;138 c.gridy = 1;139 c.gridwidth = 1;140 c.fill = GridBagConstraints.BOTH;141 c.anchor = GridBagConstraints.CENTER;142 mPanel.add(fromip3, c);143 144 c.gridy = 1;145 c.gridx = 6;146 c.gridwidth = 1;147 c.fill = GridBagConstraints.BOTH;148 c.anchor = GridBagConstraints.CENTER;149 mPanel.add(Pdot3, c);150 151 152 c.gridx = 7;153 c.gridy = 1;154 c.gridwidth = 1;155 c.fill = GridBagConstraints.BOTH;156 c.anchor = GridBagConstraints.CENTER;157 mPanel.add(fromip4, c);158 159 c.gridx = 8;160 c.gridy = 1;161 c.gridwidth = 1;162 c.fill = GridBagConstraints.BOTH;163 c.anchor = GridBagConstraints.CENTER;164 mPanel.add(p2, c);165 166 c.gridx = 9;167 c.gridy = 1;168 c.gridwidth = 1;169 c.fill = GridBagConstraints.BOTH;170 c.anchor = GridBagConstraints.CENTER;171 mPanel.add(toip, c);172 173 c.gridx = 0;174 c.gridy = 2;175 c.gridwidth = 1;176 c.fill = GridBagConstraints.BOTH;177 c.anchor = GridBagConstraints.CENTER;178 mPanel.add(radioHost, c);179 180 c.gridx = 1;181 c.gridy = 2;182 c.gridwidth = 3;183 c.fill = GridBagConstraints.BOTH;184 c.anchor = GridBagConstraints.CENTER;185 mPanel.add(nameHost, c);186 187 c.gridx = 0;188 c.gridy = 3;189 c.gridwidth = 1;190 c.fill = GridBagConstraints.BOTH;191 c.anchor = GridBagConstraints.CENTER;192 mPanel.add(p1, c);193 194 c.gridx = 1;195 c.gridy = 3;196 c.gridwidth = 1;197 c.fill = GridBagConstraints.BOTH;198 c.anchor = GridBagConstraints.CENTER;199 mPanel.add(minPort, c);200 201 c.gridx = 2;202 c.gridy = 3;203 c.gridwidth = 1;204 c.fill = GridBagConstraints.BOTH;205 c.anchor = GridBagConstraints.CENTER;206 mPanel.add(p3, c);207 208 c.gridx = 3;209 c.gridy = 3;210 c.gridwidth = 1;211 c.fill = GridBagConstraints.BOTH;212 c.anchor = GridBagConstraints.CENTER;213 mPanel.add(maxPort, c);214 215 c.gridx = 0;216 c.gridy = 4;217 c.gridwidth = 1;218 c.fill = GridBagConstraints.BOTH;219 c.anchor = GridBagConstraints.CENTER;220 mPanel.add(TNUM, c);221 222 c.gridx = 1;223 c.gridy = 4;224 c.gridwidth = 3;225 c.fill = GridBagConstraints.BOTH;226 c.anchor = GridBagConstraints.CENTER;227 mPanel.add(maxThread, c);228 229 c.gridx = 0;230 c.gridy = 5;231 c.gridwidth = 3;232 c.fill = GridBagConstraints.BOTH;233 c.anchor = GridBagConstraints.CENTER;234 mPanel.add(Submit, c);235 236 237 c.gridx = 3;238 c.gridy = 5;239 c.gridwidth = 3;240 c.fill = GridBagConstraints.BOTH;241 c.anchor = GridBagConstraints.CENTER;242 mPanel.add(saveButton, c);243 244 c.gridx = 6;245 c.gridy = 5;246 c.gridwidth =4;247 c.fill = GridBagConstraints.BOTH;248 c.anchor = GridBagConstraints.CENTER;249 mPanel.add(Cancel, c);250 251 c.gridx = 0;252 c.gridy = 6;253 c.gridwidth = 10;254 c.fill = GridBagConstraints.BOTH;255 c.anchor = GridBagConstraints.CENTER;256 mPanel.add(RST, c);257 258 //设置文本域可以换行259 Result.setLineWrap(true);260 //设置文本域不可编辑261 Result.setEditable(false);262 263 264 c.gridx = 0;265 c.gridy = 7;266 c.gridwidth = 10;267 c.gridheight = 4;268 c.fill = GridBagConstraints.VERTICAL;269 c.anchor = GridBagConstraints.CENTER;270 mPanel.add(resultPane, c);271 272 Container dPanel = DLGError.getContentPane();273 dPanel.setLayout(new FlowLayout(FlowLayout.CENTER));274 dPanel.add(DLGINFO);275 dPanel.add(Ok);276 277 278 Submit.addActionListener(new SubmitAction());279 Cancel.addActionListener(new CancelAction());280 Ok.addActionListener(new OkAction());281 282 //实现保存功能283 saveItem.addActionListener(new java.awt.event.ActionListener(){284 public void actionPerformed(java.awt.event.ActionEvent e){285 JFileChooser fc = new JFileChooser();286 int returnVal = fc.showSaveDialog(null);287 //单击保存按钮288 if( 0 == returnVal ){289 File saveFile = fc.getSelectedFile();290 try{291 FileWriter writeOut = new FileWriter( saveFile );292 writeOut.write(ThreadScan.Result.getText());293 writeOut.close();294 295 }catch( IOException ex ){ System.out.println("保存失败");}296 }297 }298 });299 //实现退出功能300 ActionListener li = new java.awt.event.ActionListener(){301 public void actionPerformed(java.awt.event.ActionEvent e){302 System.exit(0);303 }304 };305 exitItem.addActionListener(li);306 //实现帮助功能307 308 ActionListener lil = new java.awt.event.ActionListener(){309 public void actionPerformed(java.awt.event.ActionEvent e){310 new AboutDialog();//311 }312 };313 helpItem.addActionListener(lil);314 315 ActionListener lill = new java.awt.event.ActionListener(){316 public void actionPerformed(java.awt.event.ActionEvent e){317 JFileChooser fc = new JFileChooser();318 int returnVal = fc.showSaveDialog(null);319 //单击保存按钮320 if( 0 == returnVal ){321 File saveFile = fc.getSelectedFile();322 try{323 FileWriter writeOut = new FileWriter(saveFile);324 writeOut.write(ThreadScan.Result.getText());325 writeOut.close();326 }catch(IOException ex ){ System.out.println("保存失败");}327 }else{328 return;//单击取消329 }330 331 }332 };333 saveButton.addActionListener(lill);334 main.setVisible(true);335 }336 }337 /**338 * 339 * @author Nevermore340 * 实现取消功能341 */342 class CancelAction implements ActionListener{343 public void actionPerformed( ActionEvent e){344 System.exit(0);345 }346 }347 /**348 * 349 * @author Nevermore350 * 实现确定按钮351 */352 class SubmitAction implements ActionListener{353 public void actionPerformed( ActionEvent a){354 int minPort;355 int maxPort;356 int maxThread;357 358 int ip1 = 0;359 int ip2 = 0;360 int ip3 = 0;361 int ipstart = 0;362 int ipend = 0;363 364 String ipaddress = "";365 String nameHost = "";366 ThreadScan.Result.setText("");367 if( ThreadScan.Submit.isEnabled()){368 ThreadScan.Submit.setEnabled(false);369 }370 /**371 * 判断扫描类型372 * 根据IP地址扫描 type = 0373 *根据主机名称扫描type = 1374 */375 if( ThreadScan.radioIP.isSelected()){376 TcpThread.type = 0;377 //判断IP地址的前三位是否是int型378 try{379 ip1 = Integer.parseInt(ThreadScan.fromip1.getText());380 }catch( NumberFormatException e ){381 ThreadScan.DLGINFO.setText("错误的IP地址");382 ThreadScan.DLGError.setVisible(true);383 return;384 }385 try{386 ip2 = Integer.parseInt(ThreadScan.fromip2.getText());387 }catch( NumberFormatException e ){388 ThreadScan.DLGINFO.setText("错误的IP地址");389 ThreadScan.DLGError.setVisible(true);390 return;391 }392 try{393 ip3 = Integer.parseInt(ThreadScan.fromip3.getText());394 }catch( NumberFormatException e ){395 ThreadScan.DLGINFO.setText("错误的IP地址");396 ThreadScan.DLGError.setVisible(true);397 return;398 }399 try{400 ipstart = Integer.parseInt(ThreadScan.fromip4.getText());401 }catch( NumberFormatException e ){402 ThreadScan.DLGINFO.setText("错误的IP地址");403 ThreadScan.DLGError.setVisible(true);404 return;405 }406 try{407 ipend = Integer.parseInt(ThreadScan.toip.getText());408 }catch( NumberFormatException e ){409 ThreadScan.DLGINFO.setText("错误的IP地址");410 ThreadScan.DLGError.setVisible(true);411 return;412 }413 414 //判断是否是合理的IP地址415 if(ip1 < 0 || ip1 > 255 || ip2 < 0 || ip2 > 255 ||416 ip3 < 0 || ip3 > 255 || ipstart < 0 || ipstart > 255 ){417 ThreadScan.DLGINFO.setText("IP地址为0~255的整数");418 ThreadScan.DLGError.setVisible(true);419 return ;420 }else{421 TcpThread.ip1 = ip1;422 TcpThread.ip2 = ip2;423 TcpThread.ip3 = ip3;424 TcpThread.ipstart = ipstart;425 }426 //判断目标IP地是否合理427 if( ipend < 0 || ipend > 255 ){428 ThreadScan.DLGINFO.setText("目标IP地址的范围是0~255");429 ThreadScan.DLGError.setVisible(true);430 return;431 }else{432 TcpThread.ipend = ipend;433 }434 435 ipaddress = "" + ip1 + ip2 + ip3 + ipstart;436 437 /**438 * 判断IP地址的有效性439 */440 try{441 TcpThread.hostAddress = InetAddress.getByName(ipaddress);442 }catch( UnknownHostException e){443 ThreadScan.DLGINFO.setText("错误的IP或IP地址不可到达!");444 ThreadScan.DLGError.setVisible(true);445 return;446 }447 448 if( ThreadScan.radioHost.isSelected()){449 TcpThread.type = 1;450 /**451 * 判断主机名的有效性452 */453 try{454 TcpThread.hostAddress = InetAddress.getByName(ThreadScan.nameHost.getText());455 }catch( UnknownHostException e){ 456 ThreadScan.DLGINFO.setText("错误的域名或地址不可到达!");457 ThreadScan.DLGError.setVisible(true);458 return;459 }460 }461 /**462 * 判断端口号的有效性463 */464 try{465 minPort = Integer.parseInt(ThreadScan.minPort.getText());466 maxPort = Integer.parseInt(ThreadScan.maxPort.getText());467 maxThread = Integer.parseInt(ThreadScan.maxThread.getText());468 }catch( NumberFormatException e ){469 ThreadScan.DLGINFO.setText("错误的端口号或端口号和线程数必须为整数");470 ThreadScan.DLGError.setVisible(true);471 return;472 }473 /**474 * 判断最小端口号的有效范围475 * 判断条件大于0小于65535最大端口号大于最小端口号476 */477 if( minPort < 0 || minPort > 65535 || minPort > maxPort ){478 ThreadScan.DLGINFO.setText("端口号范围:0~65535,并且最大端口号应大于最小端口号!");479 ThreadScan.DLGError.setVisible(true);480 return;481 }else{482 TcpThread.MIN_port = minPort;483 }484 /**485 * 判断最大端口号的有效范围486 */487 if( maxPort < 0 || maxPort > 65535 || maxPort < minPort ){488 ThreadScan.DLGINFO.setText("端口号范围:0~65535,并且最大端口号应大于最小端口号!");489 ThreadScan.DLGError.setVisible(true);490 return;491 }else{492 TcpThread.MAX_port = maxPort;493 }494 /**495 * 判断线程数的有效范围496 * 判断条件 大于1且小于200497 */498 if( maxThread < 1 || maxThread > 200 ){499 ThreadScan.DLGINFO.setText("线程数的有效范围是1~200");500 ThreadScan.DLGError.setVisible(true);501 return;502 }503 ThreadScan.Result.append("线程数" + ThreadScan.maxThread.getText() + "\n");504 505 /**506 * 启动线程507 */508 for( int i = 0; i < maxThread; i++ ){509 new TcpThread("T" + i, i).start();510 }511 } 512 }513 }514 /**515 * 516 * @author Administrator517 * 错误对话框518 */519 class OkAction implements ActionListener{520 public void actionPerformed( ActionEvent e){521 ThreadScan.DLGError.dispose();522 }523 }
import javax.swing.*;import java.awt.*;public class AboutDialog extends JDialog{ JPanel JMainPane = new JPanel(); JTabbedPane jTabbedPane = new JTabbedPane(); private JPanel JPanel1 = new JPanel(); private JPanel JPanel2 = new JPanel(); private JTextArea jt1 = new JTextArea(6, 6); private JTextArea jt2 = new JTextArea(6, 6); /** * 构造函数 */ public AboutDialog(){ setTitle("Scaner"); setSize(300,200); setResizable(false); setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE); Container c = this.getContentPane(); jt1.setSize(260,200); jt2.setSize(260,200); jt1.setEditable(false); jt2.setEditable(false); jt1.setLineWrap(true); jt2.setLineWrap(true); jt1.setText(""); jt1.setFont(new Font("楷体_GB2312", java.awt.Font.BOLD, 13)); jt2.setText(""); jt2.setFont(new Font("楷体_GB2312", java.awt.Font.BOLD, 13)); jt1.setForeground(Color.black); jt2.setForeground(Color.black); JPanel1.add(jt1); JPanel2.add(jt2); jTabbedPane.setSize(300,200); jTabbedPane.addTab("扫描原理", null, JPanel1, null); jTabbedPane.addTab("使用说明", null, JPanel2, null); JMainPane.add(jTabbedPane); c.add(JMainPane); pack(); this.setVisible(true); }}