OpenVAS Scanner 21.4.1
Data Structures | Macros | Functions | Variables
nasl_packet_forgery.c File Reference
#include "nasl_packet_forgery.h"
#include "../misc/bpf_share.h"
#include "../misc/pcap_openvas.h"
#include "../misc/plugutils.h"
#include "capture_packet.h"
#include "exec.h"
#include "nasl_debug.h"
#include "nasl_func.h"
#include "nasl_global_ctxt.h"
#include "nasl_lex_ctxt.h"
#include "nasl_packet_forgery_v6.h"
#include "nasl_raw.h"
#include "nasl_socket.h"
#include "nasl_tree.h"
#include "nasl_var.h"
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <pcap.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>

Data Structures

struct  pseudohdr
 
struct  tcp_opt_mss
 
struct  tcp_opt_wscale
 
struct  tcp_opt_sack_perm
 
struct  tcp_opt_tstamp
 
struct  tcp_options
 
struct  pseudo_udp_hdr
 
struct  igmp
 

Macros

#define FIX(n)   htons (n)
 
#define UNFIX(n)   ntohs (n)
 
#define rnd_tcp_port()   (rand () % 65535 + 1024)
 

Functions

static int np_in_cksum (u_short *p, int n)
 
tree_cellforge_ip_packet (lex_ctxt *lexic)
 Forge an IP datagram. More...
 
tree_cellget_ip_element (lex_ctxt *lexic)
 Extracts a field from an IP datagram. More...
 
tree_cellset_ip_elements (lex_ctxt *lexic)
 Modify the fields of a datagram. More...
 
tree_cellinsert_ip_options (lex_ctxt *lexic)
 Add option datagram. More...
 
tree_celldump_ip_packet (lex_ctxt *lexic)
 Dump IP datagrams. More...
 
struct pseudohdr __attribute__ ((packed))
 
tree_cellforge_tcp_packet (lex_ctxt *lexic)
 Fills an IP datagram with TCP data. Note that the ip_p field is not updated. It returns the modified IP datagram. Its arguments are: More...
 
tree_cellget_tcp_element (lex_ctxt *lexic)
 Extracts TCP field from an IP datagram. More...
 
static void get_tcp_options (char *options, struct tcp_options *tcp_all_options)
 Extract all TCP option from an IP datagram. More...
 
tree_cellget_tcp_option (lex_ctxt *lexic)
 Get a TCP option from an IP datagram if present. Possible options are: TCPOPT_MAXSEG (2), values between 536 and 65535 TCPOPT_WINDOW (3), with values between 0 and 14 TCPOPT_SACK_PERMITTED (4), no value required. TCPOPT_TIMESTAMP (8), 8 bytes value for timestamp and echo timestamp, 4 bytes each one. More...
 
tree_cellset_tcp_elements (lex_ctxt *lexic)
 Modify the TCP fields of a datagram. More...
 
tree_cellinsert_tcp_options (lex_ctxt *lexic)
 Add options to a TCP segment header. Possible options are: TCPOPT_MAXSEG (2), values between 536 and 65535 TCPOPT_WINDOW (3), with values between 0 and 14 TCPOPT_SACK_PERMITTED (4), no value required. TCPOPT_TIMESTAMP (8), 8 bytes value for timestamp and echo timestamp, 4 bytes each one. More...
 
tree_celldump_tcp_packet (lex_ctxt *lexic)
 Dump the TCP part of a IP Datagram. More...
 
tree_cellforge_udp_packet (lex_ctxt *lexic)
 Fills an IP datagram with UDP data. Note that the ip_p field is not updated. It returns the modified IP datagram. Its arguments are: More...
 
tree_cellget_udp_element (lex_ctxt *lexic)
 Get an UDP element from a IP datagram. More...
 
tree_cellset_udp_elements (lex_ctxt *lexic)
 Modify UDP fields of an IP datagram. More...
 
tree_celldump_udp_packet (lex_ctxt *lexic)
 Dump the UDP part of a IP Datagram. More...
 
tree_cellforge_icmp_packet (lex_ctxt *lexic)
 Fill an IP datagram with ICMP data. More...
 
tree_cellget_icmp_element (lex_ctxt *lexic)
 Get an ICMP element from a IP datagram. More...
 
tree_celldump_icmp_packet (lex_ctxt *lexic)
 Dump the ICMP part of a IP Datagram. More...
 
tree_cellforge_igmp_packet (lex_ctxt *lexic)
 Fills an IP datagram with IGMP data. More...
 
tree_cellnasl_tcp_ping (lex_ctxt *lexic)
 Lunches a “TCP ping” against the target host. More...
 
tree_cellnasl_send_packet (lex_ctxt *lexic)
 Send a list of packets (passed as unnamed arguments) and listens to the answers. It returns a block made of all the sniffed “answers”. More...
 
tree_cellnasl_pcap_next (lex_ctxt *lexic)
 Listen to one packet and return it. More...
 
tree_cellnasl_send_capture (lex_ctxt *lexic)
 Send a capture. More...
 

Variables

struct in_addr saddr
 
struct in_addr daddr
 
u_char zero
 
u_char protocol
 
u_short length
 
struct tcphdr tcpheader
 
uint8_t kind
 
uint8_t len
 
uint16_t mss
 
uint8_t wscale
 
uint32_t tstamp
 
uint32_t e_tstamp
 
struct tcp_opt_sack_perm sack_perm
 
struct pseudo_udp_hdr __attribute__
 

Macro Definition Documentation

◆ FIX

#define FIX (   n)    htons (n)
Todo:
: It still needs to be taken care BSD_BYTE_ORDERING gets here if defined (e.g. by config.h)

◆ rnd_tcp_port

#define rnd_tcp_port ( )    (rand () % 65535 + 1024)

◆ UNFIX

#define UNFIX (   n)    ntohs (n)

Function Documentation

◆ __attribute__()

struct pseudohdr __attribute__ ( (packed)  )

◆ dump_icmp_packet()

tree_cell * dump_icmp_packet ( lex_ctxt lexic)

Dump the ICMP part of a IP Datagram.

Parameters
[in]lexicLexical context of NASL interpreter.
[in]...IP datagrams to dump the ICMP part from.

◆ dump_ip_packet()

tree_cell * dump_ip_packet ( lex_ctxt lexic)

Dump IP datagrams.

Parameters
[in]lexicLexical context of NASL interpreter.
[in]...IP datagrams to dump.

◆ dump_tcp_packet()

tree_cell * dump_tcp_packet ( lex_ctxt lexic)

Dump the TCP part of a IP Datagram.

Parameters
[in]lexicLexical context of NASL interpreter.
[in]...IP datagrams to dump the TCP part from.

◆ dump_udp_packet()

tree_cell * dump_udp_packet ( lex_ctxt lexic)

Dump the UDP part of a IP Datagram.

Parameters
[in]lexicLexical context of NASL interpreter.
[in]...IP datagrams to dump the UDP part from.

◆ forge_icmp_packet()

tree_cell * forge_icmp_packet ( lex_ctxt lexic)

Fill an IP datagram with ICMP data.

Parameters
[in]lexicLexical context of NASL interpreter.
[in]ipIP datagram that is updated.
[in]dataPayload.
[in]icmp_cksumChecksum, computed by default.
[in]icmp_codeICMP code. 0 by default.
[in]icmp_idICMP ID. 0 by default.
[in]icmp_seqICMP sequence number.
[in]icmp_typeICMP type. 0 by default.
[in]update_ip_lenFlag (TRUE by default). If set, NASL will recompute the size field of the IP datagram.
Returns
Modified IP datagram.

◆ forge_igmp_packet()

tree_cell * forge_igmp_packet ( lex_ctxt lexic)

Fills an IP datagram with IGMP data.

Parameters
[in]lexicLexical context of NASL interpreter.
[in]ipIP datagram that is updated.
[in]code0 by default.
[in]data
[in]group
[in]type0 by default.
[in]update_ip_lenFlag (TRUE by default). If set, NASL will recompute the size field of the IP datagram.
Returns
Modified IP datagram.

◆ forge_ip_packet()

tree_cell * forge_ip_packet ( lex_ctxt lexic)

Forge an IP datagram.

Parameters
[in]lexicLexical context of NASL interpreter.
[in]dataPayload.
[in]ip_hlIP header length in 32 bits words. 5 by default.
[in]ip_idDatagram ID. Random by default.
[in]ip_lenLength of the datagram. 20 plus the length of the data field by default.
[in]ip_offFragment offset in 64 bits words. 0 by default.
[in]ip_pIP protocol. 0 by default.
[in]ip_srcSource address in ASCII. NASL will convert it into an integer in network order.
[in]ip_dstDestination address in ASCII. NASL will convert it into an integer in network order. Uses the target ip of the current plugin by default.
[in]ip_sumPacket header checksum. It will be computed by default.
[in]ip_tosType of service field. 0 by default
[in]ip_ttlTime To Live field. 64 by default.
[in]ip_vIP version. 4 by default.
Returns
The forged IP packet.

◆ forge_tcp_packet()

tree_cell * forge_tcp_packet ( lex_ctxt lexic)

Fills an IP datagram with TCP data. Note that the ip_p field is not updated. It returns the modified IP datagram. Its arguments are:

Parameters
[in]ipIP datagram to be filled.
[in]dataTCP data payload.
[in]th_ackAcknowledge number. NASL will convert it into network order if necessary. 0 by default.
[in]th_dportDestination port. NASL will convert it into network order if necessary. 0 by default.
[in]th_flagsTCP flags. 0 by default.
[in]th_offSize of the TCP header in 32 bits words. By default, 5.
[in]th_seqTCP sequence number. NASL will convert it into network order if necessary. Random by default.
[in]th_sportSource port. NASL will convert it into network order if necessary. 0 by default.
[in]th_sumTCP checksum. Right value is computed by default.
[in]th_urpUrgent pointer. 0 by default.
[in]th_winTCP window size. NASL will convert it into network order if necessary. 0 by default.
[in]th_x2Is a reserved field and should probably be left unchanged. 0 by default.
[in]update_ip_lenFlag (TRUE by default). If set, NASL will recompute the size field of the IP datagram.
Returns
Modified IP datagram.

◆ forge_udp_packet()

tree_cell * forge_udp_packet ( lex_ctxt lexic)

Fills an IP datagram with UDP data. Note that the ip_p field is not updated. It returns the modified IP datagram. Its arguments are:

Parameters
[in]ipIP datagram to be filled.
[in]dataPayload.
[in]uh_dportDestination port. NASL will convert it into network order if necessary. 0 by default.
[in]uh_sportSource port. NASL will convert it into network order if necessary. 0 by default.
[in]uh_sumUDP checksum. Although it is not compulsory, the right value is computed by default.
[in]uh_ulenData length. By default it is set to the length of the data argument plus the size of the UDP header.
[in]update_ip_lenFlag (TRUE by default). If set, NASL will recompute the size field of the IP datagram.
Returns
Modified IP datagram.

◆ get_icmp_element()

tree_cell * get_icmp_element ( lex_ctxt lexic)

Get an ICMP element from a IP datagram.

Parameters
[in]lexicLexical context of NASL interpreter.
[in]icmpFull IP datagram (IP + ICMP).
[in]elementName of the TCP field (see forge_tcp_packet()).
Returns
Data block or an integer, according to the type of the element.

◆ get_ip_element()

tree_cell * get_ip_element ( lex_ctxt lexic)

Extracts a field from an IP datagram.

Parameters
[in]lexicLexical context of NASL interpreter.
[in]elementName of the field, e.g. "ip_len" or "ip_src".
[in]ipIP datagram or fragment.
Returns
integer or a string, depending on the type of the element.

◆ get_tcp_element()

tree_cell * get_tcp_element ( lex_ctxt lexic)

Extracts TCP field from an IP datagram.

Parameters
[in]lexicLexical context of NASL interpreter.
[in]elementName of the the TCP field. See forge_tcp_packet().
[in]tcpThe full IP datagram (IP + TCP).
Returns
Data block or an integer, according to the type of the element.

◆ get_tcp_option()

tree_cell * get_tcp_option ( lex_ctxt lexic)

Get a TCP option from an IP datagram if present. Possible options are: TCPOPT_MAXSEG (2), values between 536 and 65535 TCPOPT_WINDOW (3), with values between 0 and 14 TCPOPT_SACK_PERMITTED (4), no value required. TCPOPT_TIMESTAMP (8), 8 bytes value for timestamp and echo timestamp, 4 bytes each one.

Parameters
[in]lexicLexical context of NASL interpreter.
[in]tcpThe full IP datagram (IP + TCP).
[in]optionOption to get.
Returns
Integer or array given the case.

◆ get_tcp_options()

static void get_tcp_options ( char *  options,
struct tcp_options tcp_all_options 
)
static

Extract all TCP option from an IP datagram.

Parameters
[in]optionsAll options present in the TCP segment.
[out]tcp_all_optionsContainer for the options to return.

◆ get_udp_element()

tree_cell * get_udp_element ( lex_ctxt lexic)

Get an UDP element from a IP datagram.

Parameters
[in]lexicLexical context of NASL interpreter.
[in]udpThe full IP datagram (IP + UDP).
[in]elementName of the UDP field (see forge_udp_packet()).
Returns
Data block or an integer, according to the type of the element.

◆ insert_ip_options()

tree_cell * insert_ip_options ( lex_ctxt lexic)

Add option datagram.

Parameters
[in]lexicLexical context of NASL interpreter.
[in]ipIP datagram to add the option to.
[in]codeNumber of the option.
[in]lengthLength of the option data.
[in]valueOption data.
Returns
The modified IP datagram.

◆ insert_tcp_options()

tree_cell * insert_tcp_options ( lex_ctxt lexic)

Add options to a TCP segment header. Possible options are: TCPOPT_MAXSEG (2), values between 536 and 65535 TCPOPT_WINDOW (3), with values between 0 and 14 TCPOPT_SACK_PERMITTED (4), no value required. TCPOPT_TIMESTAMP (8), 8 bytes value for timestamp and echo timestamp, 4 bytes each one.

Parameters
[in]lexicLexical context of NASL interpreter.
[in]tcpIP datagram.
[in]data(optional) TCP data payload.
[in]unnamedoption.
[in]Valuefor unnamed option if required.
Returns
The modified IP datagram.

◆ nasl_pcap_next()

tree_cell * nasl_pcap_next ( lex_ctxt lexic)

Listen to one packet and return it.

Parameters
[in]lexicLexical context of NASL interpreter.
[in]interfaceNetwork interface name. By default, NASL will try to find the best one.
[in]pcap_filterBPF filter. By default, it listens to everything.
[in]timeout5 seconds by default.
Returns
Packet which was captured.

◆ nasl_send_capture()

tree_cell * nasl_send_capture ( lex_ctxt lexic)

Send a capture.

Parameters
[in]interfacestring
[in]pcapfilter string
[in]timeoutinteger
Returns
Packet which was captured.

◆ nasl_send_packet()

tree_cell * nasl_send_packet ( lex_ctxt lexic)

Send a list of packets (passed as unnamed arguments) and listens to the answers. It returns a block made of all the sniffed “answers”.

Parameters
[in]lexicLexical context of NASL interpreter.
[in]...Packets to send.
[in]lengthLength of each packet by default.
[in]pcap_activeTRUE by default. Otherwise, NASL does not listen for the answers.
[in]pcap_filterBPF filter.
[in]pcap_timeoutCapture timeout. 5 by default.
[in]allow_broadcastDefault 0.
Returns
block made of all the sniffed “answers”.

◆ nasl_tcp_ping()

tree_cell * nasl_tcp_ping ( lex_ctxt lexic)

Lunches a “TCP ping” against the target host.

Tries to open a TCP connection and sees if anything comes back (SYN/ACK or RST).

Parameters
[in]lexicLexical context of NASL interpreter.
[in]portPort to ping. Internal list of common ports is used as default.
Returns
1 if Ping was successful, 0 else.

◆ np_in_cksum()

static int np_in_cksum ( u_short *  p,
int  n 
)
static

◆ set_ip_elements()

tree_cell * set_ip_elements ( lex_ctxt lexic)

Modify the fields of a datagram.

Parameters
[in]lexicLexical context of NASL interpreter.
[in]ipIP datagram to set fields on.
[in]ip_hlIP header length in 32 bits words. 5 by default.
[in]ip_idDatagram ID. Random by default.
[in]ip_lenLength of the datagram. 20 plus the length of the data field by default.
[in]ip_offFragment offset in 64 bits words. 0 by default.
[in]ip_pIP protocol. 0 by default.
[in]ip_srcSource address in ASCII. NASL will convert it into an integer in network order.
[in]ip_sumPacket header checksum. It will be computed by default.
[in]ip_tosType of service field. 0 by default
[in]ip_ttlTime To Live field. 64 by default.
[in]ip_vIP version. 4 by default.
Returns
The modified IP datagram.

◆ set_tcp_elements()

tree_cell * set_tcp_elements ( lex_ctxt lexic)

Modify the TCP fields of a datagram.

Parameters
[in]lexicLexical context of NASL interpreter.
[in]tcpIP datagram.
[in]dataTCP data payload.
[in]th_ackAcknowledge number. NASL will convert it into network order if necessary.
[in]th_dportDestination port. NASL will convert it into network order if necessary.
[in]th_flagsTCP flags.
[in]th_offSize of the TCP header in 32 bits words.
[in]th_seqTCP sequence number. NASL will convert it into network order if necessary.
[in]th_sportSource port. NASL will convert it into network order if necessary.
[in]th_sumTCP checksum.
[in]th_urpUrgent pointer.
[in]th_winTCP window size. NASL will convert it into network order if necessary.
[in]th_x2Is a reserved field and should probably be left unchanged.
[in]update_ip_lenFlag (TRUE by default). If set, NASL will recompute the size field of the IP datagram.
Returns
The modified IP datagram.

◆ set_udp_elements()

tree_cell * set_udp_elements ( lex_ctxt lexic)

Modify UDP fields of an IP datagram.

Parameters
[in]udpIP datagram to modify.
[in]dataPayload.
[in]uh_dportDestination port. NASL will convert it into network order if necessary.
[in]uh_sportSource port. NASL will convert it into network order if necessary.
[in]uh_sumUDP checksum.
[in]uh_ulenData length.
Returns
Modified IP datagram.

Variable Documentation

◆ __attribute__

struct tcp_options __attribute__

◆ daddr

struct in_addr daddr

◆ e_tstamp

uint32_t e_tstamp

◆ kind

uint8_t kind

◆ len

uint8_t len

◆ length

u_short length

◆ mss

struct tcp_opt_mss mss

◆ protocol

u_char protocol

◆ sack_perm

struct tcp_opt_sack_perm sack_perm

◆ saddr

struct in_addr saddr

◆ tcpheader

struct tcphdr tcpheader

◆ tstamp

struct tcp_opt_tstamp tstamp

◆ wscale

struct tcp_opt_wscale wscale

◆ zero

u_char zero