Python socket.ntohs() Examples
The following are 30
code examples of socket.ntohs().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
socket
, or try the search function
.
![](https://www.programcreek.com/common/static/images/search.png)
Example #1
Source File: http_exfiltration.py From PyExfil with MIT License | 6 votes |
def listen(local_addr, local_port=80): """ This function will initiate a web listener (NOT SERVER!) on default port 80. It will then capture files and save them into a local file. :param local_addr: The ip address to bind to. :param local_port: The port. If not mentioned, 80 will be chosen. :return: """ def eth_addr(a): b = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (ord(a[0]), ord(a[1]), ord(a[2]), ord(a[3]), ord(a[4]), ord(a[5])) return b try: s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(ETH_P_ALL)) except socket.error, msg: sys.stderr.write('Socket could not be created. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] + "\n") raise ()
Example #2
Source File: test_socket_connection.py From boofuzz with GNU General Public License v2.0 | 6 votes |
def bind(self): """ Bind server, and call listen if using TCP, meaning that the client test code can successfully connect. """ if self.proto == "tcp": self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) elif self.proto == "udp": self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) elif self.proto == "raw": self.server_socket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(ETH_P_ALL)) else: raise Exception("Invalid protocol type: '{0}'".format(self.proto)) self.server_socket.bind((self.host, 0)) # let OS choose a free port if self.proto == "tcp": self.server_socket.listen(1) self.active_port = self.server_socket.getsockname()[1]
Example #3
Source File: test_session_failure_handling.py From boofuzz with GNU General Public License v2.0 | 6 votes |
def bind(self): """ Bind server, and call listen if using TCP, meaning that the client test code can successfully connect. """ if self.proto == "tcp": self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) elif self.proto == "udp": self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) elif self.proto == "raw": self.server_socket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0003)) else: raise Exception("Invalid protocol type: '{0}'".format(self.proto)) self.server_socket.bind((self.host, 0)) # let OS choose a free port if self.proto == "tcp": self.server_socket.listen(1) self.active_port = self.server_socket.getsockname()[1]
Example #4
Source File: sniff.py From python-scripts with GNU General Public License v3.0 | 6 votes |
def decode_ip_packet(s): d={} d['version']=(ord(s[0]) & 0xf0) >> 4 d['header_len']=ord(s[0]) & 0x0f d['tos']=ord(s[1]) d['total_len']=socket.ntohs(struct.unpack('H',s[2:4])[0]) d['id']=socket.ntohs(struct.unpack('H',s[4:6])[0]) d['flags']=(ord(s[6]) & 0xe0) >> 5 d['fragment_offset']=socket.ntohs(struct.unpack('H',s[6:8])[0] & 0x1f) d['ttl']=ord(s[8]) d['protocol']=ord(s[9]) d['checksum']=socket.ntohs(struct.unpack('H',s[10:12])[0]) d['source_address']=pcap.ntoa(struct.unpack('i',s[12:16])[0]) d['destination_address']=pcap.ntoa(struct.unpack('i',s[16:20])[0]) if d['header_len']>5: d['options']=s[20:4*(d['header_len']-5)] else: d['options']=None d['data']=s[4*d['header_len']:] return d
Example #5
Source File: packet.py From Doga with MIT License | 6 votes |
def parse(self, ip, packet_string): """ Parse required info from packet according Ethernet Header structure Reference: http://en.wikipedia.org/wiki/Ethernet_frame#Structure param: ip(str): local IP address of machine param: packet_string(str): packet string from packet tuple object """ eth_h = packet_string[:ETH_LENGTH] unpacked_eth_h = struct.unpack('!6s6sH', eth_h) eth_protocol = socket.ntohs(unpacked_eth_h[2]) if (eth_protocol == 8): iph_len, packet_protocol, addr = self.parse_ip_header( packet_string) if (packet_protocol == 6): data, ports = self.parse_tcp_header(packet_string, iph_len) if self.verify_packet_data(ip, data, addr, ports): self.payload_parser.parse(data, ports)
Example #6
Source File: winutil.py From flare-fakenet-ng with Apache License 2.0 | 6 votes |
def _get_pid_port_tcp(self, port): for item in self.get_extended_tcp_table(): lPort = socket.ntohs(item.dwLocalPort) lAddr = socket.inet_ntoa(struct.pack('L', item.dwLocalAddr)) pid = item.dwOwningPid if lPort == port: return pid else: return None ########################################################################## # The GetExtendedUdpTable function retrieves a table that contains a list of UDP endpoints available to the application. # # DWORD GetExtendedUdpTable( # _Out_ PVOID pUdpTable, # _Inout_ PDWORD pdwSize, # _In_ BOOL bOrder, # _In_ ULONG ulAf, # _In_ UDP_TABLE_CLASS TableClass, # _In_ ULONG Reserved # );
Example #7
Source File: ipv4.py From cyber-security-framework with MIT License | 6 votes |
def __init__(self, buffer: bytes = b""): super().__init__() self.data_offset = self.ihl * 4 self.raw_buffer = buffer self.raw_header = buffer[:self.data_offset] phbs = {v.value: k for k, v in DSCP.__members__.items()} self.dscp = phbs[self.raw_dscp] if self.raw_dscp in phbs else "Unknown PHB" self.ecn = "Non-ECT" if self.raw_ecn is 0 else ("ECT(0)" if self.raw_ecn is 2 else ("ECT(1)" if self.raw_ecn is 1 else "CE")) self.differentiated_services = (self.dscp, self.ecn) self.total_length = socket.ntohs(self.raw_total_length) self.identification = socket.ntohs(self.raw_identification) self.flags = {0: self.raw_flags&1, 1: self.raw_flags&2, 2: self.raw_flags&4} self.protocol = self.protocols[self.raw_protocol] if self.raw_protocol in self.protocols else "Unknown" self.checksum = socket.ntohs(self.raw_checksum) self.source = socket.inet_ntoa(struct.pack("<L", self.raw_source)) self.destination = socket.inet_ntoa(struct.pack("<L", self.raw_destination)) self.raw_options = b"" self.options = None if self.ihl > 5: self.raw_options = buffer[0x14:self.data_offset] self.options = self.options = self.Options(self.raw_options)#self.parse_options(self.raw_options) self.data = buffer[self.data_offset:]
Example #8
Source File: xcon.py From vrnetlab with MIT License | 6 votes |
def __init__(self, raw_intf = 'eth1', listen_port=10001): self.logger = logging.getLogger() # setup TCP side self.s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) self.s.bind(('::0', listen_port)) self.s.listen(1) self.tcp = None # track current state of TCP side tunnel. 0 = reading size, 1 = reading packet self.tcp_state = 0 self.tcp_buf = b'' self.tcp_remaining = 0 # setup raw side self.raw = socket.socket( socket.AF_PACKET , socket.SOCK_RAW , socket.ntohs(0x0003)) self.raw.bind((raw_intf, 0)) # don't block self.raw.setblocking(0)
Example #9
Source File: winutil.py From flare-fakenet-ng with Apache License 2.0 | 6 votes |
def _get_pid_port_udp(self, port): for item in self.get_extended_udp_table(): lPort = socket.ntohs(item.dwLocalPort) lAddr = socket.inet_ntoa(struct.pack('L', item.dwLocalAddr)) pid = item.dwOwningPid if lPort == port: return pid else: return None ########################################################################## # Retrieves the name of the executable file for the specified process. # # DWORD WINAPI GetProcessImageFileName( # _In_ HANDLE hProcess, # _Out_ LPTSTR lpImageFileName, # _In_ DWORD nSize # );
Example #10
Source File: transparentlistener.py From rsp with MIT License | 6 votes |
def get_orig_dst(sock): own_addr = sock.getsockname()[0] own_af = detect_af(own_addr) if own_af == socket.AF_INET: buf = sock.getsockopt(socket.SOL_IP, constants.SO_ORIGINAL_DST, sockaddr_size) sa = sockaddr_in.from_buffer_copy(buf) addr = socket.ntohl(sa.sin_addr) addr = str(addr >> 24) + '.' + str((addr >> 16) & 0xFF) + '.' + str((addr >> 8) & 0xFF) + '.' + str(addr & 0xFF) port = socket.ntohs(sa.sin_port) return addr, port elif own_af == socket.AF_INET6: buf = sock.getsockopt(constants.SOL_IPV6, constants.SO_ORIGINAL_DST, sockaddr6_size) sa = sockaddr_in6.from_buffer_copy(buf) addr = socket.inet_ntop(socket.AF_INET6, sa.sin6_addr) port = socket.ntohs(sa.sin_port) return addr, port else: raise RuntimeError("Unknown address family!")
Example #11
Source File: create_layer_2_socket.py From network-pipeline with Apache License 2.0 | 5 votes |
def create_layer_2_socket(): """create_layer_2_socket""" # create a socket for recording layer 2, 3 and 4 frames s = None try: log.info("Creating l234 socket") s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0003)) except socket.error as msg: log.error(("Socket could not be created ex={}") .format(msg)) return s # end of create_layer_2_socket
Example #12
Source File: network.py From PythonForWindows with BSD 3-Clause "New" or "Revised" License | 5 votes |
def local_port(self): """:type: :class:`int`""" return socket.ntohs(self.dwLocalPort)
Example #13
Source File: test_socket.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def testNtoH(self): if sys.platform[:4] == 'java': return # problems with int & long # This just checks that htons etc. are their own inverse, # when looking at the lower 16 or 32 bits. sizes = {socket.htonl: 32, socket.ntohl: 32, socket.htons: 16, socket.ntohs: 16} for func, size in sizes.items(): mask = (1L<<size) - 1 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): self.assertEqual(i & mask, func(func(i&mask)) & mask) swapped = func(mask) self.assertEqual(swapped & mask, mask) self.assertRaises(OverflowError, func, 1L<<34)
Example #14
Source File: fields.py From arissploit with GNU General Public License v3.0 | 5 votes |
def reverse(self, val): if self.size == 16: val = socket.ntohs(val) elif self.size == 32: val = socket.ntohl(val) return val
Example #15
Source File: fields.py From kamene with GNU General Public License v2.0 | 5 votes |
def reverse(self, val): if self.size == 16: val = socket.ntohs(val) elif self.size == 32: val = socket.ntohl(val) return val
Example #16
Source File: 11_5_integer_conversion.py From Python-Network-Programming with MIT License | 5 votes |
def convert_integer(): data = 1234 # 32-bit print ("Original: %s => Long host byte order: %s, Network byte order: %s" %(data, socket.ntohl(data), socket.htonl(data))) # 16-bit print ("Original: %s => Short host byte order: %s, Network byte order: %s" %(data, socket.ntohs(data), socket.htons(data)))
Example #17
Source File: grove_i2c_thermocouple_amplifier_mcp9600.py From grove.py with MIT License | 5 votes |
def read(self): data = self._bus.read_word_data(self._addr, self._junc) # Big endian -> little endian data = socket.ntohs(data) # print("RAW = 0x%X" % data) # It's 16-bit 2's complement code temperature = ctypes.c_short(data).value / 16.0 return temperature
Example #18
Source File: win32-identd.py From code with MIT License | 5 votes |
def unpack_addr(af, psockaddr): if af == socket.AF_INET: addr, port = psockaddr addr = socket.inet_ntoa(struct.pack("!L", socket.ntohl(addr))) port = socket.ntohs(port) return addr, port elif af == socket.AF_INET6: if len(psockaddr) == 2: addr, port = psockaddr flow, scope = None, None elif len(psockaddr) == 4: addr, port, flow, scope = psockaddr addr = ":".join("%04x" % x for x in struct.unpack("!8H", addr)) port = socket.ntohs(port) return addr, port, flow, scope
Example #19
Source File: fields.py From dash-hack with MIT License | 5 votes |
def reverse(self, val): if self.size == 16: val = socket.ntohs(val) elif self.size == 32: val = socket.ntohl(val) return val
Example #20
Source File: network.py From PythonForWindows with BSD 3-Clause "New" or "Revised" License | 5 votes |
def remote_port(self): """:type: :class:`int`""" if not self.established: return None return socket.ntohs(self.dwRemotePort)
Example #21
Source File: net.py From code with MIT License | 5 votes |
def port(self): return socket.ntohs(self.sin6_port)
Example #22
Source File: fields.py From POC-EXP with GNU General Public License v3.0 | 5 votes |
def reverse(self, val): if self.size == 16: val = socket.ntohs(val) elif self.size == 32: val = socket.ntohl(val) return val
Example #23
Source File: test_socket.py From medicare-demo with Apache License 2.0 | 5 votes |
def testNtoH(self): if sys.platform[:4] == 'java': return # problems with int & long # This just checks that htons etc. are their own inverse, # when looking at the lower 16 or 32 bits. sizes = {socket.htonl: 32, socket.ntohl: 32, socket.htons: 16, socket.ntohs: 16} for func, size in sizes.items(): mask = (1L<<size) - 1 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): self.assertEqual(i & mask, func(func(i&mask)) & mask) swapped = func(mask) self.assertEqual(swapped & mask, mask) self.assertRaises(OverflowError, func, 1L<<34)
Example #24
Source File: test_socket.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def testNtoHErrors(self): good_values = [ 1, 2, 3, 1, 2, 3 ] bad_values = [ -1, -2, -3, -1, -2, -3 ] for k in good_values: socket.ntohl(k) socket.ntohs(k) socket.htonl(k) socket.htons(k) for k in bad_values: self.assertRaises(OverflowError, socket.ntohl, k) self.assertRaises(OverflowError, socket.ntohs, k) self.assertRaises(OverflowError, socket.htonl, k) self.assertRaises(OverflowError, socket.htons, k)
Example #25
Source File: test_socket.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def testNtoH(self): # This just checks that htons etc. are their own inverse, # when looking at the lower 16 or 32 bits. sizes = {socket.htonl: 32, socket.ntohl: 32, socket.htons: 16, socket.ntohs: 16} for func, size in sizes.items(): mask = (1<<size) - 1 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): self.assertEqual(i & mask, func(func(i&mask)) & mask) swapped = func(mask) self.assertEqual(swapped & mask, mask) self.assertRaises(OverflowError, func, 1<<34)
Example #26
Source File: FENRIR2.py From fenrir-ocd with MIT License | 5 votes |
def bindAllIface(self): self.s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0003))
Example #27
Source File: Autoconf.py From fenrir-ocd with MIT License | 5 votes |
def __init__(self): self.hostmac = "" self.hostip = "" self.conf = True self.ifaceHost = "em1" self.ifaceNetwork = "eth0" self.sockHost = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0003)) self.sockNetwork = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0003)) try: self.sockHost.bind((self.ifaceHost, 0)) self.sockNetwork.bind((self.ifaceNetwork, 0)) except: #exit("You need 2 physical network interfaces to use FENRIR !") print("You need 2 physical network interfaces to use FENRIR !") self.inputs = [self.sockHost, self.sockNetwork]
Example #28
Source File: test_socket.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def testNtoHErrors(self): good_values = [ 1, 2, 3, 1L, 2L, 3L ] bad_values = [ -1, -2, -3, -1L, -2L, -3L ] for k in good_values: socket.ntohl(k) socket.ntohs(k) socket.htonl(k) socket.htons(k) for k in bad_values: self.assertRaises(OverflowError, socket.ntohl, k) self.assertRaises(OverflowError, socket.ntohs, k) self.assertRaises(OverflowError, socket.htonl, k) self.assertRaises(OverflowError, socket.htons, k)
Example #29
Source File: test_socket.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def testNtoH(self): # This just checks that htons etc. are their own inverse, # when looking at the lower 16 or 32 bits. sizes = {socket.htonl: 32, socket.ntohl: 32, socket.htons: 16, socket.ntohs: 16} for func, size in sizes.items(): mask = (1L<<size) - 1 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): self.assertEqual(i & mask, func(func(i&mask)) & mask) swapped = func(mask) self.assertEqual(swapped & mask, mask) self.assertRaises(OverflowError, func, 1L<<34)
Example #30
Source File: fields.py From isip with MIT License | 5 votes |
def reverse(self, val): if self.size == 16: val = socket.ntohs(val) elif self.size == 32: val = socket.ntohl(val) return val