Advertisement
Guest User

OutProxyTester

a guest
Dec 8th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.10 KB | None | 0 0
  1. package net.i2p.hack.scripts;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.DataInputStream;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileWriter;
  10. import java.io.IOException;
  11. import java.io.InputStreamReader;
  12. import java.net.HttpURLConnection;
  13. import java.net.URL;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import java.util.Properties;
  17. import java.util.Scanner;
  18. import java.util.StringTokenizer;
  19.  
  20. import net.i2p.hack.scripts.util.SystemCommandExecutor;
  21.  
  22. import org.apache.commons.lang3.StringUtils;
  23.  
  24. /**
  25.  * Test the hosts in public internet to compare the response with the I2P EEPSITES response.
  26.  * @author Adastra
  27.  * */
  28. public class OutProxyTester implements Runnable{
  29.    
  30.     /**
  31.      * The path where the file ""
  32.      * */
  33.     private FileInputStream addressNetDB;
  34.  
  35.    
  36.     /**
  37.      * The path of the results.
  38.      * */
  39.     private File storeResultsPath;
  40.    
  41.     /**
  42.      * Main rutine.
  43.      * */
  44.     public static void main(String ... args) {
  45.         OutProxyTester outproxy = new OutProxyTester();
  46.         outproxy.readAddressesInside();
  47.         if(outproxy.addressNetDB == null) {
  48.             System.out.println("Addresses File Not found, I can't continue...");
  49.             System.exit(0);
  50.         }
  51.         outproxy.readScanningDir();
  52.         outproxy.crawlOutsideServers();
  53.        
  54.     }
  55.  
  56.     /**
  57.      * Read the file of addresses corresponding the PCAP file generated and I2P NetDB Service
  58.      * */
  59.     private void readAddressesInside() {
  60.         System.out.println("Read the file of Addresses (Addresses correlated between PCAP file and Local I2P NetDb)...");
  61.         Scanner scanner = new Scanner(System.in);
  62.         String addressesFile = scanner.next();
  63.         System.out.println("Addresses File: "+addressesFile);
  64.         try {
  65.             addressNetDB = new FileInputStream(new File(addressesFile));
  66.         } catch(FileNotFoundException file_ex) {
  67.             file_ex.printStackTrace();
  68.             System.out.println("Exception: "+file_ex.getMessage());
  69.         }
  70.  
  71.     }
  72.    
  73.     /**
  74.      * Read the Scanning Directory to store all result files.
  75.      * */
  76.     private void readScanningDir() {
  77.         System.out.println("Enter the Directory to Store Files \n");
  78.         Scanner scanner = new Scanner(System.in);
  79.         String dirStore = scanner.next();
  80.         System.out.println("Dir. to store: "+dirStore);
  81.         this.storeResultsPath = new File(dirStore);
  82.         // /media/Adastra/ScanningResultsI2P
  83.         if(!this.storeResultsPath.exists()) {
  84.             this.storeResultsPath.mkdir();  
  85.         }
  86.     }  
  87.    
  88.     /**
  89.      * Crawl every site in the list of IP Addresses.
  90.      * */
  91.     public void crawlOutsideServers() {
  92.         try {
  93.             DataInputStream in = new DataInputStream(addressNetDB);
  94.             BufferedReader br = new BufferedReader(new InputStreamReader(in));
  95.             String outproxySite;
  96.             while ((outproxySite = br.readLine()) != null) {
  97.                 Thread crawler = new Thread(this);
  98.                 crawler.setName(outproxySite);
  99.                 crawler.start();
  100.             }
  101.             in.close();
  102.             br.close();
  103.         } catch (IOException e) {
  104.             e.printStackTrace();
  105.         }
  106.     }
  107.    
  108.     /**
  109.      * Overrided method to execute the current Thread.
  110.      * */
  111.     @Override
  112.     public void run() {
  113.         connectOutSiteWebServer(Thread.currentThread().getName());
  114.     }
  115.    
  116.     /**
  117.      * Method to perform the request to the web server (if there's one).
  118.      * The response is stored to perform the analisys.
  119.      * */
  120.     private void connectOutSiteWebServer(String outproxySite) {
  121.         HttpURLConnection connection = null;   
  122.         try {
  123.             String outproxySiteHttp = "http://"+outproxySite;
  124.             URL rutaPeticion = new URL(outproxySiteHttp);  
  125.             Properties sysProps = System.getProperties();
  126.             sysProps.put( "http.proxyHost", "127.0.0.1");
  127.             sysProps.put( "http.proxyPort", "81" );
  128.            
  129.                 connection = (HttpURLConnection)rutaPeticion.openConnection();
  130.                 connection.setRequestProperty("Content-type", "html/text");  
  131.                 connection.connect();
  132.                 if(Math.round((connection.getResponseCode()/100)) == 2) {
  133.                     BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  134.                     String content = "";
  135.                     while(br.readLine() != null) {
  136.                         content = content+"\n"+br.readLine();              
  137.                     }
  138.                     String iframeName = storeResultsPath+System.getProperty("file.separator")+outproxySite+"_outside.html";
  139.                     FileWriter iframeFile = new FileWriter(new File(iframeName));      
  140.                     BufferedWriter iframeWriter = new BufferedWriter(iframeFile);
  141.                     iframeWriter.write(content);
  142.                     iframeWriter.close();
  143.  
  144.                    
  145.                     List<String> command = new ArrayList<String>();
  146.                     command.add("dig");
  147.                     command.add("-x");
  148.                     command.add(outproxySite);
  149.  
  150.                     SystemCommandExecutor commandExecutor = new SystemCommandExecutor(command);
  151.                     commandExecutor.executeCommand();
  152.                     String result = commandExecutor.getStandardOutputFromCommand().toString();
  153.                     StringTokenizer stResults = new StringTokenizer(result, "\n");
  154.                     while(stResults.hasMoreTokens()) {
  155.                         String token = stResults.nextToken();
  156.                         if(StringUtils.isNotEmpty(token) && StringUtils.startsWith(token, "REVERSE")) {
  157.                             storeInformationReverseDNS(outproxySite+","+token+", "+connection.getLastModified()+"\n\n");   
  158.                         }
  159.                     }
  160.                      
  161.  
  162.                
  163.                     storeInformationOutproxySite(outproxySiteHttp+","+connection.getResponseCode()+","+connection.getResponseMessage()+","+connection.getHeaderField("Server")+","+connection.getHeaderField("Host")+"\n");                    
  164.                 } else {
  165.                     storeFailedOutproxySite(outproxySiteHttp+","+connection.getResponseCode()+","+connection.getResponseMessage()+","+connection.getHeaderField("Server")+","+connection.getHeaderField("Host")+"\n");
  166.                 }
  167.         }catch(IOException ioException){
  168.         } catch (InterruptedException e) {
  169.             // TODO Auto-generated catch block
  170.             e.printStackTrace();
  171.         } finally {
  172.             connection.disconnect();
  173.         }
  174.  
  175.     }
  176.  
  177. /**
  178.  * Store Banned information about EEPSITES scanned in the network.
  179.  * */
  180. private void storeInformationOutproxySite(String outproxySite) {
  181.     try {
  182.         FileWriter outproxyFile = new FileWriter(new File(storeResultsPath+System.getProperty("file.separator")+"outproxiesSite.txt"), true);
  183.         BufferedWriter csvWriter = new BufferedWriter(outproxyFile);
  184.         csvWriter.write(outproxySite);
  185.         csvWriter.close();             
  186.     } catch (IOException e) {
  187.         // TODO Auto-generated catch block
  188.         e.printStackTrace();
  189.     }  
  190. }
  191.  
  192. private void storeInformationReverseDNS(String resolvedIP) {
  193.     try {
  194.         FileWriter reverseDNS = new FileWriter(new File(storeResultsPath+System.getProperty("file.separator")+"reverseDNSAddresses.txt"), true);
  195.         BufferedWriter reverseDNSWriter = new BufferedWriter(reverseDNS);
  196.         reverseDNSWriter.write(resolvedIP);
  197.         reverseDNSWriter.close();              
  198.     } catch (IOException e) {
  199.         // TODO Auto-generated catch block
  200.         e.printStackTrace();
  201.     }
  202. }
  203. /**
  204.  * Store the failed addresses to find EEPSITES.
  205.  * */
  206. private void storeFailedOutproxySite(String failed) {
  207.     try {
  208.         FileWriter outproxyFailed = new FileWriter(new File(storeResultsPath+System.getProperty("file.separator")+"outproxiesFailed.txt"), true);
  209.         BufferedWriter outproxyFailedWriter = new BufferedWriter(outproxyFailed);
  210.         outproxyFailedWriter.write(failed);
  211.         outproxyFailedWriter.close();
  212.     } catch (IOException e) {
  213.         // TODO Auto-generated catch block
  214.         e.printStackTrace();
  215.     }      
  216. }  
  217.  
  218. }
  219.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement