Twilight Zone

  • Increase font size
  • Default font size
  • Decrease font size

Simple packet injection

E-mail Print PDF

An example of simple packet injection, binding to a socket and sending a packet on the wire

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <netpacket/packet.h>
#include <sys/ioctl.h>

char pkt[] = {
0x00, 0x0d, 0x60, 0xb2, 0x57, 0x22, 0x00, 0x0f,
0x66, 0x4b, 0xcb, 0xa4, 0x08, 0x00, 0x45, 0x00,
0x00, 0x61, 0x00, 0x00, 0x40, 0x00, 0x40, 0x11,
0xb5, 0x30, 0xc0, 0xa8, 0x02, 0x01, 0xc0, 0xa8,
0x02, 0x0a, 0x08, 0x00, 0x02, 0x02, 0x00, 0x4d,
0x86, 0x5c, 0x3c, 0x36, 0x3e, 0x20, 0x64, 0x6e,
0x73, 0x6d, 0x61, 0x73, 0x71, 0x5b, 0x35, 0x37,
0x32, 0x5d, 0x3a, 0x20, 0x44, 0x48, 0x43, 0x50,
0x41, 0x43, 0x4b, 0x28, 0x62, 0x72, 0x30, 0x29,
0x20, 0x31, 0x39, 0x32, 0x2e, 0x31, 0x36, 0x38,
0x2e, 0x32, 0x2e, 0x35, 0x34, 0x20, 0x30, 0x30,
0x3a, 0x31, 0x33, 0x3a, 0x65, 0x38, 0x3a, 0x38,
0x64, 0x3a, 0x61, 0x39, 0x3a, 0x36, 0x31, 0x20,
0x61, 0x73, 0x75, 0x73, 0x76, 0x32, 0x73
};

int main ( int argc, char *argv[] )
{
int s;
if ((s = socket(PF_PACKET, SOCK_RAW, ETH_P_ALL)) == -1){
printf("socket: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}

struct ifreq my_ifreq;
memset(&my_ifreq, 0, sizeof(struct ifreq));
memcpy(my_ifreq.ifr_name, argv[1], IFNAMSIZ);

struct sockaddr_ll saddr;
memset(&saddr, 0, sizeof(struct sockaddr_ll));

saddr.sll_family = AF_PACKET;
saddr.sll_protocol = htons(ETH_P_ALL);

ioctl(s, SIOCGIFINDEX, &my_ifreq);
saddr.sll_ifindex =  my_ifreq.ifr_ifindex;

ioctl(s, SIOCGIFHWADDR, &my_ifreq);
memcpy(&saddr.sll_addr, &my_ifreq.ifr_hwaddr, 8);

saddr.sll_halen = 8;

if(bind(s, (struct sockaddr*) &saddr, sizeof(struct sockaddr_ll)) == -1){
printf("bind: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}

if(send(s, pkt, sizeof(pkt), 0) == -1){
printf("send: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}

return EXIT_SUCCESS;
}
Attachments:
Download this file (packet_replay.zip)packet_replay.zip[C source]247 Kb