def buildPacket(self, data, address): # https://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol#DHCP_discovery macb = getMacInBytes() packet = b'' packet += b'\x01\x01\x06\x00' # OP ,HTYPE ,HLEN ,HOPS packet += self.transactionID packet += b'\x00\x00' # SECS packet += b'\x80\x00' # FLAGS packet += b'\x00\x00\x00\x00' # CIADDR packet += ipaddress.v4_int_to_packed( int(ipaddress.IPv4Address(address))) # YIADDR packet += ipaddress.v4_int_to_packed( int(ipaddress.IPv4Address(socket.inet_aton(socket.gethostbyname(socket.gethostname()))))) # SIADDR packet += b'\x00\x00\x00\x00' # GIADDR # acket += b'\x44\x8a\x5b\xec\xbf\x92' packet += macb packet += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' packet += b'\x00' * 192 packet += b'\x63\x82\x53\x63' # MAGIC COOKIE packet += b'\x35\x01\x03' # Option: (t=53,l=2) DHCP Message Type = DHCP Offer packet += b'\x3d\x06\x00\x26\x9e\x04\x1e\x9b' # Option: (t=61,l=6) Client identifier packet += b'\x3d\x06' + macb packet += b'\x37\x03\x03\x01\x06' # Option: (t=55,l=3) Parameter Request List packet += b'\xff' # End Option\ return packet
def timestamp(flag=0, hop=1, *specified_route): if flag == 0: if hop >= 10: raise Exception('exceed the max length of ip header') length = chr(hop * 4 + 4) option_head = b'\x44' + length.encode() + b'\x05\x00' route_data = "".join(['\x00'] * 4 * hop) option = option_head + ('%s' % route_data).encode() return option if flag == 1: if hop >= 5: raise Exception('exceed the max length of ip header') length = chr(hop * 8 + 4) option_head = b'\x44' + length.encode() + b'\x05\x01' route_data = "".join(['\x00'] * 8 * hop) option = option_head + ('%s' % route_data).encode() return option if flag == 3: hop_check = len(specified_route) if hop_check != hop: raise Exception('the number of routes is not consistent with hops') if hop >= 5: raise Exception('exceed the max length of ip header') length = chr(hop * 8 + 4) option_head = b'\x44' + length.encode() + b'\x05\x03' option = option_head for ip in specified_route: ip = ipa.v4_int_to_packed(int(ipa.IPv4Address(ip))) option = option + ip + b'\x00\x00\x00\x00' return option raise Exception( 'no such flag, flag must be set as 0,1,3 in timastamp option')
def copy_to(self, target_: asn1.SkrIPMask): if isinstance(self.mask, ipaddress.IPv4Address): target_.setComponentByName( 'ipv4-mask', ipaddress.v4_int_to_packed(int(self.mask))) else: target_.setComponentByName( 'ipv6-mask', ipaddress.v6_int_to_packed(int(self.mask)))
def copy_to(self, target_): if isinstance(self.address, ipaddress.IPv4Address): target_.setComponentByName( 'ipv4', ipaddress.v4_int_to_packed(int(self.address))) else: target_.setComponentByName( 'ipv6', ipaddress.v6_int_to_packed(int(self.address)))
def ssrr(*source_route_table): hop = len(source_route_table) if hop >= 10: raise Exception('exceed the max length of ip header') length = chr(hop * 4 + 3) option_head = b'\x89' + length.encode() + b'\x04' option = option_head for ip in source_route_table: ip = ipa.v4_int_to_packed(int(ipa.IPv4Address(ip))) option = option + ip return option
def makeResponseForA(parsedData, IP, ttl=60): ttl = int(ttl) domain = parsedData.domain typeOf = parsedData.qType IPData = v4_int_to_packed(IP._ip) data = requestMaker(parsedData.domain, parsedData.qType, 1, 1) data = writeString(data, parsedData.domain) fmt = struct.Struct("!H H I H") qT = (typeOf, IN, ttl, 4) data += fmt.pack(*qT) data += IPData return data
def __make_address__(key): """Make version and address.""" address = ipaddress.ip_address(self.__args__[key]) version = address.version if version == 4: # IPv4 address verbit = '0100' packed = ipaddress.v4_int_to_packed(int(address)) elif version == 6: # IPv6 address verbit = '0110' packed = ipaddress.v6_int_to_packed(int(address)) else: raise pcapkit.utilities.exceptions.VersionError( 'unknown IP version') return (verbit, packed)
def SerializeAddress(listenAddress): """ Serialize the subscription request message before sending it to the publisher :param listenAddress: subscriber's litening address """ portArray = array('H', [listenAddress[1]]) ipInInt = int(ipaddress.IPv4Address(listenAddress[0])) ipInBytes = ipaddress.v4_int_to_packed(ipInInt) portArray.byteswap() portInBytes = portArray.tobytes() serializedAddress = ipInBytes + portInBytes return serializedAddress
def push_ipv4(self, key:Payload, v:any): b = ipaddress.v4_int_to_packed(int(ipaddress.IPv4Address(v))) self.data.append({'key': key, 'bytes': b})
def convert_to_binary(int_ip_address): binary_ip = ipaddress.v4_int_to_packed(int_ip_address) formatted_binary_ip = binascii.hexlify(binary_ip) decoded_ip = formatted_binary_ip.decode() return decoded_ip
def push_ipv4(self, key, v): if sys.version_info < (3, 0): v = unicode(v, "utf-8") b = ipaddress.v4_int_to_packed(int(ipaddress.IPv4Address(v))) self.data.append({'key': key, 'bytes': b})
def update_event(self, inp=-1): self.set_output_val(0, ipaddress.v4_int_to_packed(self.input(0)))