Esempio n. 1
0
    def send(self, data):
        """send(data)

        Sends data.

        If log level ``DEBUG`` is enabled, also prints out the data
        received.

        If it is not possible to send anymore because of a closed
        connection, it raises ``exceptions.EOFError``

        Examples:

            >>> def p(x): print repr(x)
            >>> t = tube()
            >>> t.send_raw = p
            >>> t.send('hello')
            'hello'
        """

        if self.isEnabledFor(logging.DEBUG):
            self.debug('Sent %#x bytes:' % len(data))
            if len(set(data)) == 1:
                self.indented('%r * %#x' % (data[0], len(data)))
            elif all(c in string.printable for c in data):
                for line in data.splitlines(True):
                    self.indented(repr(line), level=logging.DEBUG)
            else:
                self.indented(fiddling.hexdump(data), level=logging.DEBUG)
        self.send_raw(data)
Esempio n. 2
0
    def show(self):
        """
        show the log data after format
        """
        log.info('%s:%d get a connnection to %s at %s' % (self._data['ip'],
                                                          self._data['sport'],
                                                          self._data['target'],
                                                          ctime(self._data['con_time'])))

        log.info('The connection from %s:%d' % (self._data['host'], self._data['dport']))
        if self._data['token'] != '':
            log.info('Token is: %s' % self._data['token'])

        datas = self._data['data']

        for data in datas:
            if data[1] == sqllog.recv:
                log.recv('Received %#x bytes At %s: ' % (len(data[2]), ctime(data[0])))
            elif data[1] == sqllog.send:
                log.send('Sent %#x bytes At %s:' % (len(data[2]), ctime(data[0])))

            if len(set(data[2])) == 1:
                log.indented('%r * %#x' % (data[2][0], len(data[2])), level=logging.INFO)
            elif all(c in string.printable for c in data[2]):
                for line in data[2].splitlines(True):
                    log.indented(repr(line), level=logging.INFO)
            else:
                log.indented(fiddling.hexdump(data[2]), level=logging.INFO)
Esempio n. 3
0
def encode(raw_bytes, avoid=None, expr=None, force=0, pcreg=''):
    """encode(raw_bytes, avoid, expr, force) -> str

    Encode shellcode ``raw_bytes`` such that it does not contain
    any bytes in ``avoid`` or ``expr``.

    Arguments:

        raw_bytes(str): Sequence of shellcode bytes to encode.
        avoid(str):     Bytes to avoid
        expr(str):      Regular expression which matches bad characters.
        force(bool):    Force re-encoding of the shellcode, even if it
                        doesn't contain any bytes in ``avoid``.
    """
    orig_avoid = avoid

    avoid = set(avoid or '')

    if expr:
        for char in all_chars:
            if re.search(expr, char):
                avoid.add(char)

    if not (force or avoid & set(raw_bytes)):
        return raw_bytes

    encoders = Encoder._encoders[context.arch]
    random.shuffle(encoders)

    for encoder in encoders:
        if encoder.blacklist & avoid:
            continue

        try:
            v = encoder(raw_bytes, avoid, pcreg)
        except NotImplementedError:
            continue

        if avoid & set(v):
            log.warning_once("Encoder %s did not succeed" % encoder)
            continue

        return v


    avoid_errmsg = ''
    if orig_avoid and expr:
        avoid_errmsg = '%r and %r' % (orig_avoid, expr)
    elif expr:
        avoid_errmsg = repr(expr)
    else:
        avoid_errmsg = repr(bytes(avoid))

    args = (context.arch, avoid_errmsg, hexdump(raw_bytes))
    msg = "No encoders for %s which can avoid %s for\n%s" % args
    msg = msg.replace('%', '%%')
    log.error(msg)
Esempio n. 4
0
    def _fillbuffer(self, timeout=default):
        """_fillbuffer(timeout = default)

        Fills the internal buffer from the pipe, by calling
        :meth:`recv_raw` exactly once.

        Returns:

            The bytes of data received, or ``''`` if no data was received.

        Examples:

            >>> t = tube()
            >>> t.recv_raw = lambda *a: 'abc'
            >>> len(t.buffer)
            0
            >>> t._fillbuffer()
            'abc'
            >>> len(t.buffer)
            3
        """
        data = b''

        with self.local(timeout):
            data = self.recv_raw(self.buffer.get_fill_size())

        if data and self.isEnabledFor(logging.DEBUG):
            self.debug('Received %#x bytes:' % len(data))

            if len(set(data)) == 1 and len(data) > 1:
                self.indented('%r * %#x' % (data[0], len(data)),
                              level=logging.DEBUG)
            elif all(c in string.printable for c in data):
                for line in data.splitlines(True):
                    self.indented(repr(line), level=logging.DEBUG)
            else:
                self.indented(fiddling.hexdump(data), level=logging.DEBUG)

        if data:
            self.buffer.add(data)

        return data