Library packet
Facilities for manipulating raw packets.
Author:
Copyright © Same as Nmap--See https://nmap.org/book/man-legal.html
Source: https://svn.nmap.org/nmap/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.
- mac_to_lladdr (mac)
Generate the link-local IPv6 address from the MAC address.
- mactobin (str)
Convert a MAC address string (like
"00:23:ae:5d:3b:10"
) to a raw six-byte long.- 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.
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. - 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. - 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 destination 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 destination 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 (beginning 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.