C语言实现网络嗅探器

这里介绍一个用C语言和网络数据包和分析开发工具libpcap及winpcap实现的简易网络Sniffer。
2网络嗅探器程序实现
在c环境下编程,源码如下:
#include
#include
//必须加路径,必须把头文件packet32.h包含进去
#include "../../Include/packet32.h"
#include "../../Include/ntddndis.h"

#define Max_Num_Adapter 10
// Prototypes原形 

void PrintPackets(LPPACKET lpPacket);      //发包
char AdapterList[Max_Num_Adapter][1024];     //设备列表

// 主程序开始
int main()
{
LPADAPTER lpAdapter = 0;     //define a pointer to an ADAPTER structure设备指针
LPPACKET lpPacket;     //define a pointer to a PACKET structure包指针
int i;
DWORD dwErrorCode;
DWORD dwVersion;
DWORD dwWindowsMajorVersion;
WCHAR AdapterName[8192]; //网络适配器设备列表 //Unicode strings (WinNT)
WCHAR *temp,*temp1;

char AdapterNamea[8192]; //网络适配器设备列表 //ASCII strings (Win9x)
char *tempa,*temp1a;
int AdapterNum=0,Open;
ULONG AdapterLength;
char buffer[256000]; // 容纳来自驱动器的数据的缓冲区
struct bpf_stat stat;

// 获得本机网卡名
AdapterLength=4096;
printf("Packet.dll test application. Library version:%s/n", PacketGetVersion());
printf("Adapters installed:/n");
i=0;

下面这段代码是用来在不同版本下得到网络适配器名:
Win9x 和WinNT中的网卡名称是分别用ASCII和UNICODE实现的,所以首先要得到本地操作系统的版本号.:
dwVersion=GetVersion();
dwWindowsMajorVersion= (DWORD)(LOBYTE(LOWORD(dwVersion)));
这里首先用到的Packet.dll函数是PacketGetAdapterNames(PTSTR pStr,PULONG BufferSize,通常它是与驱动程序通信并被调用的第一个函数,它将返回的用户本地系统中安装的网络适配器的名字放在缓冲区pStr中;BufferSize是缓冲区的长度:

  • 3
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
编写网络程序需要使用底层的网络编程知识和socket编程知识。以下是一个简单的利用C语言编写的网络程序的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netinet/if_ether.h> #include <netinet/ip.h> #include <netinet/tcp.h> #include <netinet/udp.h> int main(int argc, char *argv[]) { int sockfd, n; char buf[2048]; struct sockaddr_in addr; struct ether_header *eth; struct iphdr *iph; struct tcphdr *tcph; struct udphdr *udph; sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); if (sockfd < 0) { perror("socket"); exit(1); } while (1) { n = recvfrom(sockfd, buf, sizeof(buf), 0, NULL, NULL); if (n < 42) continue; eth = (struct ether_header *)buf; if (ntohs(eth->ether_type) != ETHERTYPE_IP) continue; iph = (struct iphdr *)(buf + sizeof(struct ether_header)); if (iph->protocol == IPPROTO_TCP) { tcph = (struct tcphdr *)(buf + sizeof(struct ether_header) + sizeof(struct iphdr)); printf("TCP packet: %s:%d -> %s:%d\n", inet_ntoa(*(struct in_addr *)&iph->saddr), ntohs(tcph->source), inet_ntoa(*(struct in_addr *)&iph->daddr), ntohs(tcph->dest)); } else if (iph->protocol == IPPROTO_UDP) { udph = (struct udphdr *)(buf + sizeof(struct ether_header) + sizeof(struct iphdr)); printf("UDP packet: %s:%d -> %s:%d\n", inet_ntoa(*(struct in_addr *)&iph->saddr), ntohs(udph->source), inet_ntoa(*(struct in_addr *)&iph->daddr), ntohs(udph->dest)); } } close(sockfd); return 0; } ``` 该程序使用了Linux下的AF_PACKET套接字来实现网络,可以用来捕获传输层协议为TCP或UDP的数据包,并打印出源地址、源端口、目的地址和目的端口。需要注意的是,该程序需要在root权限下运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值