Ejemplo n.º 1
0
def parse_curl_trace(curl_trace_content):
    if not curl_trace_content:
        return None
    
    log = []
    req = bytearray()
    res = bytearray()
    is_https = False

    blocks = cp.curl_trace_block_iterator(curl_trace_content)
    
    for meta_req_res in cp.curl_trace_reqres_iterator(blocks):
        req_bytes = _extract_req(meta_req_res)
        res_bytes = _extract_res(meta_req_res)

        reqres = parse_binary(req_bytes, res_bytes)

        if not "_meta" in reqres:
            reqres["_meta"] = {}
        reqres["_meta"]["curl_log"] = meta_req_res.meta
        if is_https:
            reqres["request"]["url"] = f"https://{reqres['request']['url']}"
        else:
            reqres["request"]["url"] = f"http://{reqres['request']['url']}"
        
        yield reqres
Ejemplo n.º 2
0
def test_multiline_info():
    inp = bytes(f"""== Info:   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: none
=> Send header, 76 bytes (0x4c)
0000: 47 45 54 20 2f 20 48 54 54 50 2f 32 0d 0a 48 6f GET / HTTP/2..Ho
0010: 73 74 3a 20 77 77 77 2e 67 6f 6f 67 6c 65 2e 63 st: www.google.c
0020: 6f 6d 0d 0a 75 73 65 72 2d 61 67 65 6e 74 3a 20 om..user-agent: 
0030: 63 75 72 6c 2f 37 2e 36 39 2e 31 0d 0a 61 63 63 curl/7.69.1..acc
0040: 65 70 74 3a 20 2a 2f 2a 0d 0a 0d 0a             ept: */*....""",
                encoding="utf-8")

    exp = [
        cp.Block(
            "Send", "header", 76,
            bytes.fromhex(
                b"47 45 54 20 2f 20 48 54 54 50 2f 32 0d 0a 48 6f 73 74 3a 20 77 77 77 2e 67 6f 6f 67 6c 65 2e 63 6f 6d 0d 0a 75 73 65 72 2d 61 67 65 6e 74 3a 20 63 75 72 6c 2f 37 2e 36 39 2e 31 0d 0a 61 63 63 65 70 74 3a 20 2a 2f 2a 0d 0a 0d 0a            "
                .decode("utf-8")),
            [
                "Info:   CAfile: /etc/ssl/certs/ca-certificates.crt\n  CApath: none"
            ])
    ]

    res = list(cp.curl_trace_block_iterator(inp))

    assert res == exp
Ejemplo n.º 3
0
def test_one_of_kind():
    imp = f"""
== Meta info block 1
== Meta info block 2
=> Send SSL data, 5 bytes (0x5)
0000: 16 03 01 02 00                                  .....
<= Recv SSL data, 5 bytes (0x5)
0000: 16 03 03 00 7a                                  ....z
=> Send header, 76 bytes (0x4c)
0000: 47 45 54 20 2f 20 48 54 54 50 2f 32 0d 0a 48 6f GET / HTTP/2..Ho
0010: 73 74 3a 20 77 77 77 2e 67 6f 6f 67 6c 65 2e 63 st: www.google.c
0020: 6f 6d 0d 0a 75 73 65 72 2d 61 67 65 6e 74 3a 20 om..user-agent: 
0030: 63 75 72 6c 2f 37 2e 36 39 2e 31 0d 0a 61 63 63 curl/7.69.1..acc
0040: 65 70 74 3a 20 2a 2f 2a 0d 0a 0d 0a             ept: */*....
=> Send data, 5 bytes (0x5)
0000: 17 03 03 02 21                                  ....!
<= Recv header, 13 bytes (0xd)
0000: 48 54 54 50 2f 32 20 32 30 30 20 0d 0a          HTTP/2 200 ..
<= Recv data, 5 bytes (0x5)
0000: 17 03 03 00 1a                                  .....
    """

    expected = [
        cp.Block("Send", "SSL data", 5,
                 bytes.fromhex(b"16 03 01 02 00".decode("utf-8")),
                 ["Meta info block 1", "Meta info block 2"]),
        cp.Block("Recv", "SSL data", 5,
                 bytes.fromhex(b"16 03 03 00 7a".decode("utf-8"))),
        cp.Block(
            "Send", "header", 76,
            bytes.fromhex(
                b"47 45 54 20 2f 20 48 54 54 50 2f 32 0d 0a 48 6f 73 74 3a 20 77 77 77 2e 67 6f 6f 67 6c 65 2e 63 6f 6d 0d 0a 75 73 65 72 2d 61 67 65 6e 74 3a 20 63 75 72 6c 2f 37 2e 36 39 2e 31 0d 0a 61 63 63 65 70 74 3a 20 2a 2f 2a 0d 0a 0d 0a            "
                .decode("utf-8"))),
        cp.Block("Send", "data", 5,
                 bytes.fromhex(b"17 03 03 02 21".decode("utf-8"))),
        cp.Block(
            "Recv", "header", 13,
            bytes.fromhex(
                b"48 54 54 50 2f 32 20 32 30 30 20 0d 0a".decode("utf-8"))),
        cp.Block("Recv", "data", 5,
                 bytes.fromhex(b"17 03 03 00 1a".decode("utf-8")))
    ]

    res = list(cp.curl_trace_block_iterator(bytes(imp, encoding="utf-8")))

    assert res[0] == expected[0], "0 Block error"
    assert res[1] == expected[1], "1 Block error"
    assert res[2] == expected[2], "2 Block error"
    assert res[3] == expected[3], "3 Block error"
    assert res[4] == expected[4], "4 Block error"
    assert res[5] == expected[5], "5 Block error"
Ejemplo n.º 4
0
def test_reqres_block_iterator():
    imp = bytes(f"""
== Meta info block 1
== Meta info block 2
=> Send SSL data, 5 bytes (0x5)
0000: 16 03 01 02 00                                  .....
<= Recv SSL data, 5 bytes (0x5)
0000: 16 03 03 00 7a                                  ....z
=> Send header, 76 bytes (0x4c)
0000: 47 45 54 20 2f 20 48 54 54 50 2f 32 0d 0a 48 6f GET / HTTP/2..Ho
0010: 73 74 3a 20 77 77 77 2e 67 6f 6f 67 6c 65 2e 63 st: www.google.c
0020: 6f 6d 0d 0a 75 73 65 72 2d 61 67 65 6e 74 3a 20 om..user-agent: 
0030: 63 75 72 6c 2f 37 2e 36 39 2e 31 0d 0a 61 63 63 curl/7.69.1..acc
0040: 65 70 74 3a 20 2a 2f 2a 0d 0a 0d 0a             ept: */*....
=> Send data, 5 bytes (0x5)
0000: 17 03 03 02 21                                  ....!
<= Recv header, 13 bytes (0xd)
0000: 48 54 54 50 2f 32 20 32 30 30 20 0d 0a          HTTP/2 200 ..
<= Recv data, 5 bytes (0x5)
0000: 17 03 03 00 1a                                  .....
    """,
                encoding="utf-8")

    exp = [
        cp.MetaReqRes(["Meta info block 1", "Meta info block 2"], [
            cp.Block(
                "Send", "header", 76,
                bytes.fromhex(
                    b"47 45 54 20 2f 20 48 54 54 50 2f 32 0d 0a 48 6f 73 74 3a 20 77 77 77 2e 67 6f 6f 67 6c 65 2e 63 6f 6d 0d 0a 75 73 65 72 2d 61 67 65 6e 74 3a 20 63 75 72 6c 2f 37 2e 36 39 2e 31 0d 0a 61 63 63 65 70 74 3a 20 2a 2f 2a 0d 0a 0d 0a            "
                    .decode("utf-8"))),
            cp.Block("Send", "data", 5,
                     bytes.fromhex(b"17 03 03 02 21".decode("utf-8")))
        ], [
            cp.Block(
                "Recv", "header", 13,
                bytes.fromhex(b"48 54 54 50 2f 32 20 32 30 30 20 0d 0a".decode(
                    "utf-8"))),
            cp.Block("Recv", "data", 5,
                     bytes.fromhex(b"17 03 03 00 1a".decode("utf-8")))
        ], True)
    ]

    blocks = cp.curl_trace_block_iterator(imp)
    res = list(cp.curl_trace_reqres_iterator(blocks))

    assert res == exp
Ejemplo n.º 5
0
def test_one_of_kind():
    send_ssl_data = str(uuid.uuid4())
    send_header = str(uuid.uuid4())
    recv_ssl_data = str(uuid.uuid4())
    send_data = str(uuid.uuid4())
    recv_header = str(uuid.uuid4())
    recv_data = str(uuid.uuid4())

    imp = f"""
== Meta info block
=> Send SSL data, 5 bytes (0x5)
{send_ssl_data}
<= Recv SSL data, 5 bytes (0x5)
{recv_ssl_data}
=> Send header, 78 bytes (0x4e)
{send_header}
=> Send data, 78 bytes (0x4e)
{send_data}
<= Recv header, 17 bytes (0x11)
{recv_header}
<= Recv data, 2044 bytes (0x7fc)
{recv_data}
    """

    expected = [
        b"== Meta info block",
        bytes(f"=> Send SSL data, 5 bytes (0x5)\n{send_ssl_data}",
              encoding="utf-8"),
        bytes(f"<= Recv SSL data, 5 bytes (0x5)\n{recv_ssl_data}",
              encoding="utf-8"),
        bytes(f"=> Send header, 78 bytes (0x4e)\n{send_header}",
              encoding="utf-8"),
        bytes(f"=> Send data, 78 bytes (0x4e)\n{send_data}", encoding="utf-8"),
        bytes(f"<= Recv header, 17 bytes (0x11)\n{recv_header}",
              encoding="utf-8"),
        bytes(f"<= Recv data, 2044 bytes (0x7fc)\n{recv_data}",
              encoding="utf-8")
    ]

    res = cp.curl_trace_block_iterator(bytes(imp, encoding="utf-8"))

    assert list(res) == expected
Ejemplo n.º 6
0
def parse_curl_trace(curl_trace_content):
    if not curl_trace_content:
        return None
    
    def block_to_bytes(block):
        no_header = b'\n'.join(block.split(b'\n')[1:])
        hex_part = hd.extract_hex_from_curl(no_header)
        return bytes.fromhex(hex_part.decode("utf-8"))

    log = []
    req = bytearray()
    res = bytearray()

    is_https = False
    
    for block in cp.curl_trace_block_iterator(curl_trace_content):
        if block.startswith(b"=="):
            msg = block.decode("utf-8")
            msg = msg.replace("== ", "")
            log.append(msg)
        elif block.startswith(b'=> Send header'): #Send header
            req.extend(block_to_bytes(block))
        elif block.startswith(b'=> Send data'): #Send data
            req.extend(block_to_bytes(block))
        elif block.startswith(b'<= Recv header'): #Recv header
            res.extend(block_to_bytes(block))
        elif block.startswith(b'<= Recv data'): #Recv data
            res.extend(block_to_bytes(block))
        elif block.startswith(b'=> Send SSL'): #Is https
            is_https = True
        else: # not my bussiness
            pass

    reqres = parse_binary(req, res)
    reqres["_meta"]["curl_log"] = log
    if is_https:
        reqres["request"]["url"] = "https://"+reqres["request"]["url"]
    else:
        reqres["request"]["url"] = "http://"+reqres["request"]["url"]
    return reqres
Ejemplo n.º 7
0
def test_none():
    inp = None
    res = cp.curl_trace_block_iterator(inp)

    assert len(list(res)) == 0
Ejemplo n.º 8
0
def test_empty():
    inp = ""
    res = cp.curl_trace_block_iterator(inp)

    assert len(list(res)) == 0