伪装飞秋发送者version2: 可快捷自定义发送接收双方

This commit is contained in:
flykhan 2023-09-12 09:52:00 +08:00
parent 245ea3738e
commit 6269ec8a5c
1 changed files with 64 additions and 14 deletions

View File

@ -19,6 +19,7 @@
#include <pthread.h> #include <pthread.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <stdlib.h> // system
ssize_t send_datapacket(int fd, unsigned char *buf, ssize_t buf_size, const char *ether_name); ssize_t send_datapacket(int fd, unsigned char *buf, ssize_t buf_size, const char *ether_name);
unsigned short checksum(unsigned short *buf, int len); unsigned short checksum(unsigned short *buf, int len);
@ -58,13 +59,35 @@ int main(int argc, char const *argv[])
bzero(&sll, sizeof(sll)); bzero(&sll, sizeof(sll));
sll.sll_ifindex = ether_req.ifr_ifindex; // 将网卡的接口类型赋值给发送接口 */ sll.sll_ifindex = ether_req.ifr_ifindex; // 将网卡的接口类型赋值给发送接口 */
char sender_ip[128] = "";
printf("请输入要伪装的发送者IP: ");
scanf("%s", sender_ip);
char sender_mac[128] = "";
printf("请输入要伪装的发送者MAC: ");
scanf("%s", sender_mac);
char receiver_ip[128] = "";
printf("请输入要伪装的接收者IP: ");
scanf("%s", receiver_ip);
char recv_mac[128] = "";
printf("请输入要伪装的接收者MAC: ");
scanf("%s", recv_mac);
char sender_name[128] = ""; char sender_name[128] = "";
printf("请输入要伪装的发送者名字:"); printf("请输入要伪装的发送者名字: ");
scanf("%s", sender_name); scanf("%s", sender_name);
char sender_pc_name[128] = ""; char sender_pc_name[128] = "";
printf("请输入要伪装的发送者电脑名字:"); printf("请输入要伪装的发送者电脑名字: ");
scanf("%s", sender_pc_name); scanf("%s", sender_pc_name);
// mac 地址格式化
unsigned char dst_mac[8] = {0}; // 目的 MAC
unsigned char src_mac[8] = {0}; // 发送者 MAC
sscanf(sender_mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &src_mac[0], &src_mac[1], &src_mac[2], &src_mac[3], &src_mac[4], &src_mac[5]);
sscanf(recv_mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &dst_mac[0], &dst_mac[1], &dst_mac[2], &dst_mac[3], &dst_mac[4], &dst_mac[5]);
while (1) while (1)
{ {
@ -82,11 +105,35 @@ int main(int argc, char const *argv[])
char msg_data[1024] = ""; char msg_data[1024] = "";
char say_what[1024] = ""; char say_what[1024] = "";
printf("请输入要发送的内容:"); printf("请输入要发送的内容:");
scanf("%s", say_what); // scanf("%s", say_what);
fgets(say_what, sizeof(say_what), stdin); // 比 scanf 的优点是可以输入空格
say_what[strlen(say_what) - 1] = '\0'; // 去掉最后的换行符
if (strcmp(say_what, "exit") == 0) if (strcmp(say_what, "exit") == 0)
{ {
break; break;
} }
if (strcmp(say_what, "clear") == 0)
{
system("clear");
continue;
}
/* if (strncpy(say_what, "change", 6) == 0)
{
printf("请输入要伪装的发送者名字:");
scanf("%s", sender_name);
printf("请输入要伪装的发送者电脑名字:");
scanf("%s", sender_pc_name);
continue;
}
if (strncmp(say_what, "help", 4) == 0)
{
printf("请输入要发送的内容:\n");
printf("1. 输入exit退出\n");
printf("2. 输入clear清屏\n");
printf("3. 输入change更改伪装信息\n");
continue;
} */
sprintf(msg_data, "1:%d:%s:%s:%d:%s", 123, sender_name, sender_pc_name, 32, say_what); sprintf(msg_data, "1:%d:%s:%s:%d:%s", 123, sender_name, sender_pc_name, 32, say_what);
int msg_data_len = strlen(msg_data) + strlen(msg_data) % 2; // 整数补齐偶数位strlen(msg_data)%2 : 偶数+0奇数+1 int msg_data_len = strlen(msg_data) + strlen(msg_data) % 2; // 整数补齐偶数位strlen(msg_data)%2 : 偶数+0奇数+1
printf("msg_data_len = %d ---> %s\n", msg_data_len, msg_data); printf("msg_data_len = %d ---> %s\n", msg_data_len, msg_data);
@ -104,10 +151,13 @@ int main(int argc, char const *argv[])
UDP pseudo header UDP 使 IP IP UDP UDP UDP pseudo header UDP 使 IP IP UDP UDP
UDP UDP 使 UDP UDP 使
*/ */
// 196: 4c:e1:73:47:16:3a
// 158: e8:d8:d1:48:46:ae
unsigned char pseudo_udp_buf[1056] = ""; // 伪头部 unsigned char pseudo_udp_buf[1056] = ""; // 伪头部
PSEUDO_UDP_HEAD *pseudo_udp_head = (PSEUDO_UDP_HEAD *)pseudo_udp_buf; PSEUDO_UDP_HEAD *pseudo_udp_head = (PSEUDO_UDP_HEAD *)pseudo_udp_buf;
pseudo_udp_head->saddr = inet_addr("192.168.31.155"); // 伪装源IP pseudo_udp_head->saddr = inet_addr(sender_ip); // 伪装源IP
pseudo_udp_head->daddr = inet_addr("192.168.31.147"); // 目的IP pseudo_udp_head->daddr = inet_addr(receiver_ip); // 目的IP
pseudo_udp_head->flag = 0; // 0 pseudo_udp_head->flag = 0; // 0
pseudo_udp_head->protocol = 17; // 协议类型 UDP pseudo_udp_head->protocol = 17; // 协议类型 UDP
pseudo_udp_head->len = htons(8 + msg_data_len); // UDP 首部长度 + 数据部分长度 pseudo_udp_head->len = htons(8 + msg_data_len); // UDP 首部长度 + 数据部分长度
@ -130,14 +180,14 @@ int main(int argc, char const *argv[])
ip_head->protocol = 17; // 协议类型 UDP ip_head->protocol = 17; // 协议类型 UDP
// ip 校验时不需要伪头部 // ip 校验时不需要伪头部
ip_head->check = htons(0); // 原始校验和暂时未知赋值0 ip_head->check = htons(0); // 原始校验和暂时未知赋值0
ip_head->saddr = inet_addr("192.168.31.155"); // 伪装源IP ip_head->saddr = inet_addr(sender_ip); // 伪装源IP
ip_head->daddr = inet_addr("192.168.31.147"); // 目的IP ip_head->daddr = inet_addr(receiver_ip); // 目的IP
ip_head->check = checksum((unsigned short *)(udp_buf + 14), 20); // IP 首部校验和(计算 IP 首部的校验和) ip_head->check = checksum((unsigned short *)(udp_buf + 14), 20); // IP 首部校验和(计算 IP 首部的校验和)
/* ---------------------组装 UDP 数据报的 MAC 首部--------------------- */ /* ---------------------组装 UDP 数据报的 MAC 首部--------------------- */
struct ether_header *mac_head = (struct ether_header *)udp_buf; // MAC 首部开始位置 struct ether_header *mac_head = (struct ether_header *)udp_buf; // MAC 首部开始位置
unsigned char dst_mac[8] = {0x00, 0xa5, 0x54, 0x89, 0xb5, 0xc5}; // 主机 MAC (目的 MAC) // unsigned char dst_mac[8] = {0xe8, 0x6a, 0x64, 0x6e, 0x93, 0x28}; // 主机 MAC (目的 MAC)
unsigned char src_mac[8] = {0xb8, 0x8a, 0x60, 0xa0, 0x49, 0x2f}; // 虚拟机 MAC (源 MAC) // unsigned char src_mac[8] = {0x4c, 0xe1, 0x73, 0x47, 0x16, 0x3a}; // 虚拟机 MAC (源 MAC)
// 使用 memcpy 函数将 dst_mac 的内容拷贝到 mac_head.ether_dhost 中 // 使用 memcpy 函数将 dst_mac 的内容拷贝到 mac_head.ether_dhost 中
memcpy(mac_head->ether_dhost, dst_mac, 6); // 目的 MAC memcpy(mac_head->ether_dhost, dst_mac, 6); // 目的 MAC
memcpy(mac_head->ether_shost, src_mac, 6); // 源 MAC memcpy(mac_head->ether_shost, src_mac, 6); // 源 MAC
@ -145,7 +195,7 @@ int main(int argc, char const *argv[])
/* ---------------------发送数据--------------------- */ /* ---------------------发送数据--------------------- */
// int send_len = sendto(sock_fd, udp_buf, 14 + 20 + 8 + msg_data_len, 0, (struct sockaddr *)&sll, sizeof(sll)); // int send_len = sendto(sock_fd, udp_buf, 14 + 20 + 8 + msg_data_len, 0, (struct sockaddr *)&sll, sizeof(sll));
int send_len = send_datapacket(sock_fd, udp_buf, 14 + 20 + 8 + msg_data_len, "ens34"); int send_len = send_datapacket(sock_fd, udp_buf, 14 + 20 + 8 + msg_data_len, "ens38");
printf("send_len = %d\n", send_len); printf("send_len = %d\n", send_len);
} }