Topic: C++ Packet Injector
I couldn’t find any other C++ Packet Injector anywhere on the net. So I decided to code one myself. This was modified from the previous C Injector. I like this because of NO PROMISCUOUS MODE NEEDED!! Also, the htons was a tricky mofo to beat in g++ but, ALAS! It was done. Copy and paste with your favorite editor into a file called start.cpp from the command line compile with:
bt#: g++ -o start start.cpp
then run the injector with:
bt#:./start "Interface ID" "# of packets"
exit the program with CTRL-C
=========================================
using namespace std; //dont like using it but its iight
#include<sys/types.h>
#include<sys/socket.h>
#include<iostream>
#include<sys/param.h>
#include<fstream>
#include<cstdio>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<features.h>
#include<linux/if_packet.h>
#include<linux/if_ether.h>
#include<errno.h>
#include<sys/ioctl.h>
#include<net/if.h>
#include<linux/inet.h>
#include<netinet/in.h>
#define PACKET_LENGTH 1024
int CreateRawSocket(int protocol_to_sniff)
{
ushort htons(ushort protocol_to_sniff);
int rawsock;
if((rawsock = socket(PF_PACKET, SOCK_RAW, htons(protocol_to_sniff)))== -1)
{
cout<<"Error creating raw socket:n";
exit(-1);
}
return rawsock;
}
int BindRawSocketToInterface(char *device, int rawsock, int protocol)
{
ushort htons(ushort protocol);
struct sockaddr_ll sll;
struct ifreq ifr;
bzero(&sll, sizeof(sll));
bzero(&ifr, sizeof(ifr));
strncpy((char *)ifr.ifr_name, device, IFNAMSIZ);
if((ioctl(rawsock, SIOCGIFINDEX, &ifr)) == -1)
{
cout<<"Error getting Interface index !n";
exit(-1);
}
sll.sll_family = AF_PACKET;
sll.sll_ifindex = ifr.ifr_ifindex;
sll.sll_protocol = htons(protocol);
if((bind(rawsock, (struct sockaddr *)&sll, sizeof(sll)))== -1)
{
cout<<"Error binding raw socket to interfacen";
exit(-1);
}
return 1;
}
int SendRawPacket(int rawsock, unsigned char *pkt, int pkt_len)
{
int sent= 0;
if((sent = write(rawsock, pkt, pkt_len)) != pkt_len)
{
return 0;
}
return 1;
}
main(int argc, char **argv)
{
int raw;
unsigned char packet[PACKET_LENGTH];
int num_of_pkts; char x; char y;
cin >> x;
memset(packet, x, PACKET_LENGTH);
while (y != EOF)
{
raw = CreateRawSocket(ETH_P_ALL);
BindRawSocketToInterface(argv[1], raw, ETH_P_ALL);
num_of_pkts = atoi(argv[2]);
while((num_of_pkts–)>0)
{
if(!SendRawPacket(raw, packet, PACKET_LENGTH))
{
cout<<"Error sending packetn";
}
else
{
cout <<"Packet sent successfullyn";
}
}
cin >> y;
close(raw);
memset(packet, y, PACKET_LENGTH);
}
return 0;
}
==================================
Modified by: JMC31337