一个VC写的完整、简单的Sniffer代码

简介: 一个VC写的完整、简单的Sniffer代码

以下完整、简单的Sniffer代码代码是用SOCK_RAW写的.SP2已经不支持RAW

#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_HOSTNAME_LAN 255
#define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
#define MAX_ADDR_LEN 16
typedef struct tcpheader {
     unsigned short int sport;
     unsigned short int dport;
     unsigned int th_seq;
     unsigned int th_ack;
     unsigned char th_x2:4;
     unsigned char th_off:4;
     unsigned char Flags;
     unsigned short int th_win;
     unsigned short int th_sum;
     unsigned short int th_urp;
}TCP_HDR;
struct ipheader {
unsigned char ip_hl:4, ip_v:4; /* this means that each member is 4 bits */
unsigned char ip_tos;
unsigned short int ip_len;
unsigned short int ip_id;
unsigned short int ip_off;
unsigned char ip_ttl;
unsigned char ip_p;
unsigned short int ip_sum;
unsigned int ip_src;
unsigned int ip_dst;
}; /* total ip header length: 20 bytes (=160 bits) */
// Psuedo Header
typedef struct ps_hdr
{
    unsigned int   source_address;   // Source Address         =>      4 Bytes
    unsigned int   dest_address;     // Destination Address     =>      4 Bytes
    unsigned char  placeholder;         // Place Holder         =>      1 Bytes
    unsigned char  protocol;         // Protocol         =>      1 Bytes
    unsigned short tcp_length;         // TCP Length         =>    +  2 Bytes
                     //                       = 12 Bytes
    struct tcpheader tcp;
}PS_HDR;
typedef struct udphdr {
unsigned short sport;
unsigned short dport;
unsigned short len;
unsigned short cksum;
}UDP_HDR;
void hexdump(char *pointer)
{
    if ((*(pointer)>0))
    printf("//x%2.2i",*(pointer));
    else
    printf("//x%2.2i",(*(pointer))*(-1)+82);
}
void main()
{
    SOCKET sock;
    WSADATA wsd;
    char RecvBuf[65535] = {0};
    DWORD  dwBytesRet;
    int pCount=0;
    unsigned int  optval = 1; //the pointer , which shows us the payload begin
    unsigned char *datatcp=NULL; //the pointer , which shows us the payload begin
    unsigned char *dataudp=NULL;
    int lentcp=0, lenudp;
    WSAStartup(MAKEWORD(2,1),&wsd);
    if((sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP))==SOCKET_ERROR)
    {
        exit(1);
    }
    char FAR name[MAX_HOSTNAME_LAN];
    gethostname(name, MAX_HOSTNAME_LAN);
    struct hostent FAR * pHostent;
    pHostent = (struct hostent * )malloc(sizeof(struct hostent));
    pHostent = gethostbyname(name);
    SOCKADDR_IN sa;
    sa.sin_family = AF_INET;
    sa.sin_port = htons(6000);
    memcpy(&sa.sin_addr.S_un.S_addr, pHostent->h_addr_list[0], pHostent->h_length);
    bind(sock, (SOCKADDR *)&sa, sizeof(sa));
    //if you don't have raw socket support (win 95/98/me/win2kuser) it calls the exit(1) function
    if ((WSAGetLastError())==10013)
    exit(1);
    WSAIoctl(sock, SIO_RCVALL, &optval, sizeof(optval), NULL, 0, &dwBytesRet, NULL, NULL);
    struct udphdr *pUdpheader;
    struct ipheader *pIpheader;
    struct tcpheader *pTcpheader;
    char szSourceIP[MAX_ADDR_LEN], szDestIP[MAX_ADDR_LEN];
    SOCKADDR_IN saSource, saDest;
    pIpheader = (struct ipheader *)RecvBuf;
    pTcpheader = (struct tcpheader *)(RecvBuf+ sizeof(struct ipheader ));
    pUdpheader = (struct udphdr *) (RecvBuf+ sizeof(struct ipheader ));
    while (1)
    {
       
        memset(RecvBuf, 0, sizeof(RecvBuf));
        recv(sock, RecvBuf, sizeof(RecvBuf), 0);
        saSource.sin_addr.s_addr = pIpheader->ip_src;
        strncpy(szSourceIP, inet_ntoa(saSource.sin_addr), MAX_ADDR_LEN);
        //Check Dest IP
        saDest.sin_addr.s_addr = pIpheader->ip_dst;
        strncpy(szDestIP, inet_ntoa(saDest.sin_addr), MAX_ADDR_LEN);
       
        lentcp =(ntohs(pIpheader->ip_len)-(sizeof(struct ipheader)+sizeof(struct tcpheader)));    
        lenudp =(ntohs(pIpheader->ip_len)-(sizeof(struct ipheader)+sizeof(struct udphdr)));        
        if(    (pIpheader->ip_p)==IPPROTO_TCP&&lentcp!=0)
        {
            printf("*******************************************/n");
            pCount++;    
            datatcp=(unsigned char *) RecvBuf+sizeof(struct ipheader)+sizeof(struct tcpheader);
            printf("-TCP-/n");                    
            printf("/nDestination address->%s/n",szDestIP);
            printf("/nDestination port->%i/n",ntohs(pTcpheader->dport));
            printf("datatcp address->%x/n",datatcp);
            printf("size of ipheader->%i/n",sizeof(struct ipheader));
            printf("size of tcpheader->%i/n",sizeof(struct tcpheader));
            printf("size of the hole packet->%i/n",ntohs(pIpheader->ip_len));
            printf("/nchar Packet%i []=/"",pCount,lentcp);
            for (int i=0;i<lentcp;i++)
            {
                printf("//x%.2x",*(datatcp+i)); //hexdump(datatcp+i);
                if(i%10==0)
                {                        
                    printf("/"");
                    printf("/n/"");
                }
            }
            printf("/";/n/n/n");
            for (int i2=0;i2<lentcp;i2++)
            {
                if( *(datatcp+i2)<=127&&*(datatcp+i2)>=20)
                    printf("%c",*(datatcp+i2));
                else
                    printf(".");
            }
            printf("/n/n");
            printf("*******************************************/n");    
        }
        if(    (pIpheader->ip_p)==IPPROTO_UDP&&lentcp!=0)
        {
            pCount++;                        
            dataudp=(unsigned char *) RecvBuf+sizeof(struct ipheader)+sizeof(struct udphdr);
            printf("-UDP-/n");
            printf("/nDestination address->%s/n",szDestIP);
            printf("/nDestination port->%d/n",ntohs(pTcpheader->dport));
            printf("dataudp address->%x/n",dataudp);
            printf("size of ipheader->%i/n",sizeof(struct ipheader));
            printf("size of udpheader->%i/n",sizeof(struct udphdr));
            printf("size of the hole packet->%i/n",ntohs(pIpheader->ip_len));
            printf("/nchar Packet%i []=/"",pCount,lenudp);
            for (int x=0;x<lenudp;x++)
            {
                printf("//x%.2x",*(dataudp+x));
                if (x%10==0)
                {                        
                    printf("/"");
                    printf("/n/"");
                }
            }
            printf("/";/n/n/n");
            for (int x2=0;x2<lenudp;x2++)
            {
                if( *(dataudp+x2)<=127&&*(dataudp+x2)>=20)
                    printf("%c",*(dataudp+x2));
                else
                    printf(".");
            }
            printf("/n/n");
            printf("*******************************************/n");
        }
    }
要用"伪造数据包"的方法,来禁止一切TCP连接,用Winpcap改写的代码为:
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib,"ws2_32.lib")
#define MAX_HOSTNAME_LAN 255
#define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
#define MAX_ADDR_LEN 16
struct ipheader {
unsigned char ip_hl:4, ip_v:4; /* this means that each member is 4 bits */
unsigned char ip_tos;
unsigned short int ip_len;
unsigned short int ip_id;
unsigned short int ip_off;
unsigned char ip_ttl;
unsigned char ip_p;
unsigned short int ip_sum;
unsigned int ip_src;
unsigned int ip_dst;
}; /* total ip header length: 20 bytes (=160 bits) */
typedef struct tcpheader {
unsigned short int sport;
unsigned short int dport;
unsigned int th_seq;
unsigned int th_ack;
unsigned char th_x:4;
unsigned char th_off:4;
unsigned char Flags;
unsigned short int th_win;
unsigned short int th_sum;
unsigned short int th_urp;
}TCP_HDR;
typedef struct udphdr {
unsigned short sport;
unsigned short dport;
unsigned short len;
unsigned short cksum;
}UDP_HDR;
void main()
{
SOCKET sock;
WSADATA wsd;
DWORD dwBytesRet;
unsigned int optval = 1;
unsigned char *dataudp,*datatcp;
int i,pCount=0,lentcp, lenudp;
SOCKADDR_IN sa,saSource, saDest;
struct hostent FAR * pHostent;
char FAR name[MAX_HOSTNAME_LAN];
char szSourceIP[MAX_ADDR_LEN], szDestIP[MAX_ADDR_LEN],RecvBuf[65535] = {0};
struct udphdr *pUdpheader;
struct ipheader *pIpheader;
struct tcpheader *pTcpheader;
WSAStartup(MAKEWORD(2,1),&wsd);
if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP))==SOCKET_ERROR)exit(1);
gethostname(name, MAX_HOSTNAME_LAN);
pHostent = gethostbyname(name);
sa.sin_family = AF_INET;
sa.sin_port = htons(6000);
memcpy(&sa.sin_addr.S_un.S_addr, pHostent->h_addr_list[0], pHostent->h_length);
bind(sock, (SOCKADDR *)&sa, sizeof(sa));
if ((WSAGetLastError())==10013)exit(1);
WSAIoctl(sock, SIO_RCVALL, &optval, sizeof(optval), NULL, 0, &dwBytesRet, NULL, NULL);
pIpheader = (struct ipheader *)RecvBuf;
pTcpheader = (struct tcpheader *)(RecvBuf+ sizeof(struct ipheader ));
pUdpheader = (struct udphdr *) (RecvBuf+ sizeof(struct ipheader ));
while (1)
{
memset(RecvBuf, 0, sizeof(RecvBuf));
recv(sock, RecvBuf, sizeof(RecvBuf), 0);
saSource.sin_addr.s_addr = pIpheader->ip_src;
strncpy(szSourceIP, inet_ntoa(saSource.sin_addr), MAX_ADDR_LEN);
saDest.sin_addr.s_addr = pIpheader->ip_dst;
strncpy(szDestIP, inet_ntoa(saDest.sin_addr), MAX_ADDR_LEN);
lentcp =(ntohs(pIpheader->ip_len)-(sizeof(struct ipheader)+sizeof(struct tcpheader)));
lenudp =(ntohs(pIpheader->ip_len)-(sizeof(struct ipheader)+sizeof(struct udphdr)));
if((pIpheader->ip_p)==IPPROTO_TCP&&lentcp!=0)
{
printf("*******************************************/n");
pCount++;
datatcp=(unsigned char *) RecvBuf+sizeof(struct ipheader)+sizeof(struct tcpheader);
printf("-TCP-/n");
printf("/nDestination address->%s/n",szDestIP);
printf("/nDestination port->%i/n",ntohs(pTcpheader->dport));
printf("datatcp address->%x/n",datatcp);
printf("size of ipheader->%i/n",sizeof(struct ipheader));
printf("size of tcpheader->%i/n",sizeof(struct tcpheader));
printf("size of the hole packet->%i/n",ntohs(pIpheader->ip_len));
printf("/nchar Packet%i [%i]=/"",pCount,lentcp-1);
for (i=0;i<lentcp;i++)
{
printf("//x%.2x",*(datatcp+i));
if (i%10==0)printf("/"/n/"");
}
printf("/";/n/n/n");
for (i=0;i<lentcp;i++)
{
if( *(datatcp+i)<=127&&*(datatcp+i)>=20)printf("%c",*(datatcp+i));
else printf(".");
}
printf("/n/n*******************************************/n");
}
if((pIpheader->ip_p)==IPPROTO_UDP&&lentcp!=0)
{
pCount++;
dataudp=(unsigned char *) RecvBuf+sizeof(struct ipheader)+sizeof(struct udphdr);
printf("-UDP-/n");
printf("/nDestination address->%s/n",szDestIP);
printf("/nDestination port->%d/n",ntohs(pTcpheader->dport));
printf("dataudp address->%x/n",dataudp);
printf("size of ipheader->%i/n",sizeof(struct ipheader));
printf("size of udpheader->%i/n",sizeof(struct udphdr));
printf("size of the hole packet->%i/n",ntohs(pIpheader->ip_len));
printf("/nchar Packet%i [%i]=/"",pCount,lenudp-1);
for (i=0;i<lenudp;i++)
{
printf("//x%.2x",*(dataudp+i));
if (i%10==0)printf("/"/n/"");
}
printf("/";/n/n/n");
for (i=0;i<lenudp;i++)
{
if( *(dataudp+i)<=127&&*(dataudp+i)>=20)printf("%c",*(dataudp+i));
else printf(".");
}
printf("/n/n*******************************************/n");
}
}
}
相关文章
|
8天前
|
人工智能 安全 API
CoPaw:5分钟部署你的 AI助理
源自阿里巴巴开源生态的个人 AI 助理——CoPaw。作为阿里倾力打造的开源力作,CoPaw 完美打通钉钉、飞书、Discord 等多平台对话通道,支持定时任务自动化。内置 PDF/Office 深度处理、新闻摘要等强大技能,更开放自定义扩展接口。坚持数据全程私有化部署,绝不上传云端,让每一位用户都能在大厂技术加持下,拥有安全、专属的智能助手。
|
11天前
|
人工智能 自然语言处理 JavaScript
2026年Windows+Ollama本地部署OpenClaw保姆级教程:本地AI Agent+阿里云上快速搭建
2026年OpenClaw凭借本地部署、私有化运行的特性,成为打造个人智能体的核心工具,而Ollama作为轻量级本地大模型管理工具,能让OpenClaw摆脱对云端大模型的依赖,实现**本地推理、数据不泄露、全流程私有化**的智能体验。本文基于Windows 11系统,从硬件环境准备、Ollama安装与模型定制、OpenClaw部署配置、技能扩展到常见问题排查,打造保姆级本地部署教程,同时补充阿里云OpenClaw(Clawdbot)快速部署步骤,兼顾本地私有化需求与云端7×24小时运行需求,文中所有代码命令均可直接复制执行,确保零基础用户也能快速搭建属于自己的本地智能体。
12964 101
|
7天前
|
人工智能 JavaScript Ubuntu
5分钟上手龙虾AI!OpenClaw部署(阿里云+本地)+ 免费多模型配置保姆级教程(MiniMax、Claude、阿里云百炼)
OpenClaw(昵称“龙虾AI”)作为2026年热门的开源个人AI助手,由PSPDFKit创始人Peter Steinberger开发,核心优势在于“真正执行任务”——不仅能聊天互动,还能自动处理邮件、管理日程、订机票、写代码等,且所有数据本地处理,隐私完全可控。它支持接入MiniMax、Claude、GPT等多类大模型,兼容微信、Telegram、飞书等主流聊天工具,搭配100+可扩展技能,成为兼顾实用性与隐私性的AI工具首选。
8647 19
|
9天前
|
人工智能 安全 JavaScript
阿里云上+本地部署OpenClaw(小龙虾)新手攻略:解锁10大必备Skills,零基础也能玩转AI助手
2026年,开源AI代理工具OpenClaw(昵称“小龙虾”)凭借“能实际做事”的核心优势,在GitHub斩获25万+星标,成为现象级AI工具。它最强大的魅力在于可扩展的Skills(技能包)系统——通过ClawHub插件市场的数百个技能,能让AI助手从简单聊天升级为处理办公、学习、日常事务的全能帮手。
9193 36
|
3天前
|
人工智能 安全 前端开发
Team 版 OpenClaw:HiClaw 开源,5 分钟完成本地安装
HiClaw 基于 OpenClaw、Higress AI Gateway、Element IM 客户端+Tuwunel IM 服务器(均基于 Matrix 实时通信协议)、MinIO 共享文件系统打造。
3734 8
|
10天前
|
人工智能 自然语言处理 机器人
保姆级教程:Mac本地搭建OpenClaw及阿里云上1分钟部署OpenClaw+飞书集成实战指南
OpenClaw(曾用名Clawdbot、Moltbot)作为2026年最热门的开源个人AI助手平台,以“自然语言驱动自动化”为核心,支持对接飞书、Telegram等主流通讯工具,可替代人工完成文件操作、日历管理、邮件处理等重复性工作。其模块化架构适配多系统环境,既可以在Mac上本地化部署打造私人助手,也能通过阿里云实现7×24小时稳定运行,完美兼顾隐私性与便捷性。
7784 19
|
5天前
|
人工智能 JavaScript 测试技术
保姆级教程:OpenClaw阿里云及本地部署+Claude Code集成,打造全能 AI 编程助手
在AI编程工具百花齐放的2026年,Anthropic推出的Claude Code凭借72.5%的SWE-bench测试高分、25倍于GitHub Copilot的上下文窗口,成为开发者追捧的智能编程助手。但单一工具仍有局限——Claude Code擅长代码生成与审查,却缺乏灵活的部署与自动化执行能力;而OpenClaw(前身为Clawdbot)作为开源AI代理框架,能完美弥补这一短板,通过云端与本地双部署,实现“代码开发-测试-部署”全流程自动化。
2495 13
|
5天前
|
人工智能 JSON API
保姆级教程:OpenClaw阿里云及本地部署+模型切换流程+GLM5.0/Seedance2.0/MiniMax M2.5接入指南
2026年,GLM5.0、Seedance2.0、MiniMax M2.5等旗舰大模型相继发布,凭借出色的性能与极具竞争力的成本优势,成为AI工具的热门选择。OpenClaw作为灵活的AI Agent平台,支持无缝接入这些主流模型,通过简单配置即可实现“永久切换、快速切换、主备切换”三种模式,让不同场景下的任务执行更高效、更稳定。
3235 3

热门文章

最新文章