日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux进去网卡,Linux上使用socket进行网卡抓包

發布時間:2025/3/12 linux 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux进去网卡,Linux上使用socket进行网卡抓包 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Linux下使用socket進行網卡抓包

有時候需要自己編寫代碼進行抓包,以找出特殊意義的包。

下面是簡單的一個示例:

#include

#include

#include

#include

#include

#include

#include

int main (int argc, char *argv[])

{

int sockfd;

int ret = 0;

char buffer[1518] = {0};

char *eth_head = NULL;

/* PF_PACKET:鏈路層,? SOCK_RAW: 包含 mac頭, ETH_P_ALL: 所有協議??*/

if ((sockfd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)

{

printf ("create socket failed\n");

return -1;

}

while (1)

{

memset (buffer, 0x0, sizeof (buffer));

ret = recvfrom (sockfd, buffer, sizeof (buffer), 0, NULL, NULL);

printf ("recview package length : %d\n", ret);

eth_head = buffer;

printf ("PACKAGE START\n");

/* get source and dectination mac address */

printf ("dectination mac:%02x-%02x-%02x-%02x-%02x-%02x,"

"source mac:%02x-%02x-%02x-%02x-%02x-%02x;\n", eth_head[0],

eth_head[1], eth_head[2], eth_head[3], eth_head[4],

eth_head[5], eth_head[6], eth_head[7], eth_head[8],

eth_head[9], eth_head[10], eth_head[11]);

printf ("eth_type:%02x%02x\n", eth_head[12], eth_head[13]);

/* ARP proptocol flag */

if (0x08 == eth_head[12] && 0x06 == eth_head[13])

{

printf ("ARP source ip:%d.%d.%d.%d,destination ip:%d.%d.%d.%d;\n",

eth_head[28], eth_head[29], eth_head[30], eth_head[31],

eth_head[38], eth_head[39], eth_head[40], eth_head[41]);

}

/* IPv4 proptocol flag */

else if (0x08 == eth_head[12] && 0x00 == eth_head[13])

{

if (0x45 == eth_head[14])

{

printf ("IPv4 source ip:%d.%d.%d.%d,destination ip:%d.%d.%d."

"%d;\n", eth_head[26], eth_head[27], eth_head[28],

eth_head[29], eth_head[30], eth_head[31],

eth_head[32], eth_head[33]);

}

else

{

printf ("p_head:%02x\n", eth_head[14]);

}

}

printf ("PACKAGE END\n");

}

return 0;

}

總結

以上是生活随笔為你收集整理的linux进去网卡,Linux上使用socket进行网卡抓包的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。