Libnidswin32的问题分析libnids编译连接出错和libnids抓不到包的问题(转)

一、Libnids问题之源

使用Libnids-win32 1.19在VC6.0环境下编程获取数据包,大部分代码都是按照《网络安全开发包详解》中的代码为例子的。但是往往编译连接出错,或者运行后Libnids抓不到包,先看程序框架:

------------------------------------------------------------------------------------------------------------

#include "nids.h"
#include <stdio.h>


//读取并分析获取的ASCII字符串内容
char* char_to_ascii(char ch)
{
......    //函数代码
}

//回调函数,分析TCP连接和TCP连接状态,并对TCP协议传输的数据进行分析
void tcp_protocol_callback(struct tcp_stream* tcp_connection, void** arg)
{
......    //回调函数代码
}

//主函数
void main()
{
nids_params.device = "2";
if(!nids_init())
{
   printf("出现错误:%s\n", nids_errbuf);
   exit(1);
}
nids_register_tcp(tcp_protocol_callback);
nids_run();
}

-------------------------------------------------------------------------------------------------------------

二、Libnids编译连接问题

已经把Libnids和Winpcap的头文件和库文件包含在了VC6.0的环境里,但还是出现如下类似问题,很让人棘手。

Linking...
error   LNK2001:   unresolved   external   symbol   "void   __cdecl   nids_run(void) "   (
?nids_run@@YAXXZ)
error   LNK2001:   unresolved   external   symbol   "void   __cdecl   nids_register_tcp(void   *) "   (
?nids_register_tcp@@YAXPAX@Z)
error   LNK2001:   unresolved   external   symbol   "char   *   nids_errbuf "   (
?nids_errbuf@@3PADA)
error   LNK2001:   unresolved   external   symbol   "int   __cdecl   nids_init(void) "   (
?nids_init@@YAHXZ)
Debug/test.exe   :   fatal   error   LNK1120:   4   unresolved   externals

解决办法如下:

在Libnids-1.19文件夹下,有一个“WIN32-Includes”文件夹,该文件夹存放的就是Libnids的头文件,进入后找到nids.h头文件,然后打开,找到int nids_init ();一行,在该行前加上如下代码:

#ifdef __cplusplus
extern "C" {
#endif

在头文件的最后一行,加上如下代码:

#ifdef __cplusplus
}
#endif

保存后进行编译,编译可以运行。

三、Libnids抓不到包的问题

运行后,一直抓不到数据包,但是用其他抓包软件抓包,比如Wireshark却能抓到。

这是因为我们的电脑中有一个适配器叫:Adapter for generic dialup and VPN capture,它是通用拨号和虚拟专用的适配器,我们要抓的并非通过它的数据包,而是是通过机器网卡的包。之所以抓不到包,因为Libnids默认选择了“Adapter for generic dialup and VPN capture”,所以我们在main()函数中第一行,添加一句代码:nids_params.device="2";将适配器设为真正起作用的设备。修改后的main()函数如下:

void main()
{
nids_params.device = "2";
if(!nids_init())
{
   printf("出现错误:%s\n", nids_errbuf);
   exit(1);
}
printf("初始化成功!\n");
nids_register_tcp(tcp_protocol_callback);
nids_run();
}

编译,运行,好了,尽情抓包吧。

原文地址:https://www.cnblogs.com/rainbowzc/p/1818657.html