Library packet
Facilities for manipulating raw packets.
Author:
| Marek Majkowski <majek04+nse@gmail.com> |
Copyright© Same as Nmap--See http://nmap.org/book/man-legal.html
Source: http://nmap.org/svn/nselib/packet.lua
Functions
| Frame:build_ether_frame (mac_dst, mac_src, ether_type, packet) |
Build an Ethernet frame. |
| Frame:ether_parse (frame) |
Parse an Ethernet frame. |
| in_cksum (b) |
Calculate a standard Internet checksum. |
| ip6tobin (str) |
Convert an IPv6 address string (like |
| iptobin (str) |
Convert a dotted-quad IP address string (like |
| ipv6_hdr_pack_tc_fl (ip6_tc, ip6_fl) |
Convert Version, Traffic Class and Flow Label to a 4-byte string. |
| mac_to_lladdr (mac) |
Generate the link-local IPv6 address from the MAC address. |
| mactobin (str) |
Convert a MAC address string (like |
| numtostr16 (num) |
Get a 2-byte string from a number. (big-endian) |
| numtostr32 (num) |
Get a 4-byte string from a number. (big-endian) |
| numtostr8 (num) |
Get a 1-byte string from a number. |
| Packet:build_icmp_echo_request (id, seq, data, mac_src, mac_dst, ip_bin_src, ip_bin_dst, dsf) |
Build an ICMP Echo Request frame. |
| Packet:build_icmp_header (icmp_type, icmp_code, icmp_payload, ip_bin_src, ip_bin_dst) |
Build an ICMP header. |
| Packet:build_icmpv6_echo_request (id, sequence, data, mac_src, mac_dst, ip_bin_src, ip_bin_dst, tc, fl, hop_limit) |
Build an ICMPv6 Echo Request frame. |
| Packet:build_icmpv6_header (icmpv6_type, icmpv6_code, icmpv6_payload, ip_bin_src, ip_bin_dst) |
Build an ICMPv6 header. |
| Packet:build_ip_packet (src, dst, payload, dsf, id, flags, off, ttl, proto) |
Build an IPv4 packet. |
| Packet:build_ipv6_packet (src, dst, nx_hdr, payload, h_limit, t_class, f_label, dsr) |
Build an IPv6 packet. |
| Packet:count_ipv6_pseudoheader_cksum () |
Count IPv6 checksum. |
| Packet:icmp_parse (force_continue) |
Parse an ICMP packet header. |
| Packet:icmp_tostring () |
Get a short string representation of the ICMP header. |
| Packet:icmpv6_parse (force_continue) |
Parse an ICMPv6 packet header. |
| Packet:ip6_parse (force_continue) |
Parse an IPv6 packet header. |
| Packet:ip6_set_plen (plen) |
Set the payload length field. |
| Packet:ip_count_checksum () |
Count checksum for packet and save it. |
| Packet:ip_parse (force_continue) |
Parse an IP packet header. |
| Packet:ip_set_bin_dst (binip) |
Set the destination IP address. |
| Packet:ip_set_bin_src (binip) |
Set the source IP address. |
| Packet:ip_set_checksum (checksum) |
Set the checksum. |
| Packet:ip_set_hl (len) |
Set the header length field. |
| Packet:ip_set_id (id) |
Set the packet identification field. |
| Packet:ip_set_len (len) |
Set the packet length field. |
| Packet:ip_set_options (ipoptions) |
Set the IP options field (and move the data, count new length, etc.). |
| Packet:ip_set_ttl (ttl) |
Set the TTL. |
| Packet:ip_tostring () |
Get a short string representation of the IP header. |
| Packet:ipv6_ext_header_parse (force_continue) |
Pare an IPv6 extension header. Just jump over it at the moment. |
| Packet:ipv6_is_extension_header (nhdr) |
Return true if and only if the next header is an known extension header. |
| Packet:new (packet, packet_len, force_continue) |
Create a new Packet object. |
| Packet:parse_options (offset, length) |
Parse IP/TCP options into a table. |
| Packet:raw (index, length) |
Return part of the packet contents as a byte string. |
| Packet:set_icmp6_cksum (check_sum) |
Set ICMPv6 checksum. |
| Packet:set_icmpv6_option (opt_type, msg) |
Set an ICMPv6 option message. |
| Packet:set_u16 (index, num) |
Set a 16-bit integer at a 0-based byte offset in the packet. (big-endian). |
| Packet:set_u32 (index, num) |
Set a 32-bit integer at a 0-based byte offset in the packet. (big-endian). |
| Packet:set_u8 (index, num) |
Set an 8-bit integer at a 0-based byte offset in the packet. (big-endian). |
| Packet:tcp_count_checksum () |
Count and save the TCP checksum field. |
| Packet:tcp_lookup_link () |
Map an MTU to a link type string. Stolen from p0f. |
| Packet:tcp_parse (force_continue) |
Parse a TCP packet header. |
| Packet:tcp_parse_options () |
Parse options for TCP header. |
| Packet:tcp_set_checksum (checksum) |
Set the TCP checksum field. |
| Packet:tcp_set_dport (port) |
Set the TCP destination port. |
| Packet:tcp_set_flags (new_flags) |
Set the TCP flags field (like SYN, ACK, RST). |
| Packet:tcp_set_seq (new_seq) |
Set the TCP sequence field. |
| Packet:tcp_set_sport (port) |
Set the TCP source port. |
| Packet:tcp_set_urp (urg_ptr) |
Set the urgent pointer field. |
| Packet:tcp_tostring () |
Get a short string representation of the TCP packet. |
| Packet:tostring () |
Get a short string representation of the packet. |
| Packet:u16 (index) |
Get a 16-bit integer at a 0-based byte offset in the packet. |
| Packet:u32 (index) |
Get a 32-bit integer at a 0-based byte offset in the packet. |
| Packet:u8 (index) |
Get an 8-bit integer at a 0-based byte offset in the packet. |
| Packet:udp_count_checksum () |
Count and save the UDP checksum field. |
| Packet:udp_parse (force_continue) |
Parse a UDP packet header. |
| Packet:udp_set_checksum (checksum) |
Set the UDP checksum field. |
| Packet:udp_set_dport (port) |
Set the UDP destination port. |
| Packet:udp_set_length (len) |
Set the UDP payload length. |
| Packet:udp_set_sport (port) |
Set the UDP source port. |
| Packet:udp_tostring () |
Get a short string representation of the UDP packet. |
| set_u16 (b, i, num) |
Set a 16-bit integer at a 0-based byte offset in a byte string (big-endian). |
| set_u32 (b, i, num) |
Set a 32-bit integer at a 0-based byte offset in a byte string (big-endian). |
| set_u8 (b, i, num) |
Set an 8-bit integer at a 0-based byte offset in a byte string (big-endian). |
| toip (raw_ip_addr) |
Convert a four-byte raw string to a dotted-quad IP address string. |
| toipv6 (raw_ipv6_addr) |
Convert a 16-byte raw string to an IPv6 address string. |
| u16 (b, i) |
Get a 16-bit integer at a 0-based byte offset in a byte string. |
| u32 (b, i) |
Get a 32-bit integer at a 0-based byte offset in a byte string. |
| u8 (b, i) |
Get an 8-bit integer at a 0-based byte offset in a byte string. |
Functions
- Frame:build_ether_frame (mac_dst, mac_src, ether_type, packet)
-
Build an Ethernet frame.
Parameters
- mac_dst: six-byte string of the destination MAC address.
- mac_src: six-byte string of the source MAC address.
- ether_type: two-byte string of the type.
- packet: string of the payload.
Return value:
frame string of the Ether frame. - Frame:ether_parse (frame)
-
Parse an Ethernet frame.
Parameters
- frame: string of the Ether frame.
Return values:
- mac_dst six-byte string of the destination MAC address.
- mac_src six-byte string of the source MAC address.
- packet string of the payload.
- in_cksum (b)
-
Calculate a standard Internet checksum.
Parameters
- b: Data to checksum.
Return value:
Checksum. - ip6tobin (str)
-
Convert an IPv6 address string (like
"fe80:21::1") to a raw string 16 bytes long (big-endian).Parameters
- str: IPv6 address string.
Return value:
16-byte string. - iptobin (str)
-
Convert a dotted-quad IP address string (like
"1.2.3.4") to a raw string four bytes long.Parameters
- str: IP address string.
Return value:
Four-byte string. - ipv6_hdr_pack_tc_fl (ip6_tc, ip6_fl)
-
Convert Version, Traffic Class and Flow Label to a 4-byte string.
Parameters
- ip6_tc: Number stands for Traffic Class.
- ip6_fl: Number stands for Flow Label.
Return value:
The first four-byte string of an IPv6 header. - mac_to_lladdr (mac)
-
Generate the link-local IPv6 address from the MAC address.
Parameters
- mac: MAC address string.
Return value:
Link-local IPv6 address string. - mactobin (str)
-
Convert a MAC address string (like
"00:23:ae:5d:3b:10") to a raw six-byte long.Parameters
- str: MAC address string.
Return value:
Six-byte string. - numtostr16 (num)
-
Get a 2-byte string from a number. (big-endian)
Parameters
- num: A number.
- numtostr32 (num)
-
Get a 4-byte string from a number. (big-endian)
Parameters
- num: A number.
- numtostr8 (num)
-
Get a 1-byte string from a number.
Parameters
- num: A number.
- Packet:build_icmp_echo_request (id, seq, data, mac_src, mac_dst, ip_bin_src, ip_bin_dst, dsf)
-
Build an ICMP Echo Request frame.
Parameters
- id: integer that represents Echo ID.
- seq: integer that represents Echo sequence.
- data: string of Echo data.
- mac_src: six-byte string of source MAC address.
- mac_dst: sis-byte string of destination MAC address.
- ip_bin_src: 16-byte string of source IPv6 address.
- ip_bin_dst: 16-byte string of destinatiion IPv6 address.
- dsf: integer that represents differentiated services field.
- Packet:build_icmp_header (icmp_type, icmp_code, icmp_payload, ip_bin_src, ip_bin_dst)
-
Build an ICMP header.
Parameters
- icmp_type: integer that represent ICMPv6 type.
- icmp_code: integer that represent ICMPv6 code.
- icmp_payload: string of the payload
- ip_bin_src: 16-byte string of the source IPv6 address.
- ip_bin_dst: 16-byte string of the destination IPv6 address.
- Packet:build_icmpv6_echo_request (id, sequence, data, mac_src, mac_dst, ip_bin_src, ip_bin_dst, tc, fl, hop_limit)
-
Build an ICMPv6 Echo Request frame.
Parameters
- id: integer that represents Echo ID.
- sequence: integer that represents Echo sequence.
- data: string of Echo data.
- mac_src: six-byte string of source MAC address.
- mac_dst: sis-byte string of destination MAC address.
- ip_bin_src: 16-byte string of source IPv6 address.
- ip_bin_dst: 16-byte string of destinatiion IPv6 address.
- tc: integer that represents traffic class of IPv6 packet.
- fl: integer that represents flow label of IPv6 packet.
- hop_limit:
- Packet:build_icmpv6_header (icmpv6_type, icmpv6_code, icmpv6_payload, ip_bin_src, ip_bin_dst)
-
Build an ICMPv6 header.
Parameters
- icmpv6_type: integer that represent ICMPv6 type.
- icmpv6_code: integer that represent ICMPv6 code.
- icmpv6_payload: string of the payload
- ip_bin_src: 16-byte string of the source IPv6 address.
- ip_bin_dst: 16-byte string of the destination IPv6 address.
- Packet:build_ip_packet (src, dst, payload, dsf, id, flags, off, ttl, proto)
-
Build an IPv4 packet.
Parameters
- src: 4-byte string of the source IP address.
- dst: 4-byte string of the destination IP address.
- payload: string containing the IP payload
- dsf: byte that represents the differentiated services field
- id: integer that represents the IP identification
- flags: integer that represents the IP flags
- off: integer that represents the IP offset
- ttl: integer that represent the IP time to live
- proto: integer that represents the IP protocol
- Packet:build_ipv6_packet (src, dst, nx_hdr, payload, h_limit, t_class, f_label, dsr)
-
Build an IPv6 packet.
Parameters
- src: 16-byte string of the source IPv6 address.
- dst:
- nx_hdr: integer that represents next header.
- payload:
- h_limit: integer that represents hop limit.
- t_class: integer that represents traffic class.
- f_label: integer that represents flow label.
- dsr: 16-byte string of the destination IPv6 address.
- Packet:count_ipv6_pseudoheader_cksum ()
-
Count IPv6 checksum.
Return value:
the checksum. - Packet:icmp_parse (force_continue)
-
Parse an ICMP packet header.
Parameters
- force_continue: Ignored.
Return value:
Whether the parsing succeeded. - Packet:icmp_tostring ()
-
Get a short string representation of the ICMP header.
Return value:
A string representation of the ICMP header. - Packet:icmpv6_parse (force_continue)
-
Parse an ICMPv6 packet header.
Parameters
- force_continue: Ignored.
Return value:
Whether the parsing succeeded. - Packet:ip6_parse (force_continue)
-
Parse an IPv6 packet header.
Parameters
- force_continue: Ignored.
Return value:
Whether the parsing succeeded. - Packet:ip6_set_plen (plen)
-
Set the payload length field.
Parameters
- plen: Payload length.
- Packet:ip_count_checksum ()
-
Count checksum for packet and save it.
- Packet:ip_parse (force_continue)
-
Parse an IP packet header.
Parameters
- force_continue: Ignored.
Return value:
Whether the parsing succeeded. - Packet:ip_set_bin_dst (binip)
-
Set the destination IP address.
Parameters
- binip: The destination IP address as a byte string.
- Packet:ip_set_bin_src (binip)
-
Set the source IP address.
Parameters
- binip: The source IP address as a byte string.
- Packet:ip_set_checksum (checksum)
-
Set the checksum.
Parameters
- checksum: Checksum.
- Packet:ip_set_hl (len)
-
Set the header length field.
Parameters
- len:
- Packet:ip_set_id (id)
-
Set the packet identification field.
Parameters
- id: packet ID.
- Packet:ip_set_len (len)
-
Set the packet length field.
Parameters
- len: Packet length.
- Packet:ip_set_options (ipoptions)
-
Set the IP options field (and move the data, count new length, etc.).
Parameters
- ipoptions: IP options.
- Packet:ip_set_ttl (ttl)
-
Set the TTL.
Parameters
- ttl: TTL.
- Packet:ip_tostring ()
-
Get a short string representation of the IP header.
Return value:
A string representation of the IP header. - Packet:ipv6_ext_header_parse (force_continue)
-
Pare an IPv6 extension header. Just jump over it at the moment.
Parameters
- force_continue: Ignored.
Return value:
Whether the parsing succeeded. - Packet:ipv6_is_extension_header (nhdr)
-
Return true if and only if the next header is an known extension header.
Parameters
- nhdr: Next header.
- Packet:new (packet, packet_len, force_continue)
-
Create a new Packet object.
Parameters
- packet: Binary string with packet data.
-
packet_len:
Packet length. It could be more than
#packet. - force_continue: whether an error in parsing headers should be fatal or not. This is especially useful when parsing ICMP packets, where a small ICMP payload could be a TCP header. The problem is that parsing this payload normally would fail because the TCP header is too small.
Return value:
A new Packet. - Packet:parse_options (offset, length)
-
Parse IP/TCP options into a table.
Parameters
- offset: Offset at which options start.
- length: Length of options.
Return value:
Table of options. - Packet:raw (index, length)
-
Return part of the packet contents as a byte string.
Parameters
- index: The beginning of the part of the packet to extract. The index is 0-based. If omitted the default value is 0 (begining of the string)
- length: The length of the part of the packet to extract. If omitted the remaining contents from index to the end of the string are returned.
Return value:
A string. - Packet:set_icmp6_cksum (check_sum)
-
Set ICMPv6 checksum.
Parameters
- check_sum:
- Packet:set_icmpv6_option (opt_type, msg)
-
Set an ICMPv6 option message.
Parameters
- opt_type:
- msg:
- Packet:set_u16 (index, num)
-
Set a 16-bit integer at a 0-based byte offset in the packet. (big-endian).
Parameters
- index: Offset.
- num: Integer to store.
- Packet:set_u32 (index, num)
-
Set a 32-bit integer at a 0-based byte offset in the packet. (big-endian).
Parameters
- index: Offset.
- num: Integer to store.
- Packet:set_u8 (index, num)
-
Set an 8-bit integer at a 0-based byte offset in the packet. (big-endian).
Parameters
- index: Offset.
- num: Integer to store.
- Packet:tcp_count_checksum ()
-
Count and save the TCP checksum field.
- Packet:tcp_lookup_link ()
-
Map an MTU to a link type string. Stolen from p0f.
Return value:
A string describing the link type. - Packet:tcp_parse (force_continue)
-
Parse a TCP packet header.
Parameters
- force_continue: Whether a short packet causes parsing to fail.
Return value:
Whether the parsing succeeded. - Packet:tcp_parse_options ()
-
Parse options for TCP header.
- Packet:tcp_set_checksum (checksum)
-
Set the TCP checksum field.
Parameters
- checksum: Checksum.
- Packet:tcp_set_dport (port)
-
Set the TCP destination port.
Parameters
- port: Destination port.
- Packet:tcp_set_flags (new_flags)
-
Set the TCP flags field (like SYN, ACK, RST).
Parameters
- new_flags: Flags, represented as an 8-bit number.
- Packet:tcp_set_seq (new_seq)
-
Set the TCP sequence field.
Parameters
- new_seq: Sequence.
- Packet:tcp_set_sport (port)
-
Set the TCP source port.
Parameters
- port: Source port.
- Packet:tcp_set_urp (urg_ptr)
-
Set the urgent pointer field.
Parameters
- urg_ptr: Urgent pointer.
- Packet:tcp_tostring ()
-
Get a short string representation of the TCP packet.
Return value:
A string representation of the TCP header. - Packet:tostring ()
-
Get a short string representation of the packet.
Return value:
A string representation of the packet. - Packet:u16 (index)
-
Get a 16-bit integer at a 0-based byte offset in the packet.
Parameters
- index: Offset.
Return value:
A 16-bit integer. - Packet:u32 (index)
-
Get a 32-bit integer at a 0-based byte offset in the packet.
Parameters
- index: Offset.
Return value:
An 32-bit integer. - Packet:u8 (index)
-
Get an 8-bit integer at a 0-based byte offset in the packet.
Parameters
- index: Offset.
Return value:
An 8-bit integer. - Packet:udp_count_checksum ()
-
Count and save the UDP checksum field.
- Packet:udp_parse (force_continue)
-
Parse a UDP packet header.
Parameters
- force_continue: Whether a short packet causes parsing to fail.
Return value:
Whether the parsing succeeded. - Packet:udp_set_checksum (checksum)
-
Set the UDP checksum field.
Parameters
- checksum: Checksum.
- Packet:udp_set_dport (port)
-
Set the UDP destination port.
Parameters
- port: Destination port.
- Packet:udp_set_length (len)
-
Set the UDP payload length.
Parameters
- len: UDP payload length.
- Packet:udp_set_sport (port)
-
Set the UDP source port.
Parameters
- port: Source port.
- Packet:udp_tostring ()
-
Get a short string representation of the UDP packet.
Return value:
A string representation of the UDP header. - set_u16 (b, i, num)
-
Set a 16-bit integer at a 0-based byte offset in a byte string (big-endian).
Parameters
- b: A byte string.
- i: Offset.
- num: Integer to store.
- set_u32 (b, i, num)
-
Set a 32-bit integer at a 0-based byte offset in a byte string (big-endian).
Parameters
- b: A byte string.
- i: Offset.
- num: Integer to store.
- set_u8 (b, i, num)
-
Set an 8-bit integer at a 0-based byte offset in a byte string (big-endian).
Parameters
- b: A byte string.
- i: Offset.
- num: Integer to store.
- toip (raw_ip_addr)
-
Convert a four-byte raw string to a dotted-quad IP address string.
Parameters
- raw_ip_addr: Four-byte string.
Return value:
IP address string. - toipv6 (raw_ipv6_addr)
-
Convert a 16-byte raw string to an IPv6 address string.
Parameters
- raw_ipv6_addr: 16-byte string.
Return value:
IPv6 address string. - u16 (b, i)
-
Get a 16-bit integer at a 0-based byte offset in a byte string.
Parameters
- b: A byte string.
- i: Offset.
Return value:
A 16-bit integer. - u32 (b, i)
-
Get a 32-bit integer at a 0-based byte offset in a byte string.
Parameters
- b: A byte string.
- i: Offset.
Return value:
A 32-bit integer. - u8 (b, i)
-
Get an 8-bit integer at a 0-based byte offset in a byte string.
Parameters
- b: A byte string.
- i: Offset.
Return value:
An 8-bit integer.


