Beispiel #1
0
/*-
- ptag = n:ipv4{len=int, protocol=int, src=ipaddr, dst=ipaddr, payload=str, ptag=int, options=ip_options}

ptag is optional, defaults to creating a new protocol block
options is optional
*/
static int lnet_ipv4 (lua_State *L)
{
    libnet_t** ud = luaL_checkudata(L, 1, L_NET_REGID);
    luaL_argcheck(L, *ud, 1, "net has been destroyed");

    int len = v_arg_integer(L, 2, "len"); // FIXME - should be optional!
    int tos = 0;
    int id = 0;
    int offset = 0;
    int ttl = 64;
    int protocol = v_arg_integer(L, 2, "protocol");
    int cksum = 0; // 0 is a flag requesting libnet to fill in correct cksum
    const char* src = v_arg_string(L, 2, "src");
    const char* dst = v_arg_string(L, 2, "dst");
    size_t payloadsz = 0;
    const char* payload = v_arg_lstring(L, 2, "payload", &payloadsz, "");
    int ptag = lnet_arg_ptag(L, 2);
    int options_ptag = 0;
    size_t optionsz = 0;
    const char* options = v_arg_lstring(L, 2, "options", &optionsz, "");

    if(payloadsz == 0) {
        payload = NULL;
    }

#ifdef NET_DUMP
    printf("net ipv4 src %s dst %s len %d payloadsz %lu ptag %d optionsz %lu\n", src, dst, len, payloadsz, ptag, optionsz);
#endif

    uint32_t src_n = check_ip_pton(L, src, "src");
    uint32_t dst_n = check_ip_pton(L, dst, "dst");

    if(ptag) {
        /* Modifying exist IPv4 packet, so find the preceeding options block (we
         * _always_ push an options block, perhaps empty, to make this easy).
         */
        libnet_pblock_t* p = libnet_pblock_find(*ud, ptag);

        if(!p)
            return check_error(L, *ud, -1);

        options_ptag = p->prev->ptag;
    }

#ifdef NET_DUMP
    printf("  options_ptag %d optionsz %lu\n", options_ptag, optionsz);
#endif

    options_ptag = libnet_build_ipv4_options((uint8_t*) options,
            optionsz, *ud, options_ptag);

    check_error(L, *ud, options_ptag);

    ptag = libnet_build_ipv4(len, tos, id, offset, ttl, protocol, cksum, src_n,
            dst_n, (uint8_t*) payload, payloadsz, *ud, ptag);
    check_error(L, *ud, ptag);
    lua_pushinteger(L, ptag);
    return 1;
}
Beispiel #2
0
/*-
-- ptag = net:udp{src=NUM, dst=NUM, len=NUM, payload=STR, ptag=int}

Build UDP packet inside net context.

ptag is optional, defaults to creating a new protocol block
*/
static int lnet_udp (lua_State *L)
{
    libnet_t* ud = checkudata(L);
    int src = v_arg_integer(L, 2, "src");
    int dst = v_arg_integer(L, 2, "dst");
    uint32_t payloadsz = 0;
    const uint8_t* payload = checkpayload(L, 2, &payloadsz);
    int len = v_arg_integer_opt(L, 2, "len", LIBNET_UDP_H + payloadsz);
    int cksum = 0;
    int ptag = lnet_arg_ptag(L, ud, 2, LIBNET_PBLOCK_UDP_H);

    ptag = libnet_build_udp(src, dst, len, cksum, payload, payloadsz, ud, ptag);
    check_error(L, ud, ptag);
    lua_pushinteger(L, ptag);
    return 1;
}
Beispiel #3
0
/*-
-- ptag = net:igmp{type=NUM, code=NUM, ip=IP, payload=STR, ptag=int}

Build IGMP packet inside net context.

ptag is optional, defaults to creating a new protocol block
*/
static int lnet_igmp (lua_State *L)
{
    libnet_t* ud = checkudata(L);
    int type = v_arg_integer(L, 2, "type");
    int code = v_arg_integer(L, 2, "code");
    const char* ip = v_arg_string(L, 2, "ip");
    uint32_t ip_n = check_ip_pton(L, ip, "ip");
    uint32_t payloadsz = 0;
    const uint8_t* payload = checkpayload(L, 2, &payloadsz);
    int cksum = 0;
    int ptag = lnet_arg_ptag(L, ud, 2, LIBNET_PBLOCK_IGMP_H);

    ptag = libnet_build_igmp(type, code, cksum, ip_n, payload, payloadsz, ud, ptag);
    check_error(L, ud, ptag);
    lua_pushinteger(L, ptag);
    return 1;
}
Beispiel #4
0
/*-
- ptag = net:udp{src=NUM, dst=NUM, len=NUM, payload=STR, ptag=int}

Build UDP packet inside net context.

ptag is optional, defaults to creating a new protocol block
*/
static int lnet_udp (lua_State *L)
{
    libnet_t** ud = luaL_checkudata(L, 1, L_NET_REGID);
    luaL_argcheck(L, *ud, 1, "net has been destroyed");

    int src = v_arg_integer(L, 2, "src");
    int dst = v_arg_integer(L, 2, "dst");

    size_t payloadsz = 0;
    const char* payload = v_arg_lstring(L, 2, "payload", &payloadsz, "");
    int len = v_arg_integer_opt(L, 2, "len", LIBNET_UDP_H + payloadsz);
    int cksum = 0;
    int ptag = lnet_arg_ptag(L, 2);

    if(payloadsz == 0) {
        payload = NULL;
    }

    ptag = libnet_build_udp(src, dst, len, cksum, (uint8_t*)payload, payloadsz, *ud, ptag);
    check_error(L, *ud, ptag);
    lua_pushinteger(L, ptag);
    return 1;
}
Beispiel #5
0
/*-
-- ptag = n:ipv4{
    -- required arguments
      src=ipaddr,
      dst=ipaddr,
      protocol=int,
    -- optional arguments
      ptag=int,
      payload=str,
      options=ip_options,
      len=int, -- default is correct length
      tos=int,
      id=int,
      frag=int,
      ttl=int, -- defaults to 64
  }

ptag is optional, defaults to creating a new protocol block
options is optional
*/
static int lnet_ipv4 (lua_State *L)
{
    libnet_t* ud = checkudata(L);
    const char* src = v_arg_string(L, 2, "src");
    const char* dst = v_arg_string(L, 2, "dst");
    uint32_t src_n = check_ip_pton(L, src, "src");
    uint32_t dst_n = check_ip_pton(L, dst, "dst");
    int protocol = v_arg_integer(L, 2, "protocol"); /* TODO make optional */
    int ptag = lnet_arg_ptag(L, ud, 2, LIBNET_PBLOCK_IPV4_H);
    uint32_t payloadsz = 0;
    const uint8_t* payload = checkpayload(L, 2, &payloadsz);
    int options_ptag = 0;
    uint32_t optionsz = 0;
    const uint8_t* options = checklbuffer(L, 2, "options", &optionsz);
    int len = v_arg_integer_opt(L, 2, "len", -1);
    int tos = v_arg_integer_opt(L, 2, "tos", 0);
    int id = v_arg_integer_opt(L, 2, "id", 0);
    int frag = v_arg_integer_opt(L, 2, "frag", 0);
    int ttl = v_arg_integer_opt(L, 2, "ttl", 64);
    int cksum = 0; /* 0 is a flag requesting libnet to fill in correct cksum */
    libnet_pblock_t* oblock = NULL;

#ifdef NET_DUMP
    printf("net ipv4 src %s dst %s len %d payloadsz %lu ptag %d optionsz %lu\n", src, dst, len, payloadsz, ptag, optionsz);
#endif

    oblock = ptag ? libnet_pblock_find(ud, ptag)->prev : ud->pblock_end;

    if(!oblock || oblock->type != LIBNET_PBLOCK_IPO_H)
      oblock = NULL;
    else
      options_ptag = oblock->ptag;

#ifdef NET_DUMP
    printf("  options_ptag %d optionsz from %lu to %lu\n",
            options_ptag, oblock ? oblock->b_len : 0, optionsz);
#endif

    /* Two initial states possible:
     *   - has prev ip options block, or not
     * Two final states desired:
     *   - has prev ip options block, or not
     */

    if(!options) {
      libnet_pblock_delete(ud, oblock);
    } else {
      options_ptag = libnet_build_ipv4_options(options, optionsz, ud, options_ptag);

      check_error(L, ud, options_ptag);

      if(oblock) {
	/* we replaced an existing block that was correctly placed */
      } else if(ptag) {
	libnet_pblock_insert_before(ud, ptag, options_ptag);
      } else {
          /* we just pushed a new options block, and are about to push a new ip block */
      }
    }

    /* If len unspecified, rewrite it to be len of ipv4 pblock + previous blocks. */
    /* FIXME I don't think defaulting to end is correct

-- libnet doesn't have a generic icmp construction api, see bug#1373
local function build_icmp(n, icmp)
    local typecode = string.char(assert(icmp.type), assert(icmp.code))
    local data = icmp.data or ""
    local checksum = net.checksum(typecode, "\0\0", data)
    local packet = typecode..checksum..data

    return n:ipv4{
        src      = arg.localip,
        dst      = arg.dutip,
        protocol = 1, -- ICMP is protocol 1 FIXME get from iana.ip.types.icmp
        payload  = packet,
        len      = 20 + #packet,
        ptag     = icmp.ptag
    }
end

getmetatable(n).icmp = build_icmp

-- set up the pblock stack, top to bottom
local ptag = n:icmp{type=0, code=0}
n:eth{src=arg.localmac, dst=arg.dutmac}

   n:icmp{ptag=ptag, type=type, code=code, payload=data}

print(n:dump())
print(n:get_ipv4())


~/w/wt/achilles-engine/data/Plugins/Grammar % sudo ./icmp-data-grammar-l2 dutip=1.1.1.1 localdev=lo localip=2.2.2.2 dutmac=11:11:11:11:11:11 localmac=22:22:22:22:22:22 pcap=pc.pcap
tag 2 flags 0 type ipdata/0xf buf 0x6541e0 b_len  4 h_len  4 copied 4 prev -1 next 1
tag 1 flags 1 type ipv4/0xd buf 0x6582f0 b_len 20 h_len 20 copied 20 prev 2 next 3
tag 3 flags 0 type eth/0x4 buf 0x647580 b_len 14 h_len  0 copied 14 prev 1 next -1
link_offset 14 aligner 0 total_size 38 nblocks 3

Total:1
Subtest 1: ICMP type 0 code 1 with payload size 1
tag 2 flags 0 type ipdata/0xf buf 0x6541e0 b_len  4 h_len  4 copied 4 prev -1 next 1
tag 1 flags 1 type ipv4/0xd buf 0x6582f0 b_len 20 h_len 20 copied 20 prev 2 next 3
tag 3 flags 0 type eth/0x4 buf 0x647580 b_len 14 h_len  0 copied 14 prev 1 next -1
link_offset 14 aligner 0 total_size 38 nblocks 3

{
ptag = 1, protocol = 1, _iphl = 5, id = 0, options = "", dst = "1.1.1.1", src = "2.2.2.2", _sum = 0, _ipv = 4, tos = 0, _len = 28, ttl = 64, frag = 0
}


============>> note that _len is 28, it should be 24
    
    */
    if(len < 0) {
        libnet_pblock_t* p = ptag ? libnet_pblock_find(ud, ptag)->prev : ud->pblock_end;

        len = LIBNET_IPV4_H + payloadsz;

        while(p) {
            len += p->b_len;
            p = p->prev;
        }
    }

    ptag = libnet_build_ipv4(
            len, tos, id, frag, ttl, protocol, cksum,
            src_n, dst_n,
            payload, payloadsz,
            ud, ptag);

    check_error(L, ud, ptag);

    lua_pushinteger(L, ptag);

    return 1;
}
Beispiel #6
0
/*-
-- ptag = n:tcp{
    -- required arguments
      src=port,
      dst=port,
      seq=int,
      ack=int,
      flags=int,
      win=int,
      urg=int,
    -- optional arguments
      ptag=int,
      payload=str,
      options=tcp_options,
  }

ptag is optional, defaults to creating a new protocol block
options is optional
*/
static int lnet_tcp (lua_State *L)
{
    libnet_t* ud = checkudata(L);
    int src = v_arg_integer(L, 2, "src");
    int dst = v_arg_integer(L, 2, "dst");
    int seq = v_arg_integer(L, 2, "seq");
    int ack = v_arg_integer(L, 2, "ack");
    int flags = v_arg_integer(L, 2, "flags");
    int win = v_arg_integer(L, 2, "win");
    int urg = v_arg_integer(L, 2, "urg");
    int ptag = lnet_arg_ptag(L, ud, 2, LIBNET_PBLOCK_TCP_H);
    uint32_t payloadsz = 0;
    const uint8_t* payload = checkpayload(L, 2, &payloadsz);
    int options_ptag = 0;
    uint32_t optionsz = 0;
    const uint8_t* options = checklbuffer(L, 2, "options", &optionsz);
    int cksum = 0; /* 0 is a flag requesting libnet to fill in correct cksum */
    libnet_pblock_t* oblock = NULL;
    int len = 0; /* libnet needs len for checksum calculation */

    oblock = ptag ? libnet_pblock_find(ud, ptag)->prev : ud->pblock_end;

    if(!oblock || oblock->type != LIBNET_PBLOCK_TCPO_H)
      oblock = NULL;
    else
      options_ptag = oblock->ptag;

    /* Two initial states possible:
     *   - has prev ip options block, or not
     * Two final states desired:
     *   - has prev ip options block, or not
     */

    if(!options) {
      libnet_pblock_delete(ud, oblock);
    } else {
      options_ptag = libnet_build_tcp_options(options, optionsz, ud, options_ptag);

      check_error(L, ud, options_ptag);

      if(oblock) {
	/* we replaced an existing block that was correctly placed */
      } else if(ptag) {
	libnet_pblock_insert_before(ud, ptag, options_ptag);
      } else {
          /* we just pushed a new options block, and are about to push a new ip block */
      }
    }

    /* Rewrite len to be len of tcp pblock + previous blocks. */
    {
        libnet_pblock_t* p = ptag ? libnet_pblock_find(ud, ptag)->prev : ud->pblock_end;

        len = LIBNET_TCP_H + payloadsz;

        while(p) {
            /* don't count tcpdata pblock... we will replace it payloadsz data below */
            if(p->type != LIBNET_PBLOCK_TCPDATA)
                len += p->b_len;
            p = p->prev;
        }
    }

    ptag = libnet_build_tcp(
            src, dst, seq, ack, flags, win, cksum, urg,
            len,
            payload, payloadsz,
            ud, ptag);

    check_error(L, ud, ptag);

    lua_pushinteger(L, ptag);

    return 1;
}