【问题标题】:Packet sniffer in C for all VM用于所有 VM 的 C 中的数据包嗅探器
【发布时间】:2015-11-19 23:01:31
【问题描述】:

我有 3 个代表客户端/服务器/攻击者的 Ubuntu 虚拟机。我创建了一个在 TCP 中连接客户端和服务器的应用程序,并且我在攻击者机器中有一个数据包嗅探器。但是,该代码仅设法嗅探来自攻击者机器的数据包,它无法检测客户端/服务器连接。我尝试了 Wireshark,它捕获了它们之间的连接。你认为问题是什么?

我正在使用 Virtual Box,并且网络已桥接。 这是代码

#include <pcap.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>


pcap_t* handle; 
int linkhdrsize; 
char filter[256];

/* - Packet Capture function - */
pcap_t* packet_capture(char* device, const char* bpfstr)
{
char errorbuffer[PCAP_ERRBUF_SIZE];
pcap_t* handle;
uint32_t source_ip, netmask;
struct bpf_program bpf;

if((handle=pcap_open_live(device,BUFSIZ,1,0,errorbuffer)) == NULL){
printf("pcap_open_live(): %s\n", errorbuffer);
return NULL;
}

if(pcap_lookupnet(device, &source_ip, &netmask, errorbuffer) <0)
{
printf("pcap_lookupnet: %s\n", errorbuffer);
return NULL;
}

if (pcap_compile(handle, &bpf, (char*)filter, 0, netmask))
{
printf("pcap_compile(): %s\n", pcap_geterr(handle));
return NULL;
}
if (pcap_setfilter(handle, &bpf) < 0)
{
printf("pcap_setfilter(): %s\n", pcap_geterr(handle));
return NULL;
}

return handle;
}

/* - Capture Loop function - */

void capture_loop(pcap_t* handle, pcap_handler func)
{
int linktype;
if((linktype = pcap_datalink(handle))<0)
{
printf("pcap_datalink(): %s\n", pcap_geterr(handle));
}
if(linktype==DLT_EN10MB)
linkhdrsize=14;
else
printf("Unsupported datalink (%d)\n", linktype);
if (pcap_loop(handle, 0, func, 0) < 0)
printf("pcap_loop failed: %s\n", pcap_geterr(handle));
}

/* - Parsing function - */

void parse_packet(u_char *user, struct pcap_pkthdr *packethdr, u_char *packetptr)
{
struct ip* ip_header;
struct tcphdr* tcp_header;
char ip_header_info[256], source_ip[256], destination_ip[256];
unsigned short id, seq;
packetptr += linkhdrsize;
ip_header = (struct ip*)packetptr;
strcpy(source_ip, inet_ntoa(ip_header->ip_src));
strcpy(destination_ip, inet_ntoa(ip_header->ip_dst));
packetptr += 4*ip_header->ip_hl;
if((ip_header->ip_p)==IPPROTO_TCP)
    {
        tcp_header = (struct tcphdr*)packetptr;
        printf("TCP  %s:%d -> %s:%d\n", source_ip, ntohs(tcp_header->source),
               destination_ip, ntohs(tcp_header->dest));
        printf("%c%c%c%c%c%c Seq: 0x%x Ack: 0x%x Win: 0x%x TcpLen: %d\n",
               (tcp_header->urg ? 'U' : '*'),
               (tcp_header->ack ? 'A' : '*'),
               (tcp_header->psh ? 'P' : '*'),
               (tcp_header->rst ? 'R' : '*'),
               (tcp_header->syn ? 'S' : '*'),
               (tcp_header->fin ? 'F' : '*'),
               ntohl(tcp_header->seq), ntohl(tcp_header->ack_seq),
               ntohs(tcp_header->window), 4*tcp_header->doff);
}
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n\n");
}
void bailout(int signo)
{
    struct pcap_stat stats;

    if (pcap_stats(handle, &stats) >= 0)
    {
        printf("%d packets received\n", stats.ps_recv);
        printf("%d packets dropped\n\n", stats.ps_drop);
    }
    pcap_close(handle);
    exit(0);
}
/* - Main function - */
int main()
{
char interface[256]="eth0"; 
char filter[256]="tcp"; 
int packets=0;

if(( handle=packet_capture(interface,filter)) != NULL)
{
signal(SIGINT, bailout);
signal(SIGTERM, bailout);
signal(SIGQUIT, bailout);
capture_loop(handle,(pcap_handler)parse_packet);
bailout(0);
}
exit(0);
}

【问题讨论】:

  • 不知道你的意思,但如果虚拟机不能嗅探其他两个虚拟机的流量,这不是正确的行为吗?
  • @Olaf 但是另外两个虚拟机使用同一个网络,那是不是意味着攻击者如果嗅探网络,他应该也能找到他们的数据包?
  • 取决于网络代码是否模拟以太网交换机(相对于集线器)。在这种情况下,来自客户端 服务器的数据包将是点对点的。如今(通常)攻击者配置需要成为数据包路由拓扑中的一个路点。
  • 您是否将接口置于攻击者虚拟机内部的混杂模式(不确定 libpcap 是否为您这样做)?
  • 由于虚拟机都在同一台主机上运行,​​因此内核路由策略也可能只是在内部将数据包从一个本地主机套接字定向到另一个。在这种情况下,您也不会在客户端/服务器之间看到任何数据包。

标签: c linux ubuntu packet-capture packet-sniffers


【解决方案1】:

您在模拟 LAN 上。行为将根据 VM 桥接架构是模拟交换机还是集线器而改变:

  • 集线器会将接收到的数据包发送到所有输出端口
  • 交换机将使用与其 ARP 表中引用的目标 IP 地址关联的 MAC 地址

关于 cmets: AFAIK 中提到的“混杂模式”,它只允许网卡查看它接收到的数据包(在物理链路上),即使它们不是发给它的(在集线器的 cas 中)例如,它接收发送到不同 MAC 地址的数据包)。

在你的情况下,网卡只会看到:

  • 数据包地址到它的 MAC 地址
  • 从网卡发送到任何收件人的数据包
  • 已发送到广播 (MAC/IP) 的数据包

要让您的攻击者虚拟机查看所有数据包,您必须:

  • 在路由模式下将其用作默认网关(将看到所有数据包)
  • 欺骗对方主机的 MAC 地址(就像 Cain 一样)

基本上,您正在尝试用 C 语言编写轻量级 Wireshark/TCPdump,如果您查看它们的功能,您会发现它们不会比我上面描述的更多。

【讨论】:

  • 感谢您提供的解决方案,我尝试通过以下命令删除并添加攻击者 IP 作为默认网关:route add default gw x.x.x.x。然而,互联网中断了,Wireshark 根本没有捕获任何东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-12
  • 2011-09-29
  • 2015-04-02
  • 2020-06-06
  • 2014-04-08
相关资源
最近更新 更多