def sendRequests(self, runManytimesMode, requestTimes): try: Helper.printOnScreen( (term.format("Requesting our webiste:\n", term.Attr.BOLD)), color=MSG_TYPES.RESULT, mode=self.mode) domain = str(self.exitNodeIp).replace('.', '-') + '.' + self.domainUrl url = 'http://' + domain result = self.query(url) # requesting many times/ testing the same node 100 times./ testing how random is the PORT number and Requset ID if runManytimesMode is True: self.requestDomain(domain=domain, times=requestTimes) self.killConnection() if result is True: Helper.printOnScreenAlways( ('Successfully connected over TOR: %s' % url), color=MSG_TYPES.RESULT) return Result(CONNECTION_STATUS.CONNECTED, DOMAIN_STATUS.ACCESSIBLE) else: return Result(CONNECTION_STATUS.CONNECTED, DOMAIN_STATUS.NOT_ACCESSIBLE) except Exception as ex: self.killConnection() TORFunctions.loggingError('Connection - sendRequests: %s' % traceback.format_exc()) return Result(CONNECTION_STATUS.CONNECTED, DOMAIN_STATUS.STATELESS) # or something went wrong
def ExitNode(self): count = 0 exit_Nodes = [] stem_Nodes = stem.descriptor.remote.get_server_descriptors() for desc in stem_Nodes: # CheckingRequest if the Node is an exit one if desc.exit_policy.is_exiting_allowed(): count = count + 1 Helper.printOnScreen(' %s %s' % (desc.nickname, desc.address), MSG_TYPES.RESULT.value, self.mode) exit_Nodes.append({ 'ExitNode': { 'Address': desc.address, 'Fingerprint': desc.fingerprint, 'Nickname': desc.nickname, 'Dir_port': desc.or_port, 'Or_port': desc.dir_port } }) # Write into Json file with open(self.NODES_PATH, 'w') as outfile: json.dump(exit_Nodes, outfile)
def requestDomainViaTor(self): Helper.printOnScreenAlways('Requesting %s via TOR ' % self.DOMAIN_URL) start_time = time.time() nodesCount = 0 successfully_Connections = 0 successfully_Connections_Checking_Failed = 0 failed_Connections = 0 json_Objects = self.loadExitNodesFromJSON() if stem.util.system.is_windows(): # Terminate the tor in case if it is still running TORFunctions.ProcesskillForWindows('tor.exe') for obj in tqdm(json_Objects, ncols=80, desc='Requesting Domain via our DNS'): ip = str(obj['ExitNode']['Address'].encode("ascii"), 'utf-8') fingerprint = str(obj['ExitNode']['Fingerprint'].encode("ascii"), 'utf-8') nickname = str(obj['ExitNode']['Nickname'].encode("ascii"), 'utf-8') or_port = str(obj['ExitNode']['Or_port'].encode("ascii")) dir_port = str(obj['ExitNode']['Dir_port'].encode("ascii")) result = self.connectToTORExitNode(fingerprint, ip, nodesCount + 1, TASK_MODE.REQUEST_DOMAIN) exitNode = ExitNode(ipaddress=ip,fingerprint=fingerprint,nickname=nickname,or_port=or_port,dir_port=dir_port,status=result) self.ExitNodes_List.append(exitNode) self.Result_List.append(result) time_taken = time.time() - start_time finalResult = Results.FinalResult(self.Result_List, nodesCount, time_taken) cur_path = os.path.dirname(__file__) os.chdir(cur_path) new_path = os.path.relpath(self.PROCESSED_NODES_PATH, cur_path) Helper.storeExitNodesJSON(object=self.ExitNodes_List,path=new_path)
def start1(self,exitFingerprint, ip): # Start an instance of Tor configured to only exit through Russia. This prints # Tor's bootstrap information as it starts. Note that this likely will not # work if you have another Tor instance running. # Terminate the tor in case if it is still running if not (stem.util.system.get_pid_by_port(self.CONTROL_PORT)): print(term.format("Starting Tor, connecting to: %s \n", term.Attr.BOLD) % ip) tor_process = stem.process.launch_tor_with_config( timeout=90, completion_percent=100, config={ 'SocksPort': str(self.SOCKS_PORT), 'ExitNodes': '$' + exitFingerprint, 'ControlPort': str(self.CONTROL_PORT), 'DataDirectory': 'Connection_info', }, ) else: pass Helper.printOnScreen("\nChecking our endpoint: \n", MSG_TYPES.RESULT, mode=self.mode) url = 'http://'+str(ip).replace('.','-')+'.'+self.DOMAIN_URL result = self.query(url) if result is True: Helper.printOnScreen(('Successfully connected over TOR: %S' % url), MSG_TYPES.RESULT, mode=self.mode)
def graphTask(): # make the directories in case they are missing makeDirectories() DrawGraphsforALL = False # Helper.printOnScreenAlways("TEST",MSG_TYPES.RESULT) print('Files Directory: %s' % FILE_PATH) total, requests, all = getInfoFormTextFiles() print('Found %d records: ' % total) # TODO: need to be refactored/renamed- be more clear writeAllTextFiles(all) # write all the Requests into json file writeAllRequests(requests) getAllDNSIPs(requests) # match DNS with its port/ID they used. # normalizeDNSRequests(Requests) # for getting info for especial ip, port and ID # draw graphs for all the DNS records if DrawGraphsforALL is True: # print('Draw Graphs for each DNS records that has more than %d records...' % MINNUMBER_DrawGraph) Helper.printOnScreenAlways( 'Draw Graphs for each DNS that has more than %d records...' % MINNUMBER_DrawGraph, MSG_TYPES.RESULT) DrawGraphsForAll(requests) Helper.printOnScreenAlways( 'Done, Graphs are stored in the following directory: Graphs/DNS_Graphs/', MSG_TYPES.RESULT) drawGraphIDPORTNumber(requests, 1)
def checkTORConnection(self): ''' Check if is establishing connection over TOR working. ''' try: url = self.torCheckConnection if self.exitNodeIp == self.query(url): Helper.printOnScreen('Connected Successfully', color=MSG_TYPES.RESULT, mode=self.mode) self.wirteIntoFile('Connected Successfully') self.killConnection() return Result(CONNECTION_STATUS.CONNECTED, DOMAIN_STATUS.ACCESSIBLE) else: Helper.printOnScreen('Checking Failed ', color=MSG_TYPES.ERROR, mode=self.mode) self.wirteIntoFile('Checking Failed ') self.killConnection() return Result(CONNECTION_STATUS.CONNECTED, DOMAIN_STATUS.NOT_ACCESSIBLE) except Exception as ex: self.killConnection() TORFunctions.loggingError('Connection - checkTORConnection: %s' % traceback.format_exc()) return Result(CONNECTION_STATUS.CONNECTED, DOMAIN_STATUS.STATELESS) # or something went wrong
def loadExitNodes(self, dir): jsonFiles = glob.glob(str('%s/*.json' % dir)) with open(jsonFiles[0]) as f: jsonObjects = json.load(f) Helper.printOnScreenAlways('DNS records have been loaded\n', MSG_TYPES.YELLOW) return jsonObjects
def requestDomain(self, domain, times, responseMode, addtionname=None): # to avoid cashing results = [] try: randNumber = random.randint(1, 10000) for i in range(1, times): if addtionname is None: sub_Domain = ("%d_%d_%d_%s" % (randNumber, times, i, domain)) else: sub_Domain = ("%d_%s_%s" % (randNumber, addtionname, domain)) url = 'http://' + sub_Domain Helper.printOnScreen(('%d- Requesting: %s' % (i, url)), color=MSG_TYPES.RESULT, mode=self.mode) # TODO: need to be solved result_ = self.query(url) # no need to wait for the reponcse if result_ is not None: if 'sock' in result_.lower() and i > 3: print(result_) break except Exception as ex: print("requestDomain") print(ex)
def writeCheckedResult(self): connectionFailed = 0 connectionSucceeded = 0 connectedAndAccessible = 0 connectedAndReAccessible = 0 connectedButNotAccessible = 0 connectedFailed = 0 print("\n--------------------------") Helper.printOnScreenAlways('Finished in %0.2f seconds' % (self.timetaken)) Helper.printOnScreenAlways( 'Found ' + str(self.nodesCount) + ' Exit nodes', MSG_TYPES.RESULT) for result in self.resultList: if result.connectionStatus == CONNECTION_STATUS.CONNECTED.value: if result.requestingDomainStatus == DOMAIN_STATUS.ACCESSIBLE.value: connectedAndAccessible += 1 elif result.requestingDomainStatus == DOMAIN_STATUS.RE_ACCESSIBLE.value: connectedAndReAccessible += 1 elif result.requestingDomainStatus == DOMAIN_STATUS.NOT_ACCESSIBLE.value: connectedButNotAccessible += 1 connectionSucceeded += 1 if result.connectionStatus == CONNECTION_STATUS.NOT_CONNECTED.value: connectedFailed += 1 data = '' with open(self.OUTPUT_FILE, 'r') as file: data = file.read() with open(self.OUTPUT_FILE, 'w+') as file: file.write(data) file.write("\n--------------------------\n") file.write("\n--------------------------\n") file.write('Finished in %0.2f seconds\n' % (self.time_taken)) file.write('Found ' + str(self.nodesCount) + ' Exit nodes:\n') file.write(' ' + str(connectionSucceeded) + ': were connected successfully\n') file.write(' ' + str(connectionSucceeded) + ': were connected successfully') file.write(' ' + str(connectedAndAccessible) + ': were connected and checked successfully.') file.write(' ' + str(connectedAndReAccessible) + ': were connected and re-checked successfully.') file.write(' ' + str(connectedButNotAccessible) + ': were connected successfully but checked failed.') file.write(' ' + str(connectionFailed) + ': failed ') file.write('\n--------------------------\n') file.write('Checking Success rate: ' + str(connectionSucceeded / self.nodesCount * 100) + '%') file.write('Checking Failed rate: ' + str(connectedButNotAccessible / self.nodesCount * 100) + '%') file.write('Failed Connections rate: ' + str(connectionFailed / self.nodesCount * 100) + '%') file.write( '\n***********************************================END===============***********************************\n' )
def writeInfoForSpicalIP(IP, requests, ID=None, PORT=None, DRAW=False, index=0): list = [] temp_Requests = [] txtFiles = glob.glob(FILE_PATH) if ID is not None and PORT is not None: filename = ('JSON/ByIP/IP_%s_ID_%s_PORT_%s.json' % (IP, ID, PORT)) for line in requests: if IP in line.srcIP and ID in line.requestId and PORT in line.srcPort: list.append(line) elif ID is not None: filename = ('JSON/ByID/IP_%s_ID_%s.json' % (IP, ID)) for line in requests: if IP in line.srcIP and ID in line.requestId: temp_Requests.append(line) list.append(line) elif PORT is not None: filename = ('JSON/ByPort/IP_%s_PORT_%s.json' % (IP, PORT)) for line in requests: if IP in line.srcIP and PORT in line.srcPort: temp_Requests.append(line) list.append(line) else: filename = ('JSON/ByIP/IP_%s.json' % IP) for line in requests: if IP == line.srcIP: temp_Requests.append(line) list.append(line) if list.__len__() > MINNUMBER_DrawGraph: print("JSON file are stored %s" % filename) with open(filename, 'w') as F: # Use the json dumps method to write the ExitNodelist to disk F.write(json.dumps(list, default=dumper)) print('Writing All Requests Info is done : %s' % str(list.__len__())) if DRAW is True: # TODO: add enum requestCount = temp_Requests.__len__() if requestCount > MINNUMBER_DrawGraph: drawGraph(temp_Requests, option=1, mode=1) # Request Ids drawGraph(temp_Requests, option=2, mode=1) # port Number else: Helper.printOnScreenAlways( '%d - Ignored: %s - Requests: %d' % (index, temp_Requests[0].srcIP, requestCount), MSG_TYPES.YELLOW)
def run(self): try: if self.opt == '-r': # check the connections self.requestDomainViaTor() elif self.opt == '-c': self.checkTorConnection(self.REQUIRED_NODES) elif self.opt == '-cd': # check the domain name connection self.checkWebsiteConnection(self.REQUIRED_NODES) elif self.opt == '-drc': # check the domain name connection self.countDNSRequest() except Exception as ex: Helper.printOnScreenAlways('TORConnector - run %s'%str(ex),MSG_TYPES.ERROR) sys.exit(2)
def checkWebsiteConnection(self, numberOfNodes=10000): start_time = time.time() nodesCount = 0 successfully_Connections = 0 successfully_Connections_Checking_Failed = 0 re_successfully_Connections = 0 re_successfully_Connections_Checking_Failed = 0 failed_Connections = 0 # load json_Objects = self.loadExitNodesFromJSON() result = 3 # assume that connection failed if stem.util.system.is_windows(): # Terminate the tor in case if it is still running TORFunctions.ProcesskillForWindows('tor.exe') print('\n') total_Nodes = len(json_Objects) number_Of_Nodes = int(numberOfNodes) nodesCount = 0 for obj in json_Objects: ip = str(obj['ExitNode']['Address'].encode("ascii"), 'utf-8') fingerprint = str(obj['ExitNode']['Fingerprint'].encode("ascii"), 'utf-8') nickname = str(obj['ExitNode']['Nickname'].encode("ascii"), 'utf-8') or_port = str(obj['ExitNode']['Or_port']) dir_port = str(obj['ExitNode']['Dir_port']) # total number of nodes if nodesCount >= number_Of_Nodes: break nodesCount = nodesCount + 1 result = self.connectToTORExitNode(fingerprint, ip, nodesCount, TASK_MODE.DNS_0x20_CHECKING) # check if the website is accessible / we use this method for check if the DNS support 0x20 coding for the domain name. exitNode = ExitNode(ipaddress=ip, fingerprint=fingerprint, nickname=nickname, or_port=or_port, dir_port=dir_port, status=result) self.ExitNodes_List.append(exitNode) self.Result_List.append(result) time_taken = time.time() - start_time finalResult = Results.FinalResult(self.Result_List, nodesCount, time_taken) curpath = os.path.dirname(__file__) os.chdir(curpath) newJSONPath = os.path.join(curpath,self.PROCESSED_NODES_PATH) Helper.storeExitNodesJSON(objects=self.ExitNodes_List, path=newJSONPath)
def makeDirectories(): ''' Make the directories in case they are missing ''' try: # TODO: Directories need more abtracte if not os.path.exists('GatheredFiles'): os.makedirs('GatheredFiles') os.makedirs('GatheredFiles/Logs') os.makedirs('GatheredFiles/JSON') else: if not os.path.exists('GatheredFiles/Logs'): os.makedirs('GatheredFiles/Logs') if not os.path.exists('GatheredFiles/JSON'): os.makedirs('GatheredFiles/JSON') except Exception as ex: Helper.printOnScreenAlways(ex, term.Color.RED)
def countDNSRequest(self): Helper.printOnScreenAlways('Requesting %s via TOR ' % self.DOMAIN_URL) start_time = time.time() nodesCount = 0 json_Objects = self.loadExitNodesFromJSON() if stem.util.system.is_windows(): # Terminate the tor in case if it is still running TORFunctions.ProcesskillForWindows('tor.exe') for obj in tqdm(json_Objects, ncols=80, desc='Requesting Domain via our DNS'): ip = str(obj['ExitNode']['Address'].encode("ascii"), 'utf-8') fingerprint = str(obj['ExitNode']['Fingerprint'].encode("ascii"), 'utf-8') nickname = str(obj['ExitNode']['Nickname'].encode("ascii"), 'utf-8') or_port = str(obj['ExitNode']['Or_port']) dir_port = str(obj['ExitNode']['Dir_port']) self.connectToTORExitNode(fingerprint, ip, nodesCount + 1, TASK_MODE.DNS_RESOLVER_COUNTER) time_taken = time.time() - start_time
def findAllDNSFiles(self, folder: object = 'Logs') -> object: RootPath = '\\'.join( os.path.dirname(os.path.abspath(__file__)).split('\\')[:-1]) newDestinationPath = os.path.join(RootPath, self.DESTINATION_PATH) # find all the logs folders for DNS files = [ os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser(self.SOURCE_PATH)) for f in fn ] if folder == 'Logs': newDestinationPath = os.path.join(newDestinationPath, "Logs") # find all the logs files for DNS filteredLogFiles = [ filePath for filePath in files if "logs" in filePath.lower() and "error" not in filePath.lower() and "DNS" in filePath ] elif folder == 'JSON': # TODO: need some more work newDestinationPath = os.path.join(newDestinationPath, "JSON") # find all the logs files for DNS filteredLogFiles = [ filePath for filePath in files if "JSON" in filePath.lower() and "error" not in filePath. lower() and "DNS" in filePath and "Checking" in filePath ] count = 0 for filePath in filteredLogFiles: try: shutil.move(filePath, newDestinationPath) count += 1 except shutil.Error as e: pass # os.rename(filePath, ('v%s _' % index) + filePath) # shutil.move(filePath, newDestinationPath) Helper.printOnScreenAlways('%d files has been moved.' % count, MSG_TYPES.RESULT)
def connect(self, index): Helper.printOnScreen( (term.format("\n\n%d- Starting Tor, connecting to: %s", term.Attr.BOLD) % (index, self.exitNodeIp)), mode=self.mode) Helper.printOnScreen('Fingerprint: ' + self.exitNodeFingerprint, MSG_TYPES.RESULT, mode=self.mode) self.wirteIntoFile('\n%d- Starting Tor, connecting to: %s' % (index, self.exitNodeIp)) self.wirteIntoFile('Fingerprint: ' + self.exitNodeFingerprint) try: self.tor_process = stem.process.launch_tor_with_config( timeout=self.torConnectionTimeouT, completion_percent=100, config={ 'SocksPort': str(self.socksPort), 'ExitNodes': '$' + self.exitNodeFingerprint, 'ControlPort': str(self.controlPort), }, ) Helper.printOnScreen('Connected, Checking...', color=MSG_TYPES.YELLOW, mode=self.mode) self.wirteIntoFile('Connected, Checking...') return Result(CONNECTION_STATUS.CONNECTED, DOMAIN_STATUS.STATELESS) except Exception as ex: Helper.printOnScreen(('Connection - connect: ' + str(ex)), color=MSG_TYPES.ERROR, mode=self.mode) Helper.printOnScreen('Connection failed! - Timed out', color=MSG_TYPES.ERROR, mode=self.mode) self.wirteIntoFile('Connection failed! - Timed out') return Result(CONNECTION_STATUS.NOT_CONNECTED, DOMAIN_STATUS.STATELESS)
def query(self, url): # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=849845;msg=127 # https://stackoverflow.com/questions/29876778/tor-tutorial-speaking-to-russia-stuck-at-45-50 # Uses pycurl to fetch a site using the proxy on the SOCKS_PORT. output = io.BytesIO() query = pycurl.Curl() query.getinfo(pycurl.PRIMARY_IP) query.setopt(pycurl.CAINFO, certifi.where()) query.setopt(pycurl.URL, url) query.setopt(pycurl.VERBOSE, False) query.setopt(pycurl.TIMEOUT, self.pycurlTimeout) query.setopt(pycurl.PROXY, '127.0.0.1') query.setopt(pycurl.PROXYPORT, self.socksPort) query.setopt(pycurl.IPRESOLVE, pycurl.IPRESOLVE_V4) query.setopt( pycurl.USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0' ) query.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5_HOSTNAME) query.setopt(pycurl.WRITEFUNCTION, output.write) try: query.perform() temp = output.getvalue() return str(temp.decode('utf-8')).strip() except Exception as ex: Helper.printOnScreen(("Unable to reach %s (%s)" % (url, ex)), MSG_TYPES.ERROR, mode=self.mode) return b'unreachable' except pycurl.error as exc: Helper.printOnScreen(("Unable to reach %s (%s)" % (url, exc)), MSG_TYPES.ERROR, mode=self.mode) return b'unreachable'
def sendRequestsWithResponseMode(self, runManytimesMode, requestTimes, responseMode=True): try: Helper.printOnScreen( (term.format("Requesting our webiste:\n", term.Attr.BOLD)), color=MSG_TYPES.RESULT, mode=self.mode) domain = str(self.exitNodeIp).replace('.', '-') + '.' + self.domainUrl url = 'http://' + domain # requesting many times/ testing the same node 100 times./ testing how random is the PORT number and Requset ID self.requestDomain( domain=domain, times=requestTimes, responseMode=responseMode, addtionname=self.forceNotResponseMsg, ) self.killConnection() except Exception as ex: self.killConnection() TORFunctions.loggingError('Connection - sendRequests: %s' % traceback.format_exc())
def Normalize(self, show='yes'): count = 0 DNSouterList = [] for obj in self.DNSObj: # get all the dns ip wittout repetation innerList = [] dnsIP = self.DNSObj[obj]['Request']['SrcIP'] dnsDomainfull = self.DNSObj[obj]['Request']['Domain'] dnsDomainfull = [x.strip() for x in dnsDomainfull.split('.') ][0] # remove the domain: dnstestsuite.space temp = '' dnsExitnodeIP = [x.strip() for x in dnsDomainfull.split('_') ][-1:][0] # get the ip of the exitnode if (dnsExitnodeIP.__contains__('-')): if dnsIP not in DNSouterList: DNSouterList.append(dnsIP) DNSouterList = set(DNSouterList) DNSList = [] for obj in DNSouterList: node = DNSResolverInfo(obj) DNSList.append(node) for DnsNodeObj in DNSList: tempNodeList = [] count = 1 + count for Dns in self.DNSObj: dnsIP = self.DNSObj[Dns]['Request']['SrcIP'] if dnsIP == DnsNodeObj.DNSIP: nodeDomainfull = self.DNSObj[Dns]['Request']['Domain'] nodeModifiedDomainfull = self.DNSObj[Dns]['Request'][ 'modifiedDomain'] nodeDomain = [ x.strip() for x in nodeDomainfull.split('.') ][0] # remove the domain: dnstestsuite.space dnsExitnodeIP = [x.strip() for x in nodeDomain.split('_') ][-1:][0] # get the ip of the exitnode if (dnsExitnodeIP.__contains__('-')): dnsExitnodeIP = dnsExitnodeIP.replace("-", ".") if dnsExitnodeIP not in tempNodeList: tempNodeList.append(dnsExitnodeIP) exitnode = DNSExitNode(dnsExitnodeIP, nodeDomain, nodeModifiedDomainfull) DnsNodeObj.insertNode(exitnode) DnsNodeObj.nodeCount += 1 if show == 'yes': for DnsNodeObj in DNSList: index = 0 Helper.printOnScreenAlways( 'DNS Resolver IP: %s - Exitnode: %d ' % (DnsNodeObj.DNSIP, DnsNodeObj.nodeCount), MSG_TYPES.RESULT) for node in DnsNodeObj.ExitNodelist: index += 1 Helper.printOnScreenAlways( " %d - %s " % (index, node.exitNodeIP), MSG_TYPES.YELLOW) print() Helper.printOnScreenAlways("We found %d DNS Resolvers " % count, MSG_TYPES.RESULT) curpath = os.path.dirname(__file__) os.chdir(curpath) newJSONPath = os.path.join(curpath, PROCESSED_DNSDATA_PATH) Helper.storeDNSResolverData(objects=DNSList, path=newJSONPath) return DNSList
def drawGraph(objects, option, mode=0, graphType=GRAPHS.ALL): ''' Plot Request options :Id 1/ Port Nnumber 2 ||| mode: 0:normal / 1: one DNS ''' list = [] graphName = '' graphTitle = '' requestCount = objects.__len__() storePathHistogram = '' storePathScatter = '' if requestCount > MINNUMBER_DrawGraph: if (option == 1): # ID graph for i in objects: list.append(int(i.requestId)) graphName = 'Request Id' graphTitle = 'Request IDs Distribution - Requests: ' + str( requestCount) elif (option == 2): # Port number graph for i in objects: list.append(int(i.srcPort)) graphName = 'Port Numbers' graphTitle = 'Port Number Distribution - Requests: ' + str( requestCount) elif (option == 3): for i in objects: list.append(i.Mix) graphName = 'Mix Numbers' graphTitle = ' Distribution' elif (option == 4): for i in objects: list.append(i.Min) graphName = 'Mix Numbers' graphTitle = ' Distribution' try: srcIP = objects[0].srcIP if mode == 1: # draw grphs for every DNS IP graphTitle += ' - IP: ' + srcIP #+ ' Requests: ' + str(requestCount) if (option == 1): storePathHistogram = "Graphs/DNS_Graphs/ByID/H_%s.png" % ( graphName + '_' + srcIP) storePathScatter = "Graphs/DNS_Graphs/ByID/S_%s.png" % ( graphName + '_' + srcIP) elif (option == 2): storePathHistogram = "Graphs/DNS_Graphs/ByPort/H_%s.png" % ( graphName + '_' + srcIP) storePathScatter = "Graphs/DNS_Graphs/ByPort/S_%s.png" % ( graphName + '_' + srcIP) else: storePathHistogram = "Graphs/H_%s.png" % (graphName) storePathScatter = "Graphs/S_%s.png" % (graphName) list.sort() if graphType == GRAPHS.ALL: plt.hist(list, bins=10, rwidth=0.9) plt.xlim([-500, 70000]) # fix the x axis plt.xlabel(graphName) plt.ylabel("Frequency") plt.title(graphTitle) if os.path.isfile(storePathHistogram): os.remove( storePathHistogram) # Opt.: os.system("rm "+strFile) plt.savefig(storePathHistogram) Helper.printOnScreenAlways(' H_%s Saved' % srcIP, MSG_TYPES.RESULT) plt.clf() unique_List = Counter(list) set(unique_List) x = [] y = [] markersize = 1 if requestCount > 1500: for i in unique_List: newVal = int(i) newValFeq = random.uniform(-0.5, 0.9) + float( unique_List[i] ) # add some noise to help to read the graph x.append(newVal) y.append(newValFeq) plt.plot(x, y, linestyle='', marker='o', markersize=0.7) else: for i in unique_List: newVal = int(i) newValFeq = float( unique_List[i] ) # add some noise to help to read the graph x.append(newVal) y.append(newValFeq) plt.plot(x, y, linestyle='', marker='o', markersize=2) if os.path.isfile(storePathScatter): os.remove( storePathScatter) # Opt.: os.system("rm "+strFile) plt.xlim([-500, 70000]) # fix the x axis plt.xlabel(graphName) plt.ylabel("Frequency") plt.title(graphTitle) plt.savefig(storePathScatter) Helper.printOnScreenAlways(' S_%s Saved' % srcIP, MSG_TYPES.RESULT) plt.clf() elif graphType == GRAPHS.HISTOGRAM: plt.hist(list, bins=10, rwidth=0.9) plt.xlim([-500, 70000]) # fix the x axis plt.xlabel(graphName) plt.ylabel("Frequency") if os.path.isfile(storePathHistogram): os.remove( storePathHistogram) # Opt.: os.system("rm "+strFile) plt.savefig(storePathHistogram) Helper.printOnScreenAlways(' H_%s Saved' % srcIP, MSG_TYPES.RESULT) plt.clf() elif graphType == GRAPHS.SCATTER: unique_List = Counter(list) set(unique_List) x = [] y = [] markersize = 1 if requestCount > 1500: for i in unique_List: newVal = int(i) newValFeq = random.uniform(-0.5, 0.9) + float( unique_List[i] ) # add some noise to help to read the graph x.append(newVal) y.append(newValFeq) plt.plot(x, y, linestyle='', marker='o', markersize=0.7) else: for i in unique_List: newVal = int(i) newValFeq = float( unique_List[i] ) # add some noise to help to read the graph x.append(newVal) y.append(newValFeq) plt.plot(x, y, linestyle='', marker='o', markersize=2) plt.xlim([-500, 70000]) # fix the x axis plt.xlabel(graphName) plt.ylabel("Frequency") plt.title(graphTitle) if os.path.isfile(storePathScatter): print('found %s' % storePathScatter) os.remove( storePathScatter) # Opt.: os.system("rm "+strFile) plt.savefig(storePathScatter) Helper.printOnScreenAlways(' H_%s Saved' % srcIP, MSG_TYPES.RESULT) plt.clf() except Exception as ex: print('In drawGraph' + str(ex))
def printCheckedResult(self): ''' This function print the result of checking if the DNS support 0x20 bit encoding(Capitalization) ''' connectionFailed = 0 connectionSucceeded = 0 connectedAndAccessible = 0 connectedAndReAccessible = 0 connectedButNotAccessible = 0 connectedFailed = 0 print("\n--------------------------") Helper.printOnScreenAlways('Finished in %0.2f seconds' % (self.timetaken)) Helper.printOnScreenAlways( 'Found ' + str(self.nodesCount) + ' Exit nodes', MSG_TYPES.RESULT) for result in self.resultList: if result.connectionStatus == CONNECTION_STATUS.CONNECTED.value: if result.requestingDomainStatus == DOMAIN_STATUS.ACCESSIBLE.value: connectedAndAccessible += 1 elif result.requestingDomainStatus == DOMAIN_STATUS.RE_ACCESSIBLE.value: connectedAndReAccessible += 1 elif result.requestingDomainStatus == DOMAIN_STATUS.NOT_ACCESSIBLE.value: connectedButNotAccessible += 1 connectionSucceeded += 1 if result.connectionStatus == CONNECTION_STATUS.NOT_CONNECTED.value: connectedFailed += 1 Helper.printOnScreenAlways( str(connectionSucceeded) + ': were connected successfully', MSG_TYPES.RESULT) Helper.printOnScreenAlways( ' ' + str(connectedAndAccessible) + ': were connected and checked successfully.', MSG_TYPES.RESULT) Helper.printOnScreenAlways( ' ' + str(connectedAndReAccessible) + ': were connected and re-checked successfully.', MSG_TYPES.RESULT) Helper.printOnScreenAlways( ' ' + str(connectedButNotAccessible) + ': were connected successfully but checked failed.', MSG_TYPES.RESULT) Helper.printOnScreenAlways( str(connectionFailed) + ': failed ', MSG_TYPES.RESULT) Helper.printOnScreenAlways("\n--------------------------") Helper.printOnScreenAlways( 'Checking Success rate: ' + str(connectionSucceeded / self.nodesCount * 100) + '%', MSG_TYPES.RESULT) Helper.printOnScreenAlways( 'Checking Failed rate: ' + str(connectedButNotAccessible / self.nodesCount * 100) + '%', MSG_TYPES.RESULT) Helper.printOnScreenAlways( 'Failed Connections rate: ' + str(connectionFailed / self.nodesCount * 100) + '%', MSG_TYPES.RESULT) Helper.printOnScreenAlways( '\n***********************************================END===============***********************************\n', MSG_TYPES.RESULT)
def main(argv): mode = '-none' required_Nodes = 0 printLogo() makeDirectories() if argv[1:] != []: # on the server try: required_Nodes = -1 opt = argv[1] error = False if len(argv) == 3: # mode printing if argv[2] == MODE_TYPES.printing.value or argv[ 2] == MODE_TYPES.none.value: mode = argv[2] elif len(argv) == 5: if argv[2] == '-n': # stop after certain nodes number required_Nodes = argv[3] else: error = True if argv[4] == MODE_TYPES.printing.value or argv[ 4] == MODE_TYPES.none.value: mode = argv[4] else: error = True if error is True: Helper.printOnScreen('WRONG ......', color=MSG_TYPES.ERROR) sys.exit(2) else: Helper.printOnScreen('WRONG Too Many arguments.', color=MSG_TYPES.ERROR) ###--------------------------------------- try: Helper.printOnScreenAlways("Gathering Info ... ", MSG_TYPES.RESULT) nodes = NodesHandler.NodesHandler(mode=mode) nodes_Number = nodes.run() Helper.printOnScreenAlways( ("DONE, %s nodes have been gathered" % str(nodes_Number)), MSG_TYPES.RESULT) except Exception as ex: Helper.printOnScreenAlways( ("Exit nodes are not gathered.. :(, ERROR : %s" % str(ex)), MSG_TYPES.ERROR) sys.exit() if opt == '-r' or opt == '-c' or opt == '-cd' or opt == '-drc': # check the connections if (int(required_Nodes) > 0): con = TORConnector.TORConnections(opt, mode, required_Nodes) con.run() else: con = TORConnector.TORConnections(opt, mode) con.run() except Exception as ex: print(ex) sys.exit()
def checkDNSFor0x20Encoding(self): ''' Check for 0x20 bit encoding ''' try: randNumber = random.randint(1, 10000) # to avoid cashing domain = (str(self.exitNodeIp).replace('.', '-') + '.' + self.domainUrlCheck).strip() subDomain = '%d_check_%s' % ( randNumber, domain ) # 768_check_12.23.243.12.dnstestsuite.space/check url = 'http://' + subDomain message = self.domainCorrectMessageResult result_message = 'none' try: result_message = self.query(url) except: result_message = 'unreachable' if message == result_message: # matches Helper.printOnScreen( ('Connected Successfully to : %s' % subDomain), color=MSG_TYPES.RESULT, mode=self.mode) self.wirteIntoFile('Connected Successfully to : %s' % subDomain) result = Result(CONNECTION_STATUS.CONNECTED, DOMAIN_STATUS.ACCESSIBLE) else: Helper.printOnScreen(('Checking Failed : %s' % subDomain), color=MSG_TYPES.ERROR, mode=self.mode) self.wirteIntoFile('Checking Failed : %s' % subDomain) # re-checking Helper.printOnScreenAlways('re-Checking...', color=MSG_TYPES.ANY) subDomain = '%d_re_check_%s' % ( randNumber, domain ) # 12321_re_check_12.23.243.12.dnstestsuite.space/check url = 'http://' + subDomain result_message = 'none' try: result_message = self.query(url) except: result_message = 'unreachable' if message == result_message: Helper.printOnScreen( ('re-Checking Successful : %s' % subDomain), color=MSG_TYPES.RESULT, mode=self.mode) self.wirteIntoFile('re-Checking Successful : %s' % subDomain) result = Result(CONNECTION_STATUS.CONNECTED, DOMAIN_STATUS.RE_ACCESSIBLE) else: Helper.printOnScreen( ('re-Checking Failed : %s' % subDomain), color=MSG_TYPES.ERROR, mode=self.mode) self.wirteIntoFile('re-Checking Failed : %s' % subDomain) result = Result(CONNECTION_STATUS.CONNECTED, DOMAIN_STATUS.NOT_ACCESSIBLE) self.killConnection() return result except Exception as ex: TORFunctions.loggingError( 'Connection - checkDNSFor0x20Encoding: %s' % traceback.format_exc()) self.killConnection() return Result(CONNECTION_STATUS.CONNECTED, DOMAIN_STATUS.NOT_ACCESSIBLE)