Exemple #1
0
def submit(line):
    """
    Resubmit some requests, optionally with modified headers and cookies.

    Usage: submit reqids [-h] [-m] [-u] [-p] [-o REQID] [-c [COOKIES [COOKIES ...]]] [-d [HEADERS [HEADERS ...]]]
    """
    
    parser = argparse.ArgumentParser(prog="submit", usage=submit.__doc__)
    parser.add_argument('reqids')
    parser.add_argument('-m', '--inmem', action='store_true', help='Store resubmitted requests in memory without storing them in the data file')
    parser.add_argument('-u', '--unique', action='store_true', help='Only resubmit one request per endpoint (different URL parameters are different endpoints)')
    parser.add_argument('-p', '--uniquepath', action='store_true', help='Only resubmit one request per endpoint (ignoring URL parameters)')
    parser.add_argument('-c', '--cookies', nargs='*', help='Apply a cookie to requests before submitting')
    parser.add_argument('-d', '--headers', nargs='*', help='Apply a header to requests before submitting')
    parser.add_argument('-o', '--copycookies', help='Copy the cookies used in another request')
    args = parser.parse_args(shlex.split(line))

    headers = {}
    cookies = {}
    clear_cookies = False

    if args.headers:
        for h in args.headers:
            k, v = h.split('=', 1)
            headers[k] = v

    if args.copycookies:
        reqid = args.copycookies
        req = yield Request.load_request(reqid)
        clear_cookies = True
        for k, v in req.cookies.all_pairs():
            cookies[k] = v

    if args.cookies:
        for c in args.cookies:
            k, v = c.split('=', 1)
            cookies[k] = v

    if args.unique and args.uniquepath:
        raise PappyException('Both -u and -p cannot be given as arguments')

    newsession = Session(cookie_vals=cookies, header_vals=headers)
    
    reqs = yield load_reqlist(args.reqids)

    for req in reqs:
        if clear_cookies:
            req.cookies.clear()
        newsession.apply_req(req)

    conf_message = "You're about to submit %d requests, continue?" % len(reqs)
    if not confirm(conf_message):
        defer.returnValue(None)

    for r in reqs:
        r.tags.add('resubmitted')

    save = not args.inmem
    yield async_submit_requests(reqs, save=save, save_in_mem=args.inmem,
        unique_paths=args.uniquepath, unique_path_and_args=args.unique)
Exemple #2
0
def list_reqs(line):
    """
    List the most recent in-context requests. By default shows the most recent 25
    Usage: list [a|num]

    If `a` is given, all the in-context requests are shown. If a number is given,
    that many requests will be shown.
    """
    args = shlex.split(line)
    if len(args) > 0:
        if args[0][0].lower() == 'a':
            print_count = -1
        else:
            try:
                print_count = int(args[0])
            except:
                print "Please enter a valid argument for list"
                return
    else:
        print_count = 25

    rows = []
    ids = yield async_main_context_ids(print_count)
    for i in ids:
        req = yield Request.load_request(i)
        rows.append(get_req_data_row(req))
    print_request_rows(rows)
Exemple #3
0
def untag(line):
    """
    Remove a tag from requests
    Usage: untag <tag> <request ids>
    You can provide as many request ids as you want and the tag will
    be removed from all of them. If no ids are given, the tag will 
    be removed from all in-context requests.
    """
    args = shlex.split(line)
    if len(args) == 0:
        raise PappyException("Tag and request ids are required")
    tag = args[0]

    ids = []
    if len(args) > 1:
        reqids = yield load_reqlist(args[1], False, ids_only=True)
        print 'Removing tag %s from %s' % (tag, ', '.join(reqids))
    else:
        print "Removing tag %s from all in-context requests" % tag
        reqids = yield main_context_ids()

    for reqid in reqids:
        req = yield Request.load_request(reqid)
        if tag in req.tags:
            req.tags.discard(tag)
            if req.saved:
                yield req.async_save()
    if ids:
        print 'Tag %s removed from %s' % (tag, ', '.join(ids))
Exemple #4
0
def list_reqs(line):
    """
    List the most recent in-context requests. By default shows the most recent 25
    Usage: list [a|num]

    If `a` is given, all the in-context requests are shown. If a number is given,
    that many requests will be shown.
    """
    args = shlex.split(line)
    if len(args) > 0:
        if args[0][0].lower() == 'a':
            print_count = -1
        else:
            try:
                print_count = int(args[0])
            except:
                print "Please enter a valid argument for list"
                return
    else:
        print_count = 25

    rows = []
    ids = yield async_main_context_ids(print_count)
    for i in ids:
        req = yield Request.load_request(i)
        rows.append(get_req_data_row(req))
    print_request_rows(rows)
Exemple #5
0
def tag(line):
    """
    Add a tag to requests.
    Usage: tag <tag> [request ids]
    You can tag as many requests as you want at the same time. If no
    ids are given, the tag will be applied to all in-context requests.
    """
    args = shlex.split(line)
    if len(args) == 0:
        raise PappyException('Tag name is required')
    tag = args[0]

    if len(args) > 1:
        reqids = yield load_reqlist(args[1], False, ids_only=True)
        print 'Tagging %s with %s' % (', '.join(reqids), tag)
    else:
        print "Tagging all in-context requests with %s" % tag
        reqids = yield main_context_ids()

    for reqid in reqids:
        req = yield Request.load_request(reqid)
        if tag not in req.tags:
            req.tags.add(tag)
            if req.saved:
                yield req.async_save()
        else:
            print 'Request %s already has tag %s' % (req.reqid, tag)
Exemple #6
0
def untag(line):
    """
    Remove a tag from requests
    Usage: untag <tag> <request ids>
    You can provide as many request ids as you want and the tag will
    be removed from all of them. If no ids are given, the tag will 
    be removed from all in-context requests.
    """
    args = shlex.split(line)
    if len(args) == 0:
        raise PappyException("Tag and request ids are required")
    tag = args[0]

    ids = []
    if len(args) > 1:
        reqids = yield load_reqlist(args[1], False, ids_only=True)
        print 'Removing tag %s from %s' % (tag, ', '.join(reqids))
    else:
        print "Removing tag %s from all in-context requests" % tag
        reqids = yield async_main_context_ids()

    for reqid in reqids:
        req = yield Request.load_request(reqid)
        if tag in req.tags:
            req.tags.discard(tag)
            if req.saved:
                yield req.async_save()
    if ids:
        print 'Tag %s removed from %s' % (tag, ', '.join(ids))
Exemple #7
0
def tag(line):
    """
    Add a tag to requests.
    Usage: tag <tag> [request ids]
    You can tag as many requests as you want at the same time. If no
    ids are given, the tag will be applied to all in-context requests.
    """
    args = shlex.split(line)
    if len(args) == 0:
        raise PappyException('Tag name is required')
    tag = args[0]

    if len(args) > 1:
        reqids = yield load_reqlist(args[1], False, ids_only=True)
        print 'Tagging %s with %s' % (', '.join(reqids), tag)
    else:
        print "Tagging all in-context requests with %s" % tag
        reqids = yield async_main_context_ids()

    for reqid in reqids:
        req = yield Request.load_request(reqid)
        if tag not in req.tags:
            req.tags.add(tag)
            if req.saved:
                yield req.async_save()
        else:
            print 'Request %s already has tag %s' % (req.reqid, tag)
Exemple #8
0
def site_map(line):
    """
    Print the site map. Only includes requests in the current context.
    Usage: site_map
    """
    ids = yield main_context_ids()
    paths_set = set()
    for reqid in ids:
        req = yield Request.load_request(reqid)
        if req.response and req.response.response_code != 404:
            paths_set.add(req.path_tuple)
    tree = sorted(list(paths_set))
    print_tree(tree)
Exemple #9
0
def dump_response(line):
    """
    Dump the data of the response to a file.
    Usage: dump_response <id> <filename>
    """
    # dump the data of a response
    args = shlex.split(line)
    reqid = args[0]
    req = yield Request.load_request(reqid)
    rsp = req.response
    if len(args) >= 2:
        fname = args[1]
    else:
        fname = req.path.split('/')[-1]

    with open(fname, 'w') as f:
        f.write(rsp.body)
    print 'Response data written to %s' % fname
Exemple #10
0
def dump_response(line):
    """
    Dump the data of the response to a file.
    Usage: dump_response <id> <filename>
    """
    # dump the data of a response
    args = shlex.split(line)
    reqid = args[0]
    req = yield Request.load_request(reqid)
    rsp = req.response
    if len(args) >= 2:
        fname = args[1]
    else:
        fname = req.path.split('/')[-1]

    with open(fname, 'w') as f:
        f.write(rsp.body)
    print 'Response data written to %s' % fname
Exemple #11
0
def site_map(line):
    """
    Print the site map. Only includes requests in the current context.
    Usage: site_map
    """
    args = shlex.split(line)
    if len(args) > 0 and args[0] == 'p':
        paths = True
    else:
        paths = False
    ids = yield async_main_context_ids()
    paths_set = set()
    for reqid in ids:
        req = yield Request.load_request(reqid)
        if req.response and req.response.response_code != 404:
            paths_set.add(req.path_tuple)
    tree = sorted(list(paths_set))
    if paths:
        for p in tree:
            print ('/'.join(list(p)))
    else:
        print_tree(tree)
Exemple #12
0
def site_map(line):
    """
    Print the site map. Only includes requests in the current context.
    Usage: site_map
    """
    args = shlex.split(line)
    if len(args) > 0 and args[0] == 'p':
        paths = True
    else:
        paths = False
    ids = yield async_main_context_ids()
    paths_set = set()
    for reqid in ids:
        req = yield Request.load_request(reqid)
        if req.response and req.response.response_code != 404:
            paths_set.add(req.path_tuple)
    tree = sorted(list(paths_set))
    if paths:
        for p in tree:
            print('/'.join(list(p)))
    else:
        print_tree(tree)
Exemple #13
0
def get_param_info(line):
    args = shlex.split(line)
    if args and args[0] == 'ct':
        contains = True
        args = args[1:]
    else:
        contains = False

    if args:
        params = tuple(args)
    else:
        params = None

    def check_key(k, params, contains):
        if contains:
            for p in params:
                if p.lower() in k.lower():
                    return True
        else:
            if params is None or k in params:
                return True
        return False

    found_params = {}

    ids = yield async_main_context_ids()
    for i in ids:
        req = yield Request.load_request(i)
        for k, v in req.url_params.all_pairs():
            if check_key(k, params, contains):
                add_param(found_params, 'Url Parameter', k, v, req.reqid)
        for k, v in req.post_params.all_pairs():
            if check_key(k, params, contains):
                add_param(found_params, 'POST Parameter', k, v, req.reqid)
        for k, v in req.cookies.all_pairs():
            if check_key(k, params, contains):
                add_param(found_params, 'Cookie', k, v, req.reqid)
    print_param_info(found_params)
Exemple #14
0
def get_param_info(line):
    args = shlex.split(line)
    if args and args[0] == 'ct':
        contains = True
        args = args[1:]
    else:
        contains = False

    if args:
        params = tuple(args)
    else:
        params = None

    def check_key(k, params, contains):
        if contains:
            for p in params:
                if p.lower() in k.lower():
                    return True
        else:
            if params is None or k in params:
                return True
        return False

    found_params = {}

    ids = yield async_main_context_ids()
    for i in ids:
        req = yield Request.load_request(i)
        for k, v in req.url_params.all_pairs():
            if check_key(k, params, contains):
                add_param(found_params, 'Url Parameter', k, v, req.reqid)
        for k, v in req.post_params.all_pairs():
            if check_key(k, params, contains):
                add_param(found_params, 'POST Parameter', k, v, req.reqid)
        for k, v in req.cookies.all_pairs():
            if check_key(k, params, contains):
                add_param(found_params, 'Cookie', k, v, req.reqid)
    print_param_info(found_params)
Exemple #15
0
def submit(line):
    """
    Resubmit some requests, optionally with modified headers and cookies.

    Usage: submit reqids [-h] [-m] [-u] [-p] [-o REQID] [-c [COOKIES [COOKIES ...]]] [-d [HEADERS [HEADERS ...]]]
    """

    parser = argparse.ArgumentParser(prog="submit", usage=submit.__doc__)
    parser.add_argument('reqids')
    parser.add_argument(
        '-m',
        '--inmem',
        action='store_true',
        help=
        'Store resubmitted requests in memory without storing them in the data file'
    )
    parser.add_argument(
        '-u',
        '--unique',
        action='store_true',
        help=
        'Only resubmit one request per endpoint (different URL parameters are different endpoints)'
    )
    parser.add_argument(
        '-p',
        '--uniquepath',
        action='store_true',
        help='Only resubmit one request per endpoint (ignoring URL parameters)'
    )
    parser.add_argument('-c',
                        '--cookies',
                        nargs='*',
                        help='Apply a cookie to requests before submitting')
    parser.add_argument('-d',
                        '--headers',
                        nargs='*',
                        help='Apply a header to requests before submitting')
    parser.add_argument('-o',
                        '--copycookies',
                        help='Copy the cookies used in another request')
    args = parser.parse_args(shlex.split(line))

    headers = {}
    cookies = {}
    clear_cookies = False

    if args.headers:
        for h in args.headers:
            k, v = h.split('=', 1)
            headers[k] = v

    if args.copycookies:
        reqid = args.copycookies
        req = yield Request.load_request(reqid)
        clear_cookies = True
        for k, v in req.cookies.all_pairs():
            cookies[k] = v

    if args.cookies:
        for c in args.cookies:
            k, v = c.split('=', 1)
            cookies[k] = v

    if args.unique and args.uniquepath:
        raise PappyException('Both -u and -p cannot be given as arguments')

    newsession = Session(cookie_vals=cookies, header_vals=headers)

    reqs = yield load_reqlist(args.reqids)

    for req in reqs:
        if clear_cookies:
            req.cookies.clear()
        newsession.apply_req(req)

    conf_message = "You're about to submit %d requests, continue?" % len(reqs)
    if not confirm(conf_message):
        defer.returnValue(None)

    for r in reqs:
        r.tags.add('resubmitted')

    save = not args.inmem
    yield async_submit_requests(reqs,
                                save=save,
                                save_in_mem=args.inmem,
                                unique_paths=args.uniquepath,
                                unique_path_and_args=args.unique)