2021-11-02 17:39:42 +08:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "tsg_entry.h"
|
2021-11-08 18:56:07 +08:00
|
|
|
|
#include <MESA/MESA_handle_logger.h>
|
2021-11-02 17:39:42 +08:00
|
|
|
|
#include "tsg_protocol_common.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define MTU_LEN 65535
|
|
|
|
|
|
#define MAC_LEN 6
|
|
|
|
|
|
#define MAC_LEN_2 ((MAC_LEN)+(MAC_LEN))
|
|
|
|
|
|
#define ETH_IP_TYPE_LEN 2
|
|
|
|
|
|
#define ETH_LEN ((MAC_LEN_2)+(ETH_IP_TYPE_LEN))
|
|
|
|
|
|
|
|
|
|
|
|
#define IPV4_TYPE 1 //ADDR_TYPE_IPV4 ==1 , 取的enum 0x0800
|
|
|
|
|
|
#define IPV6_TYPE 2 //ADDR_TYPE_IPV6 ==2 0x86dd
|
|
|
|
|
|
#define TCP_TYPE 0x06
|
|
|
|
|
|
#define UDP_TYPE 0x11
|
|
|
|
|
|
|
2021-11-04 15:04:50 +08:00
|
|
|
|
#define IPV4_LEN 20 //ip_len(20)
|
2021-11-02 17:39:42 +08:00
|
|
|
|
#define IPV4_PROTOCOL_INDEX 9 //ipv4_protocol_index_len
|
|
|
|
|
|
#define IPV4_TCP_HEAD_LEN_INDEX 32 //ip_len(20) + tcp_head_len_index()
|
|
|
|
|
|
#define ETH_IPV4_IP_UPD_LEN 28 //ip_len(20) + udp_len(8)
|
|
|
|
|
|
#define IPV4_IP_LEN_INDEX 2 //ip_len_index(2)
|
|
|
|
|
|
|
|
|
|
|
|
#define IPV6_PROTOCOL_INDEX 6 //ipv6_protocol_index(6)
|
|
|
|
|
|
#define IPV6_LEN 40
|
|
|
|
|
|
#define ETH_IPV6_LEN 40 //ipv6_len(40)
|
|
|
|
|
|
#define IPV6_TCP_OPTION_LEN_INDEX 52 //ipv6_len(40) + tcp_head_len_index(12)
|
|
|
|
|
|
#define IPV6_UDP_PALYLOAD_START_INDEX 48 //ipv6_len(40) + udp_len(8)
|
|
|
|
|
|
#define IPV6_IP_PAYLOAD_INDEX 4 //ipv6_payload_index(4)
|
|
|
|
|
|
|
2021-11-10 15:44:51 +08:00
|
|
|
|
int tamper_calc(char *str, int endlen)
|
2021-11-02 17:39:42 +08:00
|
|
|
|
{
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
int j = 0;
|
|
|
|
|
|
char temp;
|
|
|
|
|
|
|
2021-11-10 15:44:51 +08:00
|
|
|
|
if(endlen<4){ //最少满足2个16bit的长度,即最小4字节。
|
2021-11-08 18:56:07 +08:00
|
|
|
|
return 0;
|
2021-11-02 17:39:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-10 15:44:51 +08:00
|
|
|
|
for(i=1; i<endlen; i=i+2){
|
2021-11-03 17:11:28 +08:00
|
|
|
|
for (j=i+2; j<endlen; j=j+2){
|
2021-11-02 17:39:42 +08:00
|
|
|
|
if(str[i] != str[j]){
|
|
|
|
|
|
temp = str[i];
|
|
|
|
|
|
str[i] = str[j];
|
|
|
|
|
|
str[j] = temp;
|
2021-11-08 18:56:07 +08:00
|
|
|
|
return i;
|
2021-11-02 17:39:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-11-08 18:56:07 +08:00
|
|
|
|
return 0;
|
2021-11-02 17:39:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-18 17:32:34 +08:00
|
|
|
|
int send_tamper_xxx(const struct streaminfo *a_stream, const void *raw_pkt)
|
2021-11-02 17:39:42 +08:00
|
|
|
|
{
|
2021-11-08 18:56:07 +08:00
|
|
|
|
const char *p_trans_payload = NULL;
|
|
|
|
|
|
int trans_layload_len = 0;
|
2021-11-02 17:39:42 +08:00
|
|
|
|
char tamper_buf[MTU_LEN] = {0};
|
2021-11-18 17:32:34 +08:00
|
|
|
|
int tamper_index = 0;
|
|
|
|
|
|
int ret = -1;
|
2021-11-03 17:11:28 +08:00
|
|
|
|
|
2021-11-08 18:56:07 +08:00
|
|
|
|
if(a_stream==NULL){
|
2021-11-18 17:32:34 +08:00
|
|
|
|
return -1;
|
2021-11-03 17:11:28 +08:00
|
|
|
|
}
|
2021-11-02 17:39:42 +08:00
|
|
|
|
|
2021-11-08 18:56:07 +08:00
|
|
|
|
p_trans_payload = (char *)a_stream->ptcpdetail->pdata;
|
|
|
|
|
|
trans_layload_len = a_stream->ptcpdetail->datalen;
|
2021-11-10 15:44:51 +08:00
|
|
|
|
if((p_trans_payload==NULL)||(trans_layload_len<=4)){
|
2021-11-18 17:32:34 +08:00
|
|
|
|
MESA_handle_runtime_log(g_tsg_para.logger,
|
|
|
|
|
|
RLOG_LV_DEBUG,
|
|
|
|
|
|
__FUNCTION__,
|
|
|
|
|
|
"Addr: %s Packet is not tamper because the payload is too short or there is no payload",
|
|
|
|
|
|
PRINTADDR(a_stream, g_tsg_para.level));
|
|
|
|
|
|
return -1;
|
2021-11-02 17:39:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-03 17:11:28 +08:00
|
|
|
|
memcpy(tamper_buf, p_trans_payload, trans_layload_len);
|
2021-11-18 17:32:34 +08:00
|
|
|
|
tamper_index = tamper_calc(tamper_buf, trans_layload_len);
|
|
|
|
|
|
if(tamper_index > 0){
|
2021-11-10 15:44:51 +08:00
|
|
|
|
MESA_handle_runtime_log(g_tsg_para.logger,
|
2021-11-18 17:32:34 +08:00
|
|
|
|
RLOG_LV_DEBUG,
|
|
|
|
|
|
__FUNCTION__,
|
|
|
|
|
|
"Addr: %s,modify the index(%d) position of the payload:(old: %02x %02x %02x %02x, new: %02x %02x %02x %02x)",
|
|
|
|
|
|
PRINTADDR(a_stream, g_tsg_para.level),
|
|
|
|
|
|
tamper_index,
|
|
|
|
|
|
(uint8_t)p_trans_payload[tamper_index-1], (uint8_t)p_trans_payload[tamper_index], (uint8_t)p_trans_payload[tamper_index+1], (uint8_t)p_trans_payload[tamper_index+2],
|
|
|
|
|
|
(uint8_t)tamper_buf[tamper_index-1], (uint8_t)tamper_buf[tamper_index], (uint8_t)tamper_buf[tamper_index+1], (uint8_t)tamper_buf[tamper_index+2]);
|
|
|
|
|
|
|
|
|
|
|
|
if(0 == tsg_send_inject_packet(a_stream, SIO_DEFAULT, tamper_buf, trans_layload_len, a_stream->routedir)){
|
|
|
|
|
|
ret = 0;
|
2021-11-08 18:56:07 +08:00
|
|
|
|
}
|
2021-11-18 17:32:34 +08:00
|
|
|
|
}else{
|
|
|
|
|
|
MESA_handle_runtime_log(g_tsg_para.logger,
|
|
|
|
|
|
RLOG_LV_DEBUG,
|
|
|
|
|
|
__FUNCTION__,
|
|
|
|
|
|
"Addr: %s num , payload tamper failed because payload data same",
|
|
|
|
|
|
PRINTADDR(a_stream, g_tsg_para.level));
|
2021-11-03 17:11:28 +08:00
|
|
|
|
}
|
2021-11-02 17:39:42 +08:00
|
|
|
|
|
2021-11-18 17:32:34 +08:00
|
|
|
|
return ret;
|
2021-11-03 17:11:28 +08:00
|
|
|
|
}
|