2015-11-19 15 views
2

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

我正在使用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); 
} 
+0

不确定你的意思,但不是正确的行为虚拟机无法嗅探其他两个虚拟机的交通? – Olaf

+0

@Olaf,但另外两个虚拟机使用相同的网络,如果他嗅探网络,那么这不就是攻击者的意思,他应该能够找到他们的数据包? – Arwa

+0

取决于网络代码是否模拟以太网交换机(vs集线器)。在那种情况下,来自客户端<->服务器的数据包将是点对点的。现在(通常),攻击者配置需要成为分组路由拓扑中的一个路标。 – par

回答

1

您位于仿真LAN中。行为将根据VM桥架构是否模拟交换机或集线器改变:

  • 中枢将recived数据包发送到所有输出端口
  • 交换机将目标与目标的IP相关的MAC地址在自己的ARP表

关于在评论中提到的“混杂模式”简称地址:据我所知,它不但可以让网卡来看看它接收(物理链路上),即使它们没有解决的包(在集线器的cas中,例如,它接收发送到不同MAC地址的数据包)。

在你的情况下,网络卡将只能看到:

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

为了使你的攻击者VM看到所有的数据包,则必须:

  • 用它作为路由模式的默认网关(会看到所有的数据包)
  • 欺骗对方主机的MAC地址(如该隐一样)

基本上,你试图写光Wireshark的/ TCPdump,如果你看看它们的功能,你会发现它们不会比我上面描述的做得更多。

+0

谢谢,对于提供的解决方案,我尝试使用此命令删除并添加攻击者IP作为默认网关:route add default gw x.x.x.x.然而,互联网走了,Wireshark没有捕获任何东西。 – Arwa