Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.i2p.hack.scripts;
- import java.io.BufferedInputStream;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.DataInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.Serializable;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Scanner;
- import java.util.Set;
- import java.util.StringTokenizer;
- import net.i2p.data.DataFormatException;
- import net.i2p.data.RouterAddress;
- import net.i2p.data.RouterInfo;
- import org.apache.commons.lang3.StringUtils;
- /**
- * Search in every "router-info" file under "netDB" directory.
- * Search for IP Addresses for each readed file.
- * @author Adastra
- * */
- public class NetDBFilterIP implements Serializable {
- /**
- * Serial ID.
- */
- private static final long serialVersionUID = 1L;
- /**
- * The path of the results.
- * */
- private File storeResultsPath;
- /**
- * List of IP Addresses to exclude.
- * */
- private static List<String> excludeAddresses = new ArrayList<String>();
- /**
- * CSV File of Results separed with comma.
- * */
- private FileWriter addressesInsideNetDb;
- /**
- * CSV File of Results separed with comma.
- * */
- private FileWriter addressNotFound;
- /**
- * Main execution flow.
- * @param args Parameters.
- * */
- public static void main(String ... args) {
- excludeAddresses.add("192.168.1.33");
- excludeAddresses.add("192.168.1.1");
- NetDBFilterIP netFilter = new NetDBFilterIP();
- netFilter.readNetDBDirectory();
- }
- /**
- * Reads the NetDB directory.
- * */
- private void readNetDBDirectory() {
- String i2pNetDB="";
- System.out.println("Enter the location NetDb Directory\n");
- Scanner scanner = new Scanner(System.in);
- i2pNetDB = scanner.next();
- System.out.println("NetDb Directory: "+i2pNetDB);
- searchIPAddresses(i2pNetDB);
- }
- /**
- * Search the IP Addresses registered in every "Route-info" file of the Local NetDB.
- * */
- private void searchIPAddresses(String i2pNetDB) {
- File netDBRoot = new File(i2pNetDB);
- List<RouterInfo> routersNetDB = new ArrayList<RouterInfo>();
- readScanningDir();
- try {
- File[] files = netDBRoot.listFiles();
- for(File file : files) {
- System.out.println("File "+file.getName());
- BufferedInputStream binput = new BufferedInputStream(new FileInputStream(file));
- RouterInfo router = new RouterInfo();
- try {
- router.readBytes(binput);
- } catch (DataFormatException e) {
- e.printStackTrace();
- }
- routersNetDB.add(router);
- }
- System.out.println("Enter the Path of the CVS generated from PCAP file");
- Scanner scanner = new Scanner(System.in);
- String rutaCSVCap = scanner.next();
- DataInputStream in = new DataInputStream(new FileInputStream(new File(rutaCSVCap)));
- BufferedReader br = new BufferedReader(new InputStreamReader(in));
- String line="";
- List<String> addressesCSV = new ArrayList<String>();
- while((line = br.readLine()) != null) {
- StringTokenizer tokenizer = new StringTokenizer(line, ",");
- if(tokenizer.hasMoreTokens()) {
- String ipAddress = tokenizer.nextToken();
- if(StringUtils.isNotEmpty(ipAddress) && ipAddress.contains(".")) {
- addressesCSV.add(ipAddress);
- }
- }
- }
- addressesCSV.removeAll(excludeAddresses);
- Set<String> uniqueAddresses = new HashSet<String>(addressesCSV);
- Set<String> addressesFound = new HashSet<String>();
- Set<String> addressesNotFound = new HashSet<String>();
- for(RouterInfo router : routersNetDB) {
- for(RouterAddress routerAddress : router.getAddresses()) {
- if(uniqueAddresses.contains(routerAddress.getOptions().get("host"))) {
- addressesFound.add(routerAddress.getOptions().get("host").toString());
- } else {
- if(routerAddress.getOptions().get("host") == null) {
- for(int i = 0; i <=5; i++){
- if(routerAddress.getOptions().get("ihost"+i) != null) {
- if(uniqueAddresses.contains(routerAddress.getOptions().get("ihost"+i)))
- addressesFound.add(routerAddress.getOptions().get("ihost"+i).toString());
- else
- addressesNotFound.add(routerAddress.getOptions().get("ihost"+i).toString());
- }
- }
- continue;
- }
- addressesNotFound.add(routerAddress.getOptions().get("host").toString());
- }
- }
- }
- for(String address : addressesFound) {
- storeAddress(address+"\n");
- }
- for(String address : addressesNotFound) {
- storeNotFound(address+"\n");
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * Open and appends the file with addresses found.
- * */
- private void storeAddress(String content) {
- try {
- addressesInsideNetDb = new FileWriter(new File(storeResultsPath+System.getProperty("file.separator")+"addressesInsideNetDb.txt"), true);
- BufferedWriter csvAddressWriter = new BufferedWriter(addressesInsideNetDb);
- csvAddressWriter.write(content);
- csvAddressWriter.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * Open and appends the file with addresses not found.
- * */
- private void storeNotFound(String content) {
- try {
- addressNotFound = new FileWriter(new File(storeResultsPath+System.getProperty("file.separator")+"addressesNotFound.txt"), true);
- BufferedWriter csvAddressNotFoundWriter = new BufferedWriter(addressNotFound);
- csvAddressNotFoundWriter.write(content);
- csvAddressNotFoundWriter.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * Read the Scanning Directory to store all result files.
- * */
- private void readScanningDir() {
- System.out.println("Enter the Directory to Store Files \n");
- Scanner scanner = new Scanner(System.in);
- String dirStore = scanner.next();
- System.out.println("Dir. to store: "+dirStore);
- storeResultsPath = new File(dirStore);
- // /media/Adastra/ScanningResultsI2P
- if(!storeResultsPath.exists()) {
- storeResultsPath.mkdir();
- } else {
- File resultsCSV = new File(dirStore+System.getProperty("file.separator")+"resultsCSV.txt");
- if(resultsCSV.exists()) {
- try {
- DataInputStream in = new DataInputStream(new FileInputStream(resultsCSV));
- BufferedReader br = new BufferedReader(new InputStreamReader(in));
- String informationEepsite;
- List<String> informationEepsites = new ArrayList<String>();
- while ((informationEepsite = br.readLine()) != null) {
- informationEepsites.add(informationEepsite);
- }
- in.close();
- br.close();
- Set<String> uniqueInformation = new HashSet<String>(informationEepsites);
- FileWriter resultsCSVUnique = new FileWriter(resultsCSV, false);
- BufferedWriter csvAddressNotFoundWriter = new BufferedWriter(resultsCSVUnique);
- for(String eepsiteInfo : uniqueInformation)
- csvAddressNotFoundWriter.write(eepsiteInfo);
- csvAddressNotFoundWriter.close();
- }catch (IOException e) {
- System.out.println("Cleanning of results Failed...");
- e.printStackTrace();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement