英文:
I can not see my networks raw packets in C
问题
I have two virtual machines in VMware Infrastructure (Workstation) running Ubuntu 22.04, which are in the same network. With one program, I am sending raw packets from one virtual machine with a MAC address that does not match any of the computers on the network (as a requirement). In the other virtual machine, I can see the raw packets in Wireshark, but my C program cannot see them. I checked the IP tables rules, and there are no denials for raw packets. Below is the code I use to open the raw socket and read:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if_packet.h>
#include <net/ethernet.h>
#include <arpa/inet.h>
#include <linux/filter.h>
#include <fcntl.h>
int main(int argc, char *argv[]) {
int s, stat, cc;
unsigned char buf[ETH_FRAME_LEN];
struct sockaddr_ll saddr;
struct ifreq ifr;
char *interface = "ens33";
s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (s < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
strncpy(ifr.ifr_name, interface, IFNAMSIZ);
if (ioctl(s, SIOGIFINDEX, &ifr)) {
perror("ioctl");
close(s);
exit(EXIT_FAILURE);
}
saddr.sll_family = AF_PACKET;
saddr.sll_ifindex = ifr.ifr_ifindex;
saddr.sll_protocol = htons(ETH_P_ALL);
if (bind(s, (struct sockaddr *)&saddr, sizeof(saddr))) {
perror("bind");
exit(EXIT_FAILURE);
}
stat = fcntl(s, F_SETFL, O_NONBLOCK);
if (stat < 0)
perror("non-blocking");
while (1) {
cc = read(s, buf, sizeof(buf));
// Process the information
}
}
I tried to use libpcap, and it works, but due to the system's infrastructure, I am unable to change how the functions are used. I have one function to open the socket and another to read. These functions are used by other modules, and it's not possible to change them.
I changed the MAC address, but I still encounter the same problems. The virtual machines can see each other.
英文:
I have two virtual machines in vmware infrastructure (workstation) running Ubuntu 22.04, that are in the same network. With one program I am sending in one virtual machine raw packets, with a MAC that does not fit with any of the computers in the network (is a requirement). In the other virtual machine I can see the raw packets in Wireshark but my C program can not see it. I check the Iptables rules and there is no denied to raw packets. Here is the code I use to open the raw socket and read:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if_packet.h>
#include <net/ethernet.h>
#include <arpa/inet.h>
#include <linux/filter.h>
#include <fcntl.h>
int main(int arg, char** argv[])
{
int s, stat, cc;
unsigned char buf[ETH_FRAME_LEN];
struct sockaddr_ll saddr;
struct ifreq ifr;
char *interface = "ens33";
s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (s < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
strncpy(ifr.ifr_name, interface, IFNAMSIZ);
if(ioctl(s, SIOGIFINDEX, &ifr))
{
perror("ioctl");
close(s);
exit(EXIT_FAILURE);
}
saddr.sll_family = AF_PACKET;
saddr.sll_ifindex = ifr.ifr_ifindex;
saddr.sll_protocol = htons(ETH_P_ALL);
if (bind(s, (struct sockaddr *)&saddr, sizeof(saddr)))
{
perror("bind");
exit(EXIT_FAILURE);
}
stat = fcntl (s, F_SETFL, O_NONBLOCK);
if (stat < 0)
perror ("bloqueante");
while(1)
{
cc = read (s, buf, sizeof(buf));
// Process the information
}
}
I tried to use libpcap and works, but because of the infrastructure of the system I am not able to change the way the functions are used. I have one function to open the socket and another to read. This functions are used for other modules and it is not a possibility.
I change the MAC and the same problems. The virtual machines can see each other.
答案1
得分: 0
将接口设置为混杂模式,以接收非其MAC地址绑定的数据包。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if_packet.h>
#include <net/ethernet.h>
#include <arpa/inet.h>
#include <linux/filter.h>
#include <fcntl.h>
int main(int arg, char* argv[])
{
int s, stat;
size_t cc;
unsigned char buf[ETH_FRAME_LEN];
struct sockaddr_ll saddr = {};
struct ifreq ifopts; //for promiscuous mode
struct ifreq ifr = {};
const char *interface = "ens33";
s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (s < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
//设置混杂模式
strncpy(ifopts.ifr_name, interface, IFNAMSIZ-1);
ioctl(s, SIOCGIFFLAGS, &ifopts);
ifopts.ifr_flags |= IFF_PROMISC;
ioctl(s, SIOCSIFFLAGS, &ifopts);
strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
if(ioctl(s, SIOGIFINDEX, &ifr))
{
perror("ioctl");
close(s);
exit(EXIT_FAILURE);
}
saddr.sll_family = AF_PACKET;
saddr.sll_ifindex = ifr.ifr_ifindex;
saddr.sll_protocol = htons(ETH_P_ALL);
if (bind(s, (struct sockaddr *)&saddr, sizeof(saddr)))
{
perror("bind");
exit(EXIT_FAILURE);
}
stat = fcntl (s, F_SETFL, O_NONBLOCK);
if (stat < 0)
perror ("bloqueante");
while(true)
{
cc = read (s, buf, sizeof(buf));
//处理信息
}
}
英文:
Set interface to promiscuous mode to receive packets not bound for it's MAC address.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if_packet.h>
#include <net/ethernet.h>
#include <arpa/inet.h>
#include <linux/filter.h>
#include <fcntl.h>
int main(int arg, char* argv[])
{
int s, stat;
size_t cc;
unsigned char buf[ETH_FRAME_LEN];
struct sockaddr_ll saddr = {};
struct ifreq ifopts; //for promiscuous mode
struct ifreq ifr = {};
const char *interface = "ens33";
s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (s < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
//Set Promiscuous Mode
strncpy(ifopts.ifr_name, interface, IFNAMSIZ-1);
ioctl(s, SIOCGIFFLAGS, &ifopts);
ifopts.ifr_flags |= IFF_PROMISC;
ioctl(s, SIOCSIFFLAGS, &ifopts);
strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
if(ioctl(s, SIOGIFINDEX, &ifr))
{
perror("ioctl");
close(s);
exit(EXIT_FAILURE);
}
saddr.sll_family = AF_PACKET;
saddr.sll_ifindex = ifr.ifr_ifindex;
saddr.sll_protocol = htons(ETH_P_ALL);
if (bind(s, (struct sockaddr *)&saddr, sizeof(saddr)))
{
perror("bind");
exit(EXIT_FAILURE);
}
stat = fcntl (s, F_SETFL, O_NONBLOCK);
if (stat < 0)
perror ("bloqueante");
while(true)
{
cc = read (s, buf, sizeof(buf));
// Process the information
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论