def create_task_generator(task_type, ip_from, ip_to): if task_type == 'tcp': for int32_ip in range(ip_from, ip_to): str_ip = utils.int2ip(int32_ip) for port in range(1, 65535 + 1): yield utils.is_port_open, str_ip, port else: for int32_ip in range(ip_from, ip_to + 1): yield utils.ping_host, utils.int2ip(int32_ip)
def __forwardTableReceived(self, data): self.forwardTableLock.acquire() for i in range(0, len(data), 12): destIp, destPort, nextHopIp, nextHopPort = struct.unpack( "!IHIH", data[i:i + 12]) destIp = utils.int2ip(destIp) nextHopIp = utils.int2ip(nextHopIp) self.forwardTable.update({ (destIp, destPort): (nextHopIp, nextHopPort) }) self.forwardTableLock.release()
def get(self): ip = self.get_argument('ip_addr', None) if not ip: return self.response('error', 'please input an IPv4 address') try: ip = utils.ip2int(ip) except: return self.response('error', 'please input a valid IPv4 address') if not (0 <= ip <= 4294967295): return self.response('error', 'please input a valid IPv4 address') response = None resolver = self.get_argument('resolver', 'mem') start_addr = memback.find_ip(ip) if not start_addr: return self.response('error', 'could not find address') if resolver == 'postgres': cursor = self.pgconn.cursor() #cursor.execute('select x, y, country, county, city from addresses where x <= %s and y >= %s', (ip, ip)) cursor.execute('select x, y, country, county, city from addresses where x = %s', (start_addr, )) records = cursor.fetchall() if records: record = records[0] response = {'range': (utils.int2ip(record[0]), utils.int2ip(record[1])), 'country': record[2], 'county': record[3], 'city': record[4] } cursor.close() elif resolver == 'mongo': #records = self.mgconn.ipdb.addresses.find({'x': {'$lte': ip}, 'y': {'$gte': ip}}) records = self.mgconn.ipdb.addresses.find({'x': start_addr}) if records: record = records[0] response = {'range': (utils.int2ip(record['x']), utils.int2ip(record['y'])), 'country': record['country'], 'county': record['county'], 'city': record['city'] } records.close() else: #details = self.rdconn.get(start_addr) #response = utils.sh2rec(start_addr, json.loads(details)) response = utils.sh2rec(start_addr, memback.get_address_details(start_addr)) if response: return self.response(data=response) self.response('error', 'could not find address')
def __handleData(self, data): (command, ip, port) = struct.unpack("!BIH", data[0:7]) sourceAddress = (utils.int2ip(ip), port) if command == 0: (ip, port) = struct.unpack("!IH", data[7:13]) destAddress = (utils.int2ip(ip), port) if destAddress != self.address: self.__sendPacket(data, destAddress) return self.__normalPacketReceived(sourceAddress, data[13:]) elif command == 1: self.__helloReceived(sourceAddress, data[7:]) elif command == 3: self.__forwardTableReceived(data[1:])
def __LSUReceived(self, sourceAddress, data): sourceAddressNeighbour = {} for i in range(0, len(data), 8): (ip, port, metric) = struct.unpack("!IHH", data[i:i + 8]) address = (utils.int2ip(ip), port) sourceAddressNeighbour.update({address: metric}) self.__updateVector(sourceAddress, sourceAddressNeighbour)
def get_valid(limit=1000, anonymous=True, age_secs=3600, protocol=None): result = [] max_age = datetime.utcnow() - timedelta(seconds=age_secs) conditions = ((Proxy.scan_date > max_age) & (Proxy.fail_count == 0) & (Proxy.niantic == ProxyStatus.OK) & (Proxy.ptc_login == ProxyStatus.OK) & (Proxy.ptc_signup == ProxyStatus.OK)) if anonymous: conditions &= (Proxy.anonymous == ProxyStatus.OK) if protocol is not None: conditions &= (Proxy.protocol == protocol) try: query = (Proxy .select() .where(conditions) .order_by(Proxy.latency.asc()) .limit(limit) .dicts()) for proxy in query: proxy['ip'] = int2ip(proxy['ip']) proxy['url'] = Proxy.url_format(proxy) result.append(proxy) except OperationalError as e: log.exception('Failed to get valid proxies from database: %s', e) return result
def responde(self,remetente,dados): data = struct.unpack('>BIII',dados[:13]) novo_sucessor = Nodo(nid=data[2],bip=data[3]) self.node.sucessor = novo_sucessor print "UPDATE respondido: %s é novo sucessor de %s!" % (novo_sucessor.nid, self.node.nid) self.message = ['I',self.node.nid] self.envia_resposta(int2ip(data[3]))
def get_scan(limit=1000, exclude=[], age_secs=3600, protocol=None): result = [] min_age = datetime.utcnow() - timedelta(seconds=age_secs) conditions = (((Proxy.scan_date < min_age) & (Proxy.fail_count < 5)) | Proxy.scan_date.is_null()) if exclude: conditions &= (Proxy.hash.not_in(exclude)) if protocol is not None: conditions &= (Proxy.protocol == protocol) try: query = (Proxy .select() .where(conditions) .order_by(Proxy.scan_date.asc(), Proxy.insert_date.asc()) .limit(limit) .dicts()) for proxy in query: proxy['ip'] = int2ip(proxy['ip']) proxy['url'] = Proxy.url_format(proxy) result.append(proxy) except OperationalError as e: log.exception('Failed to get proxies to scan from database: %s', e) return result
def datagramReceived(self, data, addr): # print(data) command, = struct.unpack("!B", data[0:1]) if command == 2: # LS (ip, port) = struct.unpack("!IH", data[1:7]) client = (utils.int2ip(ip), port) self.__LSReceived(client, data[7:])
def __tracerouteReceived(self, packet): (ip, port) = struct.unpack("!IH", packet[1:7]) sourceAddress = (utils.int2ip(ip), port) (ip, port, count) = struct.unpack("!IHB", packet[7:14]) destAddress = (utils.int2ip(ip), port) count = count - 1 if count > 0 and destAddress != self.address: packet = packet[0:-1] + struct.pack("!B", count) self.__sendPacket(packet, destAddress) elif count == 0: # send Echo packet EchoPacket = struct.pack("!BIHIHIH", 4, utils.ip2int(sourceAddress[0]), sourceAddress[1], utils.ip2int( destAddress[0]), destAddress[1], utils.ip2int( self.address[0]), self.address[1]) self.__sendPacket(EchoPacket, sourceAddress)
def __EchoReceived(self, packet): (ip, port) = struct.unpack("!IH", packet[7:13]) destAddress = (utils.int2ip(ip), port) (ip, port) = struct.unpack("!IH", packet[13:19]) pathAddress = (utils.int2ip(ip), port) if destAddress not in self.path: self.path[destAddress] = [pathAddress, ] else: self.path[destAddress].append(pathAddress) if pathAddress == destAddress: # print the path print("(%s:%d)" % (self.address[0], self.address[1]), end='') for item in self.path[destAddress]: print("->(%s:%d)" % (item[0], item[1]), end='') print("") self.path.pop(destAddress) self.traceRouteList.append(destAddress) self.traceRouteResult[destAddress] = 1
def __LSReceived(self, client, data): neighbourVector = {} for i in range(0, len(data), 8): (ip, port, metric) = struct.unpack("!IHH", data[i:i + 8]) destAddress = (utils.int2ip(ip), port) neighbourVector[destAddress] = metric self.__updateVector(client, neighbourVector) if client in self.callLaterHandles: if self.callLaterHandles[client].active(): self.callLaterHandles[client].cancel() self.callLaterHandles[client] = self.reactor.callLater( self.DEADINTERVAL, self.__removeClient, args=[client])
def __init__(self, name, **data): ip_range = data['range'] self.ip1, ip2_sz = ip_range.split('-') self.ip2, self.sz = ip2_sz.split('/') self.ip1 = self.ip1.strip() self.ip2 = self.ip2.strip() self.sz = int(self.sz) self.url = data.get('url', 'qemu:///system') self.name = name self.ip = int2ip(ip2int(self.ip1) + 1) self.bridge = data['bridge'].strip() self.netmask = netsz2netmask(self.sz)
def __handleData(self, data): (command, ip, port) = struct.unpack("!BIH", data[0:7]) sourceAddress = (utils.int2ip(ip), port) if command == 0: (ip, port) = struct.unpack("!IH", data[7:13]) destAddress = (utils.int2ip(ip), port) if destAddress != self.address: m = hashlib.md5() m.update(data) md5 = m.hexdigest() md5Lock.acquire() if md5 not in self.forwardPacket: self.__sendPacket(data, destAddress) if len(self.forwardPacket) < 10: self.forwardPacket.append(md5) else: self.forwardPacket[self.forwardPacketIndex] = md5 self.forwardPacketIndex = ( self.forwardPacketIndex + 1) % 10 md5Lock.release() return elif destAddress == self.address: self.__normalPacketReceived(sourceAddress, data[13:]) elif command == 1: self.__helloReceived(sourceAddress, data[7:]) elif command == 2: self.__LSUReceived(sourceAddress, data[13:]) (ip, port) = struct.unpack("!IH", data[7:13]) transmitAddress = (utils.int2ip(ip), port) self.__broadcastReceived(transmitAddress, sourceAddress, data) elif command == 3: self.__tracerouteReceived(data) elif command == 4: if sourceAddress != self.address: self.__sendPacket(data, sourceAddress) return else: self.__EchoReceived(data)
def generate_hash(proxy): # Check if proxy is already formatted for database. if isinstance(proxy['ip'], (int, long)): ip = int2ip(proxy['ip']) port = str(proxy['port']) else: ip = proxy['ip'] port = proxy['port'] hasher = hashlib.md5() hasher.update(ip) hasher.update(port) if proxy['username']: hasher.update(proxy['username']) if proxy['password']: hasher.update(proxy['password']) # 4 bit * 8 hex chars = 32 bit = 4 bytes return int(hasher.hexdigest()[:8], 16)
def prepare_guest_debian(disk_path, hostname, passwords, eth_devs, format=None, apt_proxy_ip=None): logger.info("Prepare image for " + hostname) if format == 'lxc': gfs = LocalGuestFS(disk_path) gfs.rm('/etc/init/udev.conf') interfaces = [] for dev, (hw, ip, sz, gw) in eth_devs.items(): if ip == 'dhcp': interfaces.append("dhclient {0}".format(dev)) else: interfaces.append("ifconfig {0} {1}/{2} up".format(dev, ip, sz)) gfs.write('/etc/init/lxc_lan.conf', ifconfig_script.format("\n".join(interfaces))) else: gfs = guestfs.GuestFS() gfs.add_drive_opts(disk_path, format=format) logger.debug("Launch libguestfs vm") gfs.launch() logger.debug("ok") os_devs = gfs.inspect_os() if len(os_devs) > 1: msg = "Two or more bootable partitions - disk prepare impossible " + disk_path logger.error(msg) raise CloudError(msg) # for dev, fs_type in gfs.list_filesystems(): # logger.debug("Fount partition {0} with fs type {1}".format(dev, fs_type)) # # TODO: add lvm support # if fs_type in 'ext2 ext3 reiserfs3 reiserfs4 xfs jfs btrfs': # gfs.mount(dev, '/') # if gfs.exists('/etc'): # logger.debug("Fount /etc on partition {0} - will work on it".format(dev)) # break # gfs.umount(dev) # logger.debug("No /etc dir found - continue") if 0 == len(os_devs): mounts = sorted(gfs.inspect_get_mountpoints(os_devs[0])) for mpoint, dev in mounts: gfs.mount(dev, mpoint) if not gfs.exists('/etc'): msg = "Can't fount /etc dir in image " + disk_path logger.error(msg) raise CloudError(msg) else: gfs.mount(os_devs[0], '/') #gfs.mount('/dev/vda1', '/') if not gfs.exists('/etc'): msg = "Can't fount /etc dir in image " + disk_path logger.error(msg) raise CloudError(msg) logger.debug("Launch ok. Set hostname") #hostname gfs.write('/etc/hostname', hostname) #set device names logger.debug("Set device names and network imterfaces") templ = 'SUBSYSTEM=="net", DRIVERS=="?*", ATTR{{address}}=="{hw}", NAME="{name}"' rules_fc = [] interfaces = ["auto lo\niface lo inet loopback"] for dev, (hw, ip, sz, gw) in eth_devs.items(): rules_fc.append(templ.format(hw=hw, name=dev)) interfaces.append("auto " + dev) if ip == 'dhcp': interfaces.append("iface {0} inet dhcp".format(dev)) else: interfaces.append("iface {0} inet static".format(dev)) interfaces.append(" address " + ip) network = int2ip(ip2int(ip) & ip2int(netsz2netmask(sz))) interfaces.append(" network " + network) interfaces.append(" netmask " + netsz2netmask(sz)) gfs.write('/etc/udev/rules.d/70-persistent-net.rules', "\n".join(rules_fc)) # gfs.write('/etc/network/interfaces', "\n".join(interfaces)) gfs.write('/etc/network/interfaces.d/eth0', "\n".join(interfaces)) # update passwords logger.debug("Update passwords") chars = "".join(chr(i) for i in range(ord('a'), ord('z') + 1)) chars += "".join(chr(i) for i in range(ord('A'), ord('Z') + 1)) chars += "".join(chr(i) for i in range(ord('0'), ord('9') + 1)) hashes = {} for login, passwd in passwords.items(): salt = "".join(random.choice(chars) for _ in range(8)) hashes[login] = crypt.crypt(passwd, "$6$" + salt) new_shadow = [] need_logins = set(hashes) for ln in gfs.read_file('/etc/shadow').split('\n'): ln = ln.strip() if ln != '' and ln[0] != '#': login = ln.split(':', 1)[0] if login in hashes: sh_templ = "{login}:{hash}:{rest}" sh_line = sh_templ.format(login=login, hash=hashes[login], rest=ln.split(':', 2)[2]) new_shadow.append(sh_line) need_logins.remove(login) else: new_shadow.append(ln) for login in need_logins: new_sh_templ = "{login}:{hash}:{rest}" new_sh_line = new_sh_templ.format(login=login, hash=hashes[login], rest="0:0:99999:7:::") new_shadow.append(new_sh_line) gfs.write('/etc/shadow', "\n".join(new_shadow)) # add new users to passwd ids = [] logins = [] passwd = gfs.read_file('/etc/passwd') for ln in passwd.split('\n'): ln = ln.strip() if ln != '' and ln[0] != '#': logins.append(ln.split(':', 1)[0]) ids.append(ln.split(':')[2]) ids.append(ln.split(':')[3]) add_lines = [] try: mid = max(i for i in ids if i < 65000) except ValueError: mid = 0 mid += 1024 for login in set(hashes) - set(logins): home = '/home/' + login add_lines.append(":".join([login, 'x', str(mid), str(mid), "", home, '/bin/bash'])) if not gfs.exists(home): gfs.mkdir_p(home) mid += 1 if add_lines != []: gfs.write('/etc/passwd', passwd.rstrip() + "\n" + "\n".join(add_lines)) # if apt_proxy_ip is not None: # logger.debug("Set apt-proxy to http://{0}:3142".format(apt_proxy_ip)) # fc = 'Acquire::http {{ Proxy "http://{0}:3142"; }};'.format(apt_proxy_ip) # gfs.write('/etc/apt/apt.conf.d/02proxy', fc) logger.debug("Update hosts") hosts = gfs.read_file('/etc/hosts') new_hosts = ["127.0.0.1 localhost\n127.0.0.1 " + hostname] for ln in hosts.split('#'): if not ln.strip().startswith('127.0.0.1'): new_hosts.append(ln) gfs.write('/etc/hosts', "\n".join(new_hosts)) # allow ssh passwd auth if gfs.is_file('/etc/ssh/ssh_config'): name = '/etc/ssh/ssh_config' elif gfs.is_file('/etc/ssh/sshd_config'): name = '/etc/ssh/sshd_config' else: logger.warning("Both '/etc/ssh/sshd_config' and '/etc/ssh/ssh_config' are absent. Skip ssh config patching") name = None if name is not None: sshd_conf = gfs.read_file('/etc/ssh/ssh_config') sshd_conf_lines = sshd_conf.split("\n") for pos, ln in enumerate(sshd_conf_lines): if "PasswordAuthentication" in ln: sshd_conf_lines[pos] = "PasswordAuthentication yes" break else: sshd_conf_lines.append("PasswordAuthentication yes") gfs.write('/etc/ssh/ssh_config', "\n".join(sshd_conf_lines))
def setBytesIP(self,bip): self.bip = bip self.ip = int2ip(self.bip)
def prepare_guest_debian(disk_path, hostname, passwords, eth_devs, format=None, apt_proxy_ip=None): logger.info("Prepare image for " + hostname) if format == 'lxc': gfs = LocalGuestFS(disk_path) gfs.rm('/etc/init/udev.conf') interfaces = [] for dev, (hw, ip, sz, gw) in eth_devs.items(): if ip == 'dhcp': interfaces.append("dhclient {0}".format(dev)) else: interfaces.append("ifconfig {0} {1}/{2} up".format( dev, ip, sz)) gfs.write('/etc/init/lxc_lan.conf', ifconfig_script.format("\n".join(interfaces))) else: gfs = guestfs.GuestFS() gfs.add_drive_opts(disk_path, format=format) logger.debug("Launch libguestfs vm") gfs.launch() logger.debug("ok") os_devs = gfs.inspect_os() if len(os_devs) > 1: msg = "Two or more bootable partitions - disk prepare impossible " + disk_path logger.error(msg) raise CloudError(msg) # for dev, fs_type in gfs.list_filesystems(): # logger.debug("Fount partition {0} with fs type {1}".format(dev, fs_type)) # # TODO: add lvm support # if fs_type in 'ext2 ext3 reiserfs3 reiserfs4 xfs jfs btrfs': # gfs.mount(dev, '/') # if gfs.exists('/etc'): # logger.debug("Fount /etc on partition {0} - will work on it".format(dev)) # break # gfs.umount(dev) # logger.debug("No /etc dir found - continue") if 0 == len(os_devs): mounts = sorted(gfs.inspect_get_mountpoints(os_devs[0])) for mpoint, dev in mounts: gfs.mount(dev, mpoint) if not gfs.exists('/etc'): msg = "Can't fount /etc dir in image " + disk_path logger.error(msg) raise CloudError(msg) else: gfs.mount(os_devs[0], '/') #gfs.mount('/dev/vda1', '/') if not gfs.exists('/etc'): msg = "Can't fount /etc dir in image " + disk_path logger.error(msg) raise CloudError(msg) logger.debug("Launch ok. Set hostname") #hostname gfs.write('/etc/hostname', hostname) #set device names logger.debug("Set device names and network imterfaces") templ = 'SUBSYSTEM=="net", DRIVERS=="?*", ATTR{{address}}=="{hw}", NAME="{name}"' rules_fc = [] interfaces = ["auto lo\niface lo inet loopback"] for dev, (hw, ip, sz, gw) in eth_devs.items(): rules_fc.append(templ.format(hw=hw, name=dev)) interfaces.append("auto " + dev) if ip == 'dhcp': interfaces.append("iface {0} inet dhcp".format(dev)) else: interfaces.append("iface {0} inet static".format(dev)) interfaces.append(" address " + ip) network = int2ip(ip2int(ip) & ip2int(netsz2netmask(sz))) interfaces.append(" network " + network) interfaces.append(" netmask " + netsz2netmask(sz)) gfs.write('/etc/udev/rules.d/70-persistent-net.rules', "\n".join(rules_fc)) # gfs.write('/etc/network/interfaces', "\n".join(interfaces)) gfs.write('/etc/network/interfaces.d/eth0', "\n".join(interfaces)) # update passwords logger.debug("Update passwords") chars = "".join(chr(i) for i in range(ord('a'), ord('z') + 1)) chars += "".join(chr(i) for i in range(ord('A'), ord('Z') + 1)) chars += "".join(chr(i) for i in range(ord('0'), ord('9') + 1)) hashes = {} for login, passwd in passwords.items(): salt = "".join(random.choice(chars) for _ in range(8)) hashes[login] = crypt.crypt(passwd, "$6$" + salt) new_shadow = [] need_logins = set(hashes) for ln in gfs.read_file('/etc/shadow').split('\n'): ln = ln.strip() if ln != '' and ln[0] != '#': login = ln.split(':', 1)[0] if login in hashes: sh_templ = "{login}:{hash}:{rest}" sh_line = sh_templ.format(login=login, hash=hashes[login], rest=ln.split(':', 2)[2]) new_shadow.append(sh_line) need_logins.remove(login) else: new_shadow.append(ln) for login in need_logins: new_sh_templ = "{login}:{hash}:{rest}" new_sh_line = new_sh_templ.format(login=login, hash=hashes[login], rest="0:0:99999:7:::") new_shadow.append(new_sh_line) gfs.write('/etc/shadow', "\n".join(new_shadow)) # add new users to passwd ids = [] logins = [] passwd = gfs.read_file('/etc/passwd') for ln in passwd.split('\n'): ln = ln.strip() if ln != '' and ln[0] != '#': logins.append(ln.split(':', 1)[0]) ids.append(ln.split(':')[2]) ids.append(ln.split(':')[3]) add_lines = [] try: mid = max(i for i in ids if i < 65000) except ValueError: mid = 0 mid += 1024 for login in set(hashes) - set(logins): home = '/home/' + login add_lines.append(":".join( [login, 'x', str(mid), str(mid), "", home, '/bin/bash'])) if not gfs.exists(home): gfs.mkdir_p(home) mid += 1 if add_lines != []: gfs.write('/etc/passwd', passwd.rstrip() + "\n" + "\n".join(add_lines)) # if apt_proxy_ip is not None: # logger.debug("Set apt-proxy to http://{0}:3142".format(apt_proxy_ip)) # fc = 'Acquire::http {{ Proxy "http://{0}:3142"; }};'.format(apt_proxy_ip) # gfs.write('/etc/apt/apt.conf.d/02proxy', fc) logger.debug("Update hosts") hosts = gfs.read_file('/etc/hosts') new_hosts = ["127.0.0.1 localhost\n127.0.0.1 " + hostname] for ln in hosts.split('#'): if not ln.strip().startswith('127.0.0.1'): new_hosts.append(ln) gfs.write('/etc/hosts', "\n".join(new_hosts)) # allow ssh passwd auth if gfs.is_file('/etc/ssh/ssh_config'): name = '/etc/ssh/ssh_config' elif gfs.is_file('/etc/ssh/sshd_config'): name = '/etc/ssh/sshd_config' else: logger.warning( "Both '/etc/ssh/sshd_config' and '/etc/ssh/ssh_config' are absent. Skip ssh config patching" ) name = None if name is not None: sshd_conf = gfs.read_file('/etc/ssh/ssh_config') sshd_conf_lines = sshd_conf.split("\n") for pos, ln in enumerate(sshd_conf_lines): if "PasswordAuthentication" in ln: sshd_conf_lines[pos] = "PasswordAuthentication yes" break else: sshd_conf_lines.append("PasswordAuthentication yes") gfs.write('/etc/ssh/ssh_config', "\n".join(sshd_conf_lines))
def random_ip_generator(): while True: ip = random.randint(1,pow(2,32)-1) ip = int2ip(ip) to_send_queue.put(ip, block=True)
def random_ip_generator(): while True: ip = random.randint(1, pow(2, 32) - 1) ip = int2ip(ip) to_send_queue.put(ip, block=True)