Exemple #1
0
    def from_filter_string(filter_string=None, parsed_args=None):
        """
        from_filter_string(filter_string)

        Create a filter from a filter string. If passed a list of arguments, they
        will be used instead of parsing the string.

        :rtype: Deferred that returns a :class:`pappyproxy.context.Filter`
        """
        if parsed_args is not None:
            args = parsed_args
        else:
            args = shlex.split(filter_string)
        if len(args) == 0:
            raise PappyException('Field is required')
        field = args[0]
        new_filter = None

        field_args = args[1:]
        if field in Filter._filter_functions:
            new_filter = Filter._filter_functions[field](field_args)
        elif field in Filter._async_filter_functions:
            new_filter = yield Filter._async_filter_functions[field](
                field_args)
        else:
            raise FilterParseError("%s is not a valid field" % field)

        if new_filter is None:
            raise FilterParseError("Error creating filter")
        defer.returnValue(new_filter)
Exemple #2
0
def cmp_containsr(a, b):
    try:
        if re.search(b, a):
            return True
        return False
    except re.error as e:
        raise PappyException('Invalid regexp: %s' % e)
Exemple #3
0
    def action_get_request(self, data):
        try:
            reqid = data['reqid']
            req = yield pappyproxy.http.Request.load_request(reqid)
        except KeyError:
            raise PappyException("Request with given ID does not exist")

        dat = json.loads(req.to_json())
        defer.returnValue(dat)
Exemple #4
0
def gen_filter_by_saved(args):
    if len(args) != 0:
        raise PappyException('Invalid number of arguments')
    def f(req):
        if req.saved:
            return True
        else:
            return False
    return f
Exemple #5
0
def get_saved_context(name, dbpool):
    rows = yield dbpool.runQuery(
        """
        SELECT filter_strings FROM saved_contexts WHERE context_name=?;
        """, (name,)
    )
    if len(rows) == 0:
        raise PappyException("Saved context with name %s does not exist" % name)
    filter_strs = json.loads(rows[0][0])
    defer.returnValue(filter_strs)
Exemple #6
0
def compval_from_args_repdict(args):
    """
    NOINDEX
    Similar to compval_from_args but checks a repeatable dict with up to 2
    comparers and values.
    """
    if len(args) == 0:
        raise PappyException('Invalid number of arguments')
    nextargs = args[:]
    value = None
    if args[0] in _BARE_COMPARERS:
        comparer = relation_from_text(args[0], None)
        if len(args) > 1:
            nextargs = args[1:]
    else:
        if len(args) == 1:
            raise PappyException('Invalid number of arguments')
        comparer = relation_from_text(args[0], args[1])
        value = args[1]
        nextargs = args[2:]

    comparer2 = None
    value2 = None
    if nextargs:
        if nextargs[0] in _BARE_COMPARERS:
            comparer2 = relation_from_text(nextargs[0], None)
        else:
            if len(nextargs) == 1:
                raise PappyException('Invalid number of arguments')
            comparer2 = relation_from_text(nextargs[0], nextargs[1])
            value2 = nextargs[1]

    def retfunc(d):
        for k, v in d.all_pairs():
            if comparer2 is None:
                if comparer(k, value) or comparer(v, value):
                    return True
            else:
                if comparer(k, value) and comparer2(v, value2):
                    return True
        return False

    return retfunc
Exemple #7
0
def gen_filter_by_after(args, negate=False):
    if len(args) != 1:
        raise PappyException('Invalid number of arguments')
    r = yield Request.load_request(args[0])
    def f(req):
        if req.time_start is None:
            return False
        if r.time_start is None:
            return False
        return req.time_start >= r.time_start
    defer.returnValue(f)
Exemple #8
0
def compval_from_args(args):
    """
    NOINDEX
    returns a function that compares to a value from text.
    ie compval_from_text('ct foo') will return a function that returns true
    if the passed in string contains foo.
    """
    if len(args) == 0:
        raise PappyException('Invalid number of arguments')
    if args[0] in _BARE_COMPARERS:
        if len(args) != 1:
            raise PappyException('Invalid number of arguments')
        comparer = relation_from_text(args[0], None)
        value = None
    else:
        if len(args) != 2:
            raise PappyException('Invalid number of arguments')
        comparer = relation_from_text(args[0], args[1])
        value = args[1]

    def retfunc(s):
        return comparer(s, value)

    return retfunc
Exemple #9
0
    def action_get_response(self, data):
        try:
            reqid = data['reqid']
            req = yield Request.load_request(reqid)
        except KeyError:
            raise PappyException(
                "Request with given ID does not exist, cannot fetch associated response."
            )

        if req.response:
            rsp = yield Response.load_response(req.response.rspid)
            dat = json.loads(rsp.to_json())
        else:
            dat = {}
        defer.returnValue(dat)
Exemple #10
0
    def action_submit_request(self, data):
        try:
            req = pappyproxy.http.Request(
                base64.b64decode(data['full_request']))
            req.port = data['port']
            req.is_ssl = data['is_ssl']
        except:
            raise PappyException("Error parsing request")
        yield req.async_submit()
        yield req.async_deep_save()

        retdata = {}
        retdata['request'] = json.loads(req.to_json())
        if req.response:
            retdata['response'] = json.loads(req.response.to_json())
        defer.returnValue(retdata)
Exemple #11
0
    def lineReceived(self, line):
        line = line.strip()

        if line == '':
            return
        #try:
        command_data = json.loads(line)
        command = command_data['action']
        valid = False
        if command in self.action_handlers:
            valid = True
            result = {'success': True}
            func_defer = self.action_handlers[command](command_data)
            func_defer.addCallback(self.action_result_handler, result)
            func_defer.addErrback(self.action_error_handler, result)
        if not valid:
            raise PappyException('%s is an invalid command' %
                                 command_data['action'])
Exemple #12
0
 def lineReceived(self, line):
     if line == '':
         return
     try:
         command_data = json.loads(line)
         command = command_data['action']
         valid = False
         if command in self.action_handlers:
             valid = True
             result = {'success': True}
             func_defer = self.action_handlers[command](command_data)
             func_defer.addCallback(self.action_result_handler, result)
             func_defer.addErrback(self.action_error_handler, result)
         if not valid:
             raise PappyException('%s is an invalid command' %
                                  command_data['action'])
     except PappyException as e:
         return_data = {'success': False, 'message': str(e)}
         self.sendLine(json.dumps(return_data))
Exemple #13
0
 def help(name):
     if name not in BuiltinFilters._filters:
         raise PappyException('%s not a bult in filter' % name)
     return Filter(BuiltinFilters._filters[name][1])
Exemple #14
0
    def from_filter_string(filter_string):
        args = shlex.split(filter_string)
        field = args[0]
        relation = args[1]
        new_filter = None

        negate = False
        if relation[0] == 'n' and len(relation) > 1:
            negate = True
            relation = relation[1:]

        if len(args) > 2:
            val1 = args[2]
        elif relation not in ('ex', ):
            raise PappyException('%s requires a value' % relation)
        else:
            val1 = None
        if len(args) > 3:
            comp2 = args[3]
        else:
            comp2 = None
        if len(args) > 4:
            val2 = args[4]
        else:
            comp2 = None

        # Raises exception if invalid
        comparer = get_relation(relation, val1)

        if field in ("all", ):
            new_filter = gen_filter_by_all(comparer, val1, negate)
        elif field in ("host", "domain", "hs", "dm"):
            new_filter = gen_filter_by_host(comparer, val1, negate)
        elif field in ("path", "pt"):
            new_filter = gen_filter_by_path(comparer, val1, negate)
        elif field in ("body", "bd", "data", "dt"):
            new_filter = gen_filter_by_body(comparer, val1, negate)
        elif field in ("verb", "vb"):
            new_filter = gen_filter_by_verb(comparer, val1, negate)
        elif field in ("param", "pm"):
            if len(args) > 4:
                comparer2 = get_relation(comp2, val2)
                new_filter = gen_filter_by_params(comparer, val1, comparer2,
                                                  val2, negate)
            else:
                new_filter = gen_filter_by_params(comparer,
                                                  val1,
                                                  negate=negate)
        elif field in ("header", "hd"):
            if len(args) > 4:
                comparer2 = get_relation(comp2, val2)
                new_filter = gen_filter_by_headers(comparer, val1, comparer2,
                                                   val2, negate)
            else:
                new_filter = gen_filter_by_headers(comparer,
                                                   val1,
                                                   negate=negate)
        elif field in ("rawheaders", "rh"):
            new_filter = gen_filter_by_raw_headers(comparer, val1, negate)
        elif field in ("sentcookie", "sck"):
            if len(args) > 4:
                comparer2 = get_relation(comp2, val2)
                new_filter = gen_filter_by_submitted_cookies(
                    comparer, val1, comparer2, val2, negate)
            else:
                new_filter = gen_filter_by_submitted_cookies(comparer,
                                                             val1,
                                                             negate=negate)
        elif field in ("setcookie", "stck"):
            if len(args) > 4:
                comparer2 = get_relation(comp2, val2)
                new_filter = gen_filter_by_set_cookies(comparer, val1,
                                                       comparer2, val2, negate)
            else:
                new_filter = gen_filter_by_set_cookies(comparer,
                                                       val1,
                                                       negate=negate)
        elif field in ("statuscode", "sc", "responsecode"):
            new_filter = gen_filter_by_response_code(comparer, val1, negate)
        elif field in ("responsetime", "rt"):
            pass
        elif field in ("tag", "tg"):
            new_filter = gen_filter_by_tag(comparer, val1, negate)
        elif field in ("saved", "svd"):
            new_filter = gen_filter_by_saved(comparer, val1, negate)
        else:
            raise FilterParseError("%s is not a valid field" % field)

        if new_filter is not None:
            return new_filter
        else:
            raise FilterParseError("Error creating filter")
Exemple #15
0
def validate_regexp(r):
    try:
        re.compile(r)
    except re.error as e:
        raise PappyException('Invalid regexp: %s' % e)
Exemple #16
0
 def get(name):
     if name not in BuiltinFilters._filters:
         raise PappyException('%s not a bult in filter' % name)
     if name in BuiltinFilters._filters:
         return [Filter(f) for f in BuiltinFilters._filters[name][0]]
Exemple #17
0
    def from_filter_string(filter_string=None, parsed_args=None):
        """
        from_filter_string(filter_string)

        Create a filter from a filter string. If passed a list of arguments, they
        will be used instead of parsing the string.

        :rtype: Deferred that returns a :class:`pappyproxy.context.Filter`
        """
        if parsed_args is not None:
            args = parsed_args
        else:
            args = shlex.split(filter_string)
        if len(args) == 0:
            raise PappyException('Field is required')
        field = args[0]
        new_filter = None

        field_args = args[1:]
        if field in ("all",):
            new_filter = gen_filter_by_all(field_args)
        elif field in ("host", "domain", "hs", "dm"):
            new_filter = gen_filter_by_host(field_args)
        elif field in ("path", "pt"):
            new_filter = gen_filter_by_path(field_args)
        elif field in ("body", "bd", "data", "dt"):
            new_filter = gen_filter_by_body(field_args)
        elif field in ("reqbody", "qbd", "reqdata", "qdt"):
            new_filter = gen_filter_by_req_body(field_args)
        elif field in ("rspbody", "sbd", "qspdata", "sdt"):
            new_filter = gen_filter_by_rsp_body(field_args)
        elif field in ("verb", "vb"):
            new_filter = gen_filter_by_verb(field_args)
        elif field in ("param", "pm"):
            new_filter = gen_filter_by_params(field_args)
        elif field in ("header", "hd"):
            new_filter = gen_filter_by_headers(field_args)
        elif field in ("reqheader", "qhd"):
            new_filter = gen_filter_by_request_headers(field_args)
        elif field in ("rspheader", "shd"):
            new_filter = gen_filter_by_response_headers(field_args)
        elif field in ("rawheaders", "rh"):
            new_filter = gen_filter_by_raw_headers(field_args)
        elif field in ("sentcookie", "sck"):
            new_filter = gen_filter_by_submitted_cookies(field_args)
        elif field in ("setcookie", "stck"):
            new_filter = gen_filter_by_set_cookies(field_args)
        elif field in ("statuscode", "sc", "responsecode"):
            new_filter = gen_filter_by_response_code(field_args)
        elif field in ("responsetime", "rt"):
            raise PappyException('Not implemented yet, sorry!')
        elif field in ("tag", "tg"):
            new_filter = gen_filter_by_tag(field_args)
        elif field in ("saved", "svd"):
            new_filter = gen_filter_by_saved(field_args)
        elif field in ("before", "b4", "bf"):
            new_filter = yield gen_filter_by_before(field_args)
        elif field in ("after", "af"):
            new_filter = yield gen_filter_by_after(field_args)
        elif field in ("inv",):
            new_filter = yield gen_filter_by_inverse(field_args)
        else:
            raise FilterParseError("%s is not a valid field" % field)

        if new_filter is None:
            raise FilterParseError("Error creating filter")
        # dirty hack to get it to work if we don't generate any deferreds
        # d = defer.Deferred()
        # d.callback(None)
        # yield d
        defer.returnValue(new_filter)