21#include <gvm/base/networking.h>
22#include <linux/if_packet.h>
23#include <net/ethernet.h>
25#include <net/if_arp.h>
26#include <netinet/ether.h>
27#include <netinet/if_ether.h>
38#define G_LOG_DOMAIN "lib misc"
67 printf (
"\nThe Frame:\n");
70 printf (
"%02x%02x ", ((u_char *) frame)[f], ((u_char *) frame)[f + 1]);
86 const u_char *ether_dst_addr)
88 soc_addr_ll->sll_family = AF_PACKET;
89 soc_addr_ll->sll_ifindex = ifindex;
90 soc_addr_ll->sll_halen = ETHER_ADDR_LEN;
91 soc_addr_ll->sll_protocol = htons (ETH_P_ALL);
92 memcpy (soc_addr_ll->sll_addr, ether_dst_addr, ETHER_ADDR_LEN);
107 struct msghdr *message;
110 iov.iov_len = payload_sz;
112 message = g_malloc0 (
sizeof (
struct msghdr) + payload_sz);
114 message->msg_name = soc_addr_ll;
115 message->msg_namelen =
sizeof (
struct sockaddr_ll);
116 message->msg_iov = &iov;
117 message->msg_iovlen = 1;
118 message->msg_control = 0;
119 message->msg_controllen = 0;
121 memcpy (msg, (u_char *) message,
sizeof (
struct msghdr) + payload_sz);
141send_frame (
const u_char *frame,
int frame_sz,
int use_pcap,
int timeout,
142 char *filter,
struct in6_addr *ipaddr, u_char **answer)
148 int frame_and_payload = 0;
152 soc = socket (AF_PACKET, SOCK_RAW, htons (ETH_P_ALL));
155 g_debug (
"%s: %s", __func__, strerror (errno));
162 g_message (
"%s: Missing interface index\n", __func__);
167 u_char dst_haddr[ETHER_ADDR_LEN];
168 memcpy (&dst_haddr, (
struct pseudo_frame *) frame, ETHER_ADDR_LEN);
170 struct sockaddr_ll soc_addr;
171 memset (&soc_addr,
'\0',
sizeof (
struct sockaddr_ll));
175 if (use_pcap != 0 && bpf < 0)
177 if (IN6_IS_ADDR_V4MAPPED (ipaddr))
179 struct in_addr sin, this_host;
180 memset (&sin,
'\0',
sizeof (
struct in_addr));
181 memset (&this_host,
'\0',
sizeof (
struct in_addr));
182 sin.s_addr = ipaddr->s6_addr32[3];
187 struct in6_addr this_host;
188 memset (&this_host,
'\0',
sizeof (
struct in6_addr));
194 message = g_malloc0 (
sizeof (
struct msghdr) + frame_sz);
197 int b = sendmsg (soc, (
struct msghdr *) message, 0);
201 g_message (
"%s: Error sending message: %s", __func__, strerror (errno));
230forge_frame (
const u_char *ether_src_addr,
const u_char *ether_dst_addr,
231 int ether_proto, u_char *
payload,
int payload_sz,
239 memcpy ((*frame)->framehdr.h_dest, ether_dst_addr, ETHER_ADDR_LEN);
240 memcpy ((*frame)->framehdr.h_source, ether_src_addr, ETHER_ADDR_LEN);
241 (*frame)->framehdr.h_proto = htons (ether_proto);
244 frame_sz = ETH_HLEN + payload_sz;
245 memcpy ((
char *) *frame + ETH_HLEN,
payload, payload_sz);
279 if (ether_src_addr == NULL || ether_dst_addr == NULL ||
payload == NULL)
282 "%s usage: payload, src_haddr and dst_haddr are mandatory "
288 frame_sz =
forge_frame ((u_char *) ether_src_addr, (u_char *) ether_dst_addr,
289 ether_proto,
payload, payload_sz, &frame);
293 retc->
size = frame_sz;
324 u_char *answer = NULL;
327 if (frame == NULL || frame_sz <= 0)
329 nasl_perror (lexic,
"%s usage: frame is a mandatory parameters.\n",
335 send_frame (frame, frame_sz, use_pcap, to, filter, ipaddr, &answer);
338 g_message (
"%s: Not possible to send the frame", __func__);
342 if (answer && answer_sz > -1)
346 retc->
size = answer_sz;
368 if (frame == NULL || frame_sz <= 0)
370 nasl_perror (lexic,
"%s usage: frame is a mandatory parameters.\n",
393 char *if_name = NULL;
398 g_debug (
"%s: Missing interface name", __func__);
402 strncpy (ifr.ifr_name, if_name, sizeof (ifr.ifr_name) - 1);
404 ifr.ifr_name[
sizeof (ifr.ifr_name) - 1] =
'\0';
406 sock = socket (PF_INET, SOCK_STREAM, 0);
409 perror (
"socket() ");
413 if (-1 == ioctl (sock, SIOCGIFHWADDR, &ifr))
415 g_debug (
"%s: ioctl(SIOCGIFHWADDR)", __func__);
419 memcpy (
mac, (u_char *) ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
446 mac = g_malloc0 (
sizeof (u_char) * ETHER_ADDR_LEN);
450 buffer = g_strdup_printf (
"%02x:%02x:%02x:%02x:%02x:%02x",
mac[0],
mac[1],
475 struct in_addr dst_inaddr, src_inaddr;
479 char ip_src_str[INET6_ADDRSTRLEN];
480 u_char mac_broadcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
481 u_char
mac[6], *mac_aux;
483 u_char *answer = NULL;
489 if (dst == NULL || (IN6_IS_ADDR_V4MAPPED (dst) != 1))
492 memset (&dst_inaddr,
'\0',
sizeof (
struct in_addr));
493 dst_inaddr.s_addr = dst->s6_addr32[3];
495 ipv4_as_ipv6 (&src_inaddr, &src);
498 addr6_to_str (&src, ip_src_str);
500 mac_aux = (u_char *) g_malloc0 (
sizeof (u_char) * 6);
512 eth_arp.
arp_header.ar_hrd = htons (ARPHRD_ETHER);
513 eth_arp.
arp_header.ar_pro = htons (ETHERTYPE_IP);
516 eth_arp.
arp_header.ar_op = htons (ARPOP_REQUEST);
519 memcpy (&(eth_arp.
__ar_sip), &src_inaddr, 4);
520 memcpy (&(eth_arp.
__ar_tha), mac_broadcast_addr, ETH_ALEN);
521 memcpy (&(eth_arp.
__ar_tip), &dst_inaddr, 4);
524 forge_frame (
mac, mac_broadcast_addr, ETH_P_ARP, (u_char *) ð_arp,
528 snprintf (filter,
sizeof (filter),
"arp and src host %s",
529 inet_ntoa (dst_inaddr));
532 send_frame ((
const u_char *) frame, frame_sz, 1, to, filter, dst, &answer);
536 g_message (
"%s: Not possible to send the frame", __func__);
540 if (answer && answer_sz > -1)
543 struct ether_header *answer_aux;
545 answer_aux = (
struct ether_header *) answer;
546 daddr = g_strdup_printf (
547 "%02x:%02x:%02x:%02x:%02x:%02x", (u_int) answer_aux->ether_shost[0],
548 (u_int) answer_aux->ether_shost[1], (u_int) answer_aux->ether_shost[2],
549 (u_int) answer_aux->ether_shost[3], (u_int) answer_aux->ether_shost[4],
550 (u_int) answer_aux->ether_shost[5]);
557 g_debug (
"%s: No answer received.", __func__);
Header file for module bpf_share.
char * capture_next_frame(int bpf, int timeout, int *sz, int dl_layer_only)
Capture a link layer frame.
int init_capture_device(struct in_addr src, struct in_addr dest, char *filter)
Set up the pcap filter, and select the correct interface.
int init_v6_capture_device(struct in6_addr src, struct in6_addr dest, char *filter)
gpg_err_code_t mac(const char *key, const size_t key_len, const char *data, const size_t data_len, const char *iv, const size_t iv_len, int algo, int flags, char **out, size_t *out_len)
void nasl_perror(lex_ctxt *lexic, char *msg,...)
static int forge_frame(const u_char *ether_src_addr, const u_char *ether_dst_addr, int ether_proto, u_char *payload, int payload_sz, struct pseudo_frame **frame)
Forge a datalink layer frame.
tree_cell * nasl_send_frame(lex_ctxt *lexic)
Send a frame and listen to the answer.
static int get_local_mac_address_from_ip(char *ip_address, u_char *mac)
Get the MAC address of host.
static int send_frame(const u_char *frame, int frame_sz, int use_pcap, int timeout, char *filter, struct in6_addr *ipaddr, u_char **answer)
Send a frame and listen to the answer.
static void prepare_sockaddr_ll(struct sockaddr_ll *soc_addr_ll, int ifindex, const u_char *ether_dst_addr)
Prepare message header to be sent with sendmsg().
tree_cell * nasl_dump_frame(lex_ctxt *lexic)
Dump a datalink layer frame.
static void prepare_message(u_char *msg, struct sockaddr_ll *soc_addr_ll, u_char *payload, int payload_sz)
Prepare message header to be sent with sendmsg().
tree_cell * nasl_get_local_mac_address_from_ip(lex_ctxt *lexic)
Get the MAC address of host.
static void dump_frame(const u_char *frame, int frame_sz)
Dump a datalink layer frame.
tree_cell * nasl_forge_frame(lex_ctxt *lexic)
Forge a datalink layer frame.
tree_cell * nasl_send_arp_request(lex_ctxt *lexic)
Send an arp request to an IP host.
Header file for module nasl_frame_forgery.
struct struct_lex_ctxt lex_ctxt
int get_var_size_by_name(lex_ctxt *, const char *)
char * get_str_var_by_name(lex_ctxt *, const char *)
char * get_str_var_by_num(lex_ctxt *, int)
long int get_int_var_by_name(lex_ctxt *, const char *, int)
struct pseudo_udp_hdr __attribute__
tree_cell * alloc_typed_cell(int typ)
char * routethrough(struct in_addr *dest, struct in_addr *source)
An awesome function to determine what interface a packet to a given destination should be routed thro...
int get_iface_index(struct in6_addr *ipaddr, int *ifindex)
Get the interface index depending on the target's IP.
char * get_iface_from_ip(const char *local_ip)
Given an IP address, determines which interface belongs to.
Header file for module pcap.
struct in6_addr * plug_get_host_ip(struct script_infos *args)
Header file for module plugutils.
union TC::@332262321161220155002104006201360276211317150140 x
u_char __ar_sha[ETH_ALEN]
u_char __ar_tha[ETH_ALEN]
u_char __zero_padding[18]
struct script_infos * script_infos