Is it possible to add an ethernet header to packet data captured with pcap?
Basically I'm have a stab at redirecting traffic, what I'm trying to do is capture packets using pcap and then either edit the packets ethernet frame or build it a new one with a different destination address.
I am aware I can build ethernet frames easily with libnet, however I'm not sure how to build a frame with libnet and "attach" it to the beginning of the packet.
I'm also trying to do this in a callback function for pcap_loop which I've attached. If anyone has any ideas that'd be great. Thanks in advance.
void decode(pcap_t* handle, const struct pcap_pkthdr* pkthdr, const u_char* packet)
{
u_char* ethhead = packet; //Get Ethernet header
u_char* iphead = packet+14; //Skip Ethernet header
struct ethhdr* eth_header = malloc(ETH_HLEN);
struct iphdr* ip_header = iphead;
int len = 6;
char libbuf[LIBNET_ERRBUF_SIZE];
memcpy(eth_header, packet, ETH_HLEN);
libnet_t* l = libnet_init(LIBNET_LINK, "enp6s0", libbuf);
u_int32_t target_ip = inet_addr("192.168.1.7");
u_int8_t gateway_mac = libnet_hex_aton("76:b0:65:cd:13:d2", &len);
u_int8_t target_mac = libnet_hex_aton("d0:b2:f4:12:06:e1", &len);
u_int8_t this = libnet_get_hwaddr(l);
if((ip_header->saddr == target_ip) || (ip_header->daddr == target_ip))
{
printf("n---------------------- Caught %d byte packet ----------------------n", pkthdr->len);
dump(packet, pkthdr->len);
if(eth_header->h_proto != ETH_P_ARP)
{
printf("nEthernet frame:n");
printf("Source MAC address: %02x:%02x:%02x:%02x:%02x:%02xn", ethhead[0],ethhead[1],ethhead[2], ethhead[3],ethhead[4],ethhead[5]);
printf("Destination MAC address: %02x:%02x:%02x:%02x:%02x:%02xn", ethhead[6],ethhead[7],ethhead[8], ethhead[9],ethhead[10],ethhead[11]);
if (*iphead==0x45) //For IPv4
{
printf("nnIP Header:n");
printf("Version: %in", ip_header->version);
printf("Type of service: %in", ip_header->tos);
printf("Header length: %in", ip_header->ihl);
printf("Total Length: %in", ip_header->tot_len);
printf("Time to live: %dn", ip_header->ttl);
printf("Layer-4 protocol %in", ip_header->protocol);
printf("Source host %d.%d.%d.%dn", iphead[12], iphead[13], iphead[14],iphead[15]);
printf("Dest host %d.%d.%d.%dn", iphead[16],iphead[17], iphead[18], iphead[19]);
if((int)iphead[9] == 6)
{
struct tcphdr *tcp_header;
tcp_header = iphead + ip_header->ihl;
printf("nnTCP Header:n");
printf("Source Port: %in", tcp_header->source);
printf("Dest Port: %in", tcp_header->dest);
printf("Sequence Number: %in", tcp_header->seq);
printf("Acknowledgement Number: %in", tcp_header->ack_seq);
printf("URG: %in", tcp_header->urg);
printf("ACK: %in", tcp_header->ack);
printf("PSH: %in", tcp_header->psh);
printf("RST: %in", tcp_header->rst);
printf("SYN: %in", tcp_header->syn);
printf("FIN: %in", tcp_header->fin);
printf("Checksum: %in", tcp_header->check);
}
}
printf("n---------------------------------------------------------------------n");
int inject;
if(ip_header->daddr == target_ip)
{
inject = libnet_autobuild_ethernet(&target_mac, eth_header->h_proto, l);
if(inject == -1)
printf("nn ----ERROR IN libnet_build_ethernet----");
libnet_write(l);
libnet_destroy(l);
}
else if(ip_header->saddr == target_ip)
{
inject = libnet_autobuild_ethernet(&gateway_mac, eth_header->h_proto, l);
if(inject == -1)
printf("nn ----ERROR IN libnet_build_ethernet----");
libnet_write(l);
libnet_destroy(l);
}
free(eth_header);
}
}
}
Using this program along with a separate cache poisoning program I've made I hope to be able to receive traffic from the target, change the mac address of said packets to the networks gateway, and then send it on its way while doing the reverse with the gateways traffic.
c networking ethernet packet libnet
add a comment |
Basically I'm have a stab at redirecting traffic, what I'm trying to do is capture packets using pcap and then either edit the packets ethernet frame or build it a new one with a different destination address.
I am aware I can build ethernet frames easily with libnet, however I'm not sure how to build a frame with libnet and "attach" it to the beginning of the packet.
I'm also trying to do this in a callback function for pcap_loop which I've attached. If anyone has any ideas that'd be great. Thanks in advance.
void decode(pcap_t* handle, const struct pcap_pkthdr* pkthdr, const u_char* packet)
{
u_char* ethhead = packet; //Get Ethernet header
u_char* iphead = packet+14; //Skip Ethernet header
struct ethhdr* eth_header = malloc(ETH_HLEN);
struct iphdr* ip_header = iphead;
int len = 6;
char libbuf[LIBNET_ERRBUF_SIZE];
memcpy(eth_header, packet, ETH_HLEN);
libnet_t* l = libnet_init(LIBNET_LINK, "enp6s0", libbuf);
u_int32_t target_ip = inet_addr("192.168.1.7");
u_int8_t gateway_mac = libnet_hex_aton("76:b0:65:cd:13:d2", &len);
u_int8_t target_mac = libnet_hex_aton("d0:b2:f4:12:06:e1", &len);
u_int8_t this = libnet_get_hwaddr(l);
if((ip_header->saddr == target_ip) || (ip_header->daddr == target_ip))
{
printf("n---------------------- Caught %d byte packet ----------------------n", pkthdr->len);
dump(packet, pkthdr->len);
if(eth_header->h_proto != ETH_P_ARP)
{
printf("nEthernet frame:n");
printf("Source MAC address: %02x:%02x:%02x:%02x:%02x:%02xn", ethhead[0],ethhead[1],ethhead[2], ethhead[3],ethhead[4],ethhead[5]);
printf("Destination MAC address: %02x:%02x:%02x:%02x:%02x:%02xn", ethhead[6],ethhead[7],ethhead[8], ethhead[9],ethhead[10],ethhead[11]);
if (*iphead==0x45) //For IPv4
{
printf("nnIP Header:n");
printf("Version: %in", ip_header->version);
printf("Type of service: %in", ip_header->tos);
printf("Header length: %in", ip_header->ihl);
printf("Total Length: %in", ip_header->tot_len);
printf("Time to live: %dn", ip_header->ttl);
printf("Layer-4 protocol %in", ip_header->protocol);
printf("Source host %d.%d.%d.%dn", iphead[12], iphead[13], iphead[14],iphead[15]);
printf("Dest host %d.%d.%d.%dn", iphead[16],iphead[17], iphead[18], iphead[19]);
if((int)iphead[9] == 6)
{
struct tcphdr *tcp_header;
tcp_header = iphead + ip_header->ihl;
printf("nnTCP Header:n");
printf("Source Port: %in", tcp_header->source);
printf("Dest Port: %in", tcp_header->dest);
printf("Sequence Number: %in", tcp_header->seq);
printf("Acknowledgement Number: %in", tcp_header->ack_seq);
printf("URG: %in", tcp_header->urg);
printf("ACK: %in", tcp_header->ack);
printf("PSH: %in", tcp_header->psh);
printf("RST: %in", tcp_header->rst);
printf("SYN: %in", tcp_header->syn);
printf("FIN: %in", tcp_header->fin);
printf("Checksum: %in", tcp_header->check);
}
}
printf("n---------------------------------------------------------------------n");
int inject;
if(ip_header->daddr == target_ip)
{
inject = libnet_autobuild_ethernet(&target_mac, eth_header->h_proto, l);
if(inject == -1)
printf("nn ----ERROR IN libnet_build_ethernet----");
libnet_write(l);
libnet_destroy(l);
}
else if(ip_header->saddr == target_ip)
{
inject = libnet_autobuild_ethernet(&gateway_mac, eth_header->h_proto, l);
if(inject == -1)
printf("nn ----ERROR IN libnet_build_ethernet----");
libnet_write(l);
libnet_destroy(l);
}
free(eth_header);
}
}
}
Using this program along with a separate cache poisoning program I've made I hope to be able to receive traffic from the target, change the mac address of said packets to the networks gateway, and then send it on its way while doing the reverse with the gateways traffic.
c networking ethernet packet libnet
add a comment |
Basically I'm have a stab at redirecting traffic, what I'm trying to do is capture packets using pcap and then either edit the packets ethernet frame or build it a new one with a different destination address.
I am aware I can build ethernet frames easily with libnet, however I'm not sure how to build a frame with libnet and "attach" it to the beginning of the packet.
I'm also trying to do this in a callback function for pcap_loop which I've attached. If anyone has any ideas that'd be great. Thanks in advance.
void decode(pcap_t* handle, const struct pcap_pkthdr* pkthdr, const u_char* packet)
{
u_char* ethhead = packet; //Get Ethernet header
u_char* iphead = packet+14; //Skip Ethernet header
struct ethhdr* eth_header = malloc(ETH_HLEN);
struct iphdr* ip_header = iphead;
int len = 6;
char libbuf[LIBNET_ERRBUF_SIZE];
memcpy(eth_header, packet, ETH_HLEN);
libnet_t* l = libnet_init(LIBNET_LINK, "enp6s0", libbuf);
u_int32_t target_ip = inet_addr("192.168.1.7");
u_int8_t gateway_mac = libnet_hex_aton("76:b0:65:cd:13:d2", &len);
u_int8_t target_mac = libnet_hex_aton("d0:b2:f4:12:06:e1", &len);
u_int8_t this = libnet_get_hwaddr(l);
if((ip_header->saddr == target_ip) || (ip_header->daddr == target_ip))
{
printf("n---------------------- Caught %d byte packet ----------------------n", pkthdr->len);
dump(packet, pkthdr->len);
if(eth_header->h_proto != ETH_P_ARP)
{
printf("nEthernet frame:n");
printf("Source MAC address: %02x:%02x:%02x:%02x:%02x:%02xn", ethhead[0],ethhead[1],ethhead[2], ethhead[3],ethhead[4],ethhead[5]);
printf("Destination MAC address: %02x:%02x:%02x:%02x:%02x:%02xn", ethhead[6],ethhead[7],ethhead[8], ethhead[9],ethhead[10],ethhead[11]);
if (*iphead==0x45) //For IPv4
{
printf("nnIP Header:n");
printf("Version: %in", ip_header->version);
printf("Type of service: %in", ip_header->tos);
printf("Header length: %in", ip_header->ihl);
printf("Total Length: %in", ip_header->tot_len);
printf("Time to live: %dn", ip_header->ttl);
printf("Layer-4 protocol %in", ip_header->protocol);
printf("Source host %d.%d.%d.%dn", iphead[12], iphead[13], iphead[14],iphead[15]);
printf("Dest host %d.%d.%d.%dn", iphead[16],iphead[17], iphead[18], iphead[19]);
if((int)iphead[9] == 6)
{
struct tcphdr *tcp_header;
tcp_header = iphead + ip_header->ihl;
printf("nnTCP Header:n");
printf("Source Port: %in", tcp_header->source);
printf("Dest Port: %in", tcp_header->dest);
printf("Sequence Number: %in", tcp_header->seq);
printf("Acknowledgement Number: %in", tcp_header->ack_seq);
printf("URG: %in", tcp_header->urg);
printf("ACK: %in", tcp_header->ack);
printf("PSH: %in", tcp_header->psh);
printf("RST: %in", tcp_header->rst);
printf("SYN: %in", tcp_header->syn);
printf("FIN: %in", tcp_header->fin);
printf("Checksum: %in", tcp_header->check);
}
}
printf("n---------------------------------------------------------------------n");
int inject;
if(ip_header->daddr == target_ip)
{
inject = libnet_autobuild_ethernet(&target_mac, eth_header->h_proto, l);
if(inject == -1)
printf("nn ----ERROR IN libnet_build_ethernet----");
libnet_write(l);
libnet_destroy(l);
}
else if(ip_header->saddr == target_ip)
{
inject = libnet_autobuild_ethernet(&gateway_mac, eth_header->h_proto, l);
if(inject == -1)
printf("nn ----ERROR IN libnet_build_ethernet----");
libnet_write(l);
libnet_destroy(l);
}
free(eth_header);
}
}
}
Using this program along with a separate cache poisoning program I've made I hope to be able to receive traffic from the target, change the mac address of said packets to the networks gateway, and then send it on its way while doing the reverse with the gateways traffic.
c networking ethernet packet libnet
Basically I'm have a stab at redirecting traffic, what I'm trying to do is capture packets using pcap and then either edit the packets ethernet frame or build it a new one with a different destination address.
I am aware I can build ethernet frames easily with libnet, however I'm not sure how to build a frame with libnet and "attach" it to the beginning of the packet.
I'm also trying to do this in a callback function for pcap_loop which I've attached. If anyone has any ideas that'd be great. Thanks in advance.
void decode(pcap_t* handle, const struct pcap_pkthdr* pkthdr, const u_char* packet)
{
u_char* ethhead = packet; //Get Ethernet header
u_char* iphead = packet+14; //Skip Ethernet header
struct ethhdr* eth_header = malloc(ETH_HLEN);
struct iphdr* ip_header = iphead;
int len = 6;
char libbuf[LIBNET_ERRBUF_SIZE];
memcpy(eth_header, packet, ETH_HLEN);
libnet_t* l = libnet_init(LIBNET_LINK, "enp6s0", libbuf);
u_int32_t target_ip = inet_addr("192.168.1.7");
u_int8_t gateway_mac = libnet_hex_aton("76:b0:65:cd:13:d2", &len);
u_int8_t target_mac = libnet_hex_aton("d0:b2:f4:12:06:e1", &len);
u_int8_t this = libnet_get_hwaddr(l);
if((ip_header->saddr == target_ip) || (ip_header->daddr == target_ip))
{
printf("n---------------------- Caught %d byte packet ----------------------n", pkthdr->len);
dump(packet, pkthdr->len);
if(eth_header->h_proto != ETH_P_ARP)
{
printf("nEthernet frame:n");
printf("Source MAC address: %02x:%02x:%02x:%02x:%02x:%02xn", ethhead[0],ethhead[1],ethhead[2], ethhead[3],ethhead[4],ethhead[5]);
printf("Destination MAC address: %02x:%02x:%02x:%02x:%02x:%02xn", ethhead[6],ethhead[7],ethhead[8], ethhead[9],ethhead[10],ethhead[11]);
if (*iphead==0x45) //For IPv4
{
printf("nnIP Header:n");
printf("Version: %in", ip_header->version);
printf("Type of service: %in", ip_header->tos);
printf("Header length: %in", ip_header->ihl);
printf("Total Length: %in", ip_header->tot_len);
printf("Time to live: %dn", ip_header->ttl);
printf("Layer-4 protocol %in", ip_header->protocol);
printf("Source host %d.%d.%d.%dn", iphead[12], iphead[13], iphead[14],iphead[15]);
printf("Dest host %d.%d.%d.%dn", iphead[16],iphead[17], iphead[18], iphead[19]);
if((int)iphead[9] == 6)
{
struct tcphdr *tcp_header;
tcp_header = iphead + ip_header->ihl;
printf("nnTCP Header:n");
printf("Source Port: %in", tcp_header->source);
printf("Dest Port: %in", tcp_header->dest);
printf("Sequence Number: %in", tcp_header->seq);
printf("Acknowledgement Number: %in", tcp_header->ack_seq);
printf("URG: %in", tcp_header->urg);
printf("ACK: %in", tcp_header->ack);
printf("PSH: %in", tcp_header->psh);
printf("RST: %in", tcp_header->rst);
printf("SYN: %in", tcp_header->syn);
printf("FIN: %in", tcp_header->fin);
printf("Checksum: %in", tcp_header->check);
}
}
printf("n---------------------------------------------------------------------n");
int inject;
if(ip_header->daddr == target_ip)
{
inject = libnet_autobuild_ethernet(&target_mac, eth_header->h_proto, l);
if(inject == -1)
printf("nn ----ERROR IN libnet_build_ethernet----");
libnet_write(l);
libnet_destroy(l);
}
else if(ip_header->saddr == target_ip)
{
inject = libnet_autobuild_ethernet(&gateway_mac, eth_header->h_proto, l);
if(inject == -1)
printf("nn ----ERROR IN libnet_build_ethernet----");
libnet_write(l);
libnet_destroy(l);
}
free(eth_header);
}
}
}
Using this program along with a separate cache poisoning program I've made I hope to be able to receive traffic from the target, change the mac address of said packets to the networks gateway, and then send it on its way while doing the reverse with the gateways traffic.
c networking ethernet packet libnet
c networking ethernet packet libnet
edited Jan 2 at 20:33
CHTENNO
asked Jan 2 at 17:18
CHTENNOCHTENNO
1615
1615
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010553%2fis-it-possible-to-add-an-ethernet-header-to-packet-data-captured-with-pcap%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010553%2fis-it-possible-to-add-an-ethernet-header-to-packet-data-captured-with-pcap%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown