# 網絡逆向分析實驗3 ## 實驗簡介 > 嘗試採用libnet向外部網絡發出構造的測試數據包(低速率)發送速率模式可以採用泊松及On /Off模型 ## 實驗環境 * VirtualBox: Ubuntu * Windows * WireShark * Libnet * GCC ## 實驗流程 > 本實驗是在Ubuntu和Windows兩個平台上實現的。 1. 在Ubuntu上安裝Libnet > sudo apt-get install libnet-dev 2. 在Ubuntu使用編寫C語言編寫Libnet程序 ```c= #include <stdio.h> #include <string.h> #include <libnet.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <math.h> #include <limits.h> void Sleep(int t); int Fac(int N); void P(int X, libnet_t *); int main() { //1.初始化内存 libnet_t *lib_handle = libnet_init(LIBNET_LINK_ADV,"enp0s3",NULL); //获取键盘输入 char data[128]="Hello! Libnet"; // printf("请输入数据:\n"); // fgets(data,sizeof(data),stdin); data[strlen(data)]=0; int data_len = strlen(data)+strlen(data)%2; //将长度变为偶数 //2.构建数据包:从应用层--->链路层 //构建UDP数据(传输层) libnet_ptag_t ptag_udp =libnet_build_udp(8000,9000,8+data_len,0,data,data_len,lib_handle,0); //构建IPv4数据(网络层) libnet_ptag_t ptag_ip = libnet_build_ipv4(20+8+data_len,0,0,0,128,17,0,inet_addr("192.168.56.102"),inet_addr("192.168.56.1"),NULL,0,lib_handle,0); //构建以太网数据包(链路层) unsigned char src_mac[6]={0x08, 0x00, 0x27, 0x5f, 0x56, 0x19}; //虚拟机mac unsigned char dst_mac[6]={0xd0, 0x57, 0x7b, 0x1c, 0x08, 0x81};//windows的mac libnet_ptag_t ptag_mac = libnet_build_ethernet(dst_mac,src_mac,0x0800,NULL,0,lib_handle,0); int times = 15; int delay = 10000000; for (int k = 0; k < times; k++) { //3.发送帧数据 P(k, lib_handle); Sleep(delay); } //4.释放资源 libnet_destroy(lib_handle); puts("Success!"); return 0; } void Sleep(int t) { for (int i = 0; i < t; i++) { puts("Waiting!"); } } int Fac(int N) { if (N == 1 || N == 0) return 1; int n = 1; for (int i = 2; i <= N; i++) { n *= i; } return n; } void P(int X, libnet_t * lib_handle) { float e = 2.718282; float p = pow(4, X) / Fac(X); p *= pow(e, -4); p *= 100000; printf("%f\n", p); for (int i = 1; i <= p; i++) { puts("Writting!"); libnet_write(lib_handle); } } ``` 3. 編譯Libnet代碼 ![](https://i.imgur.com/ozcY05I.png) 4. 利用WireShark抓Libnet發送的包, ![](https://i.imgur.com/gVyTU0Y.png) ![](https://i.imgur.com/XN6bdBF.png)