📄 524.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>CTerm非常精华下载</title>
</head>
<body bgcolor="#FFFFFF">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="577">
<tr><td width="32%" rowspan="3" height="123"><img src="DDl_back.jpg" width="300" height="129" alt="DDl_back.jpg"></td><td width="30%" background="DDl_back2.jpg" height="35"><p align="center"><a href="http://apue.dhs.org"><font face="黑体"><big><big>apue</big></big></font></a></td></tr>
<tr>
<td width="68%" background="DDl_back2.jpg" height="44"><big><big><font face="黑体"><p align="center"> ● UNIX网络编程 (BM: clown) </font></big></big></td></tr>
<tr>
<td width="68%" height="44" bgcolor="#000000"><font face="黑体"><big><big><p align="center"></big></big><a href="http://cterm.163.net"><img src="banner.gif" width="400" height="60" alt="banner.gif"border="0"></a></font></td>
</tr>
<tr><td width="100%" colspan="2" height="100" align="center" valign="top"><br><p align="center">[<a href="index.htm">回到开始</a>][<a href="518.htm">上一层</a>][<a href="525.htm">下一篇</a>]
<hr><p align="left"><small>:网络安全工具开发函数库介绍之一 ——libnet(续二) <br>
---------------------------------------------------------------------------- <br>
---- <br>
: backend 于 00-7-15 17:52:05 加贴在 绿盟科技论坛(bbs.nsfocus.com)--UNIX系统安 <br>
:全: <br>
:绿色兵团版权所有。未经允许,严禁转载! <br>
============================ cut here ============================ <br>
:/* Example 2 [link layer api - ICMP_MASK] */ <br>
/* gcc -Wall `libnet-config --defines` \ <br>
libnet-example-x.c -o libnet-example-x \ <br>
`libnet-config --libs` */ <br>
#include <libnet.h> <br>
void usage(char *); <br>
u_char enet_src[6] = {0x0d, 0x0e, 0x0a, 0x0d, 0x00, 0x00}; <br>
u_char enet_dst[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; <br>
int <br>
main(int argc, char *argv[]) <br>
{ <br>
int packet_size, /* size of our packet */ <br>
c; /* misc */ <br>
u_long src_ip, dst_ip; /* source ip, dest ip */ <br>
u_char *packet; /* pointer to our packet buffer */ <br>
char err_buf[LIBNET_ERRBUF_SIZE]; /* error buffer */ <br>
u_char *device; /* pointer to the device to use */ <br>
struct libnet_link_int *network; /* pointer to link interface struct <br>
*/ <br>
printf("libnet example code:\tmodule 2\n\n"); <br>
printf("packet injection interface:\tlink layer\n"); <br>
printf("packet type:\t\t\tICMP net mask [no payload]\n"); <br>
device = NULL; <br>
src_ip = 0; <br>
dst_ip = 0; <br>
while ((c = getopt(argc, argv, "i:d:s:")) != EOF) <br>
{ <br>
switch (c) <br>
{ <br>
case 'd': <br>
if (!(dst_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE))) <br>
<br>
{ <br>
libnet_error(LIBNET_ERR_FATAL, <br>
"Bad destination IP address: %s\n", optarg) <br>
; <br>
} <br>
break; <br>
case 'i': <br>
device = optarg; <br>
break; <br>
case 's': <br>
if (!(src_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE))) <br>
<br>
{ <br>
libnet_error(LIBNET_ERR_FATAL, <br>
"Bad source IP address: %s\n", optarg); <br>
} <br>
break; <br>
default: <br>
exit(EXIT_FAILURE); <br>
} <br>
} <br>
if (!src_ip || !dst_ip) <br>
{ <br>
usage(argv[0]); <br>
exit(EXIT_FAILURE); <br>
} <br>
/* <br>
* Step 1: Network Initialization (interchangable with step 2). <br>
*/ <br>
if (device == NULL) <br>
{ <br>
struct sockaddr_in sin; <br>
/* <br>
* Try to locate a device. <br>
*/ <br>
if (libnet_select_device(&sin, &device, err_buf) == -1) <br>
{ <br>
libnet_error(LIBNET_ERR_FATAL, <br>
"libnet_select_device failed: %s\n", err_buf); <br>
} <br>
printf("device:\t\t\t\t%s\n", device); <br>
} <br>
if ((network = libnet_open_link_interface(device, err_buf)) == NULL) <br>
{ <br>
libnet_error(LIBNET_ERR_FATAL, <br>
"libnet_open_link_interface: %s\n", err_buf); <br>
} <br>
/* <br>
* We're going to build an ICMP packet with no payload using the <br>
* link-layer API, so this time we need memory for a ethernet header <br>
* as well as memory for the ICMP and IP headers. <br>
*/ <br>
packet_size = LIBNET_IP_H + LIBNET_ETH_H + LIBNET_ICMP_MASK_H; <br>
/* <br>
* Step 2: Memory Initialization (interchangable with step 1). <br>
*/ <br>
if (libnet_init_packet(packet_size, &packet) == -1) <br>
{ <br>
libnet_error(LIBNET_ERR_FATAL, "libnet_init_packet failed\n"); <br>
} <br>
/* <br>
* Step 3: Packet construction (ethernet header). <br>
*/ <br>
libnet_build_ethernet(enet_dst, <br>
enet_src, <br>
ETHERTYPE_IP, <br>
NULL, <br>
0, <br>
packet); <br>
/* <br>
* Step 3: Packet construction (ICMP header). <br>
*/ <br>
*/ <br>
libnet_build_icmp_mask(ICMP_MASKREPLY, /* type */ <br>
0, /* code */ <br>
242, /* id */ <br>
0, /* seq */ <br>
0xffffffff, /* mask */ <br>
NULL, /* payload */ <br>
0, /* payload_s */ <br>
packet + LIBNET_ETH_H + LIBNET_IP_H); <br>
/* <br>
* Step 3: Packet construction (IP header). <br>
*/ <br>
libnet_build_ip(ICMP_MASK_H, <br>
0, /* IP tos */ <br>
242, /* IP ID */ <br>
0, /* Frag */ <br>
64, /* TTL */ <br>
IPPROTO_ICMP, /* Transport protocol */ <br>
src_ip, /* Source IP */ <br>
dst_ip, /* Destination IP */ <br>
NULL, /* Pointer to payload (none) */ <br>
0, <br>
packet + LIBNET_ETH_H); /* Packet header memory */ <br>
/* <br>
* Step 4: Packet checksums (ICMP header *AND* IP header). <br>
*/ <br>
if (libnet_do_checksum(packet + ETH_H, IPPROTO_ICMP, LIBNET_ICMP_MASK_H) <br>
== -1) <br>
{ <br>
libnet_error(LIBNET_ERR_FATAL, "libnet_do_checksum failed\n"); <br>
} <br>
if (libnet_do_checksum(packet + ETH_H, IPPROTO_IP, LIBNET_IP_H) == -1) <br>
{ <br>
libnet_error(LIBNET_ERR_FATAL, "libnet_do_checksum failed\n"); <br>
} <br>
/* <br>
* Step 5: Packet injection. <br>
*/ <br>
c = libnet_write_link_layer(network, device, packet, packet_size); <br>
if (c < packet_size) <br>
{ <br>
libnet_error(LN_ERR_WARNING, <br>
"libnet_write_link_layer only wrote %d bytes\n", c); <br>
} <br>
else <br>
else <br>
{ <br>
printf("construction and injection completed, wrote all %d bytes\n", <br>
c); <br>
} <br>
/* <br>
* Shut down the interface. <br>
*/ <br>
if (libnet_close_link_interface(network) == -1) <br>
{ <br>
libnet_error(LN_ERR_WARNING, <br>
"libnet_close_link_interface couldn't close the interfa <br>
ce"); <br>
} <br>
/* <br>
* Free packet memory. <br>
*/ <br>
libnet_destroy_packet(&packet); <br>
return (c == -1 ? EXIT_FAILURE : EXIT_SUCCESS); <br>
} <br>
void usage(char *name) <br>
{ <br>
fprintf(stderr, "usage: %s [-i interface] -s s_ip -d d_ip\n", name); <br>
} <br>
============================ cut here ============================ <br>
:/* Example 3 [raw socket api - ICMP_ECHO using an arena] */ <br>
/* gcc -Wall `libnet-config --defines` \ <br>
libnet-example-x.c -o libnet-example-x \ <br>
`libnet-config --libs` */ <br>
#include <libnet.h> <br>
void usage(char *); <br>
int main(int argc, char **argv) <br>
{ <br>
int network, n, c, number_of_packets, packet_size; <br>
struct libnet_arena arena, *arena_p; <br>
u_char *packets[10]; <br>
u_long src_ip, dst_ip; <br>
printf("libnet example code:\tmodule 3\n\n"); <br>
printf("packet injection interface:\tlink layer\n"); <br>
printf("packet type:\t\t\tICMP_ECHO [no payload] using an arena\n"); <br>
src_ip = 0; <br>
dst_ip = 0; <br>
while((c = getopt(argc, argv, "d:s:")) != EOF) <br>
{ <br>
switch (c) <br>
{ <br>
case 'd': <br>
if (!(dst_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE))) <br>
<br>
{ <br>
libnet_error(LIBNET_ERR_FATAL, <br>
"Bad destination IP address: %s\n", optarg) <br>
; <br>
} <br>
break; <br>
case 's': <br>
if (!(src_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE))) <br>
<br>
{ <br>
libnet_error(LIBNET_ERR_FATAL, <br>
"Bad source IP address: %s\n", optarg); <br>
} <br>
break; <br>
} <br>
} <br>
if (!src_ip || !dst_ip) <br>
{ <br>
{ <br>
usage(argv[0]); <br>
exit(EXIT_FAILURE); <br>
} <br>
/* <br>
* We're just going to build an ICMP packet with no payload using the <br>
* raw sockets API, so we only need memory for a ICMP header and an IP <br>
* header. <br>
*/ <br>
packet_size = LIBNET_IP_H + LIBNET_ICMP_ECHO_H; <br>
/* <br>
* Let's just build say, 10 packets. <br>
*/ <br>
number_of_packets = 10; <br>
arena_p = &arena; <br>
if (libnet_init_packet_arena(&arena_p, number_of_packets, packet_size) = <br>
= -1) <br>
{ <br>
libnet_error(LIBNET_ERR_FATAL, "libnet_init_packet_arena failed\n"); <br>
<br>
} <br>
else <br>
{ <br>
{ <br>
printf("Allocated an arena of %ld bytes..\n", <br>
LIBNET_GET_ARENA_SIZE(arena)); <br>
} <br>
network = libnet_open_raw_sock(IPPROTO_RAW); <br>
if (network == -1) <br>
{ <br>
libnet_error(LIBNET_ERR_FATAL, "Can't open the network.\n"); <br>
} <br>
for (n = 0; n < number_of_packets; n++) <br>
{ <br>
printf("%ld bytes remaining in arena\n", <br>
LIBNET_GET_ARENA_REMAINING_BYTES(arena)); <br>
packets[n] = libnet_next_packet_from_arena(&arena_p, packet_size); <br>
if (!packets[n]) <br>
{ <br>
libnet_error(LIBNET_ERR_WARNING, "Arena is empty\n"); <br>
continue; <br>
} <br>
libnet_build_ip(ICMP_ECHO_H, /* Size of the payload * <br>
/ <br>
IPTOS_LOWDELAY | IPTOS_THROUGHPUT, /* IP tos */ <br>
242, /* IP ID */ <br>
0, /* frag stuff */ <br>
48, /* TTL */ <br>
IPPROTO_ICMP, /* transport protocol */ <br>
<br>
src_ip, /* source IP */ <br>
dst_ip, /* destination IP */ <br>
NULL, /* pointer to payload */ <br>
<br>
0, /* payload length */ <br>
packets[n]); /* packet header memory <br>
*/ <br>
libnet_build_icmp_echo(ICMP_ECHO, /* type */ <br>
0, /* code */ <br>
242, /* id */ <br>
5, /* seq */ <br>
NULL, /* pointer to payload */ <br>
<br>
0, /* payload length */ <br>
packets[n] + LIBNET_IP_H); /* packet header memory <br>
*/ <br>
if (libnet_do_checksum(packets[n], IPPROTO_ICMP, LIBNET_ICMP_ECHO_H) <br>
== -1) <br>
== -1) <br>
{ <br>
libnet_error(LIBNET_ERR_FATAL, "libnet_do_checksum failed\n"); <br>
} <br>
c = libnet_write_ip(network, packets[n], packet_size); <br>
if (c < packet_size) <br>
{ <br>
libnet_error(LN_ERR_WARNING, <br>
"libnet_write_ip only wrote %d bytes\n", c); <br>
} <br>
else <br>
{ <br>
printf("construction and injection of packet %d of %d completed, <br>
wrote all %d bytes\n", <br>
n + 1, number_of_packets, c); <br>
} <br>
} <br>
libnet_destroy_packet_arena(&arena_p); <br>
return (c == -1 ? EXIT_FAILURE : EXIT_SUCCESS); <br>
} <br>
void usage(char *name) <br>
{ <br>
fprintf(stderr, "usage: %s -s source_ip -d destination_ip\n ", name); <br>
} <br>
============================ cut here ============================ <br>
<<< 待续 >>> <br>
绿色兵团版权所有。未经允许,严禁转载! <br>
=== 说难不难,说易不易。=== <br>
</small><hr>
<p align="center">[<a href="index.htm">回到开始</a>][<a href="518.htm">上一层</a>][<a href="525.htm">下一篇</a>]
<p align="center"><a href="http://cterm.163.net">欢迎访问Cterm主页</a></p>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -