| 1 |
#!/bin/env python3 |
| 2 |
|
| 3 |
# Copyright (C) 2022-2024 Cisco Systems, Inc. and/or its affiliates. All rights reserved. |
| 4 |
# |
| 5 |
# Authors: Andrew Williams |
| 6 |
# |
| 7 |
# This program is free software; you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License version 2 as |
| 9 |
# published by the Free Software Foundation. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program; if not, write to the Free Software |
| 18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 |
# MA 02110-1301, USA. |
| 20 |
|
| 21 |
''' |
| 22 |
Build a `test.exe` program with distinctive features that may be easily detected by clamscan. |
| 23 |
This program is also signed with an authenticode certificate to test authenticode certificate trust features. |
| 24 |
''' |
| 25 |
|
| 26 |
import errno |
| 27 |
import os |
| 28 |
import argparse |
| 29 |
import random |
| 30 |
|
| 31 |
parser = argparse.ArgumentParser() |
| 32 |
parser.add_argument("os", choices=['windows', 'linux', 'win32', 'win64'], nargs='?', default='windows') |
| 33 |
parser.add_argument("--no-sign", action='store_true', default=False) |
| 34 |
parser.add_argument("--no-strip", action='store_true', default=False) |
| 35 |
parser.add_argument("--no-cleanup", action='store_true', default=False) |
| 36 |
args = parser.parse_args() |
| 37 |
|
| 38 |
do_cleanup = not args.no_cleanup |
| 39 |
|
| 40 |
if args.os == 'windows' or args.os == 'win32': |
| 41 |
prefix = 'i686-w64-mingw32-' |
| 42 |
do_res = True |
| 43 |
do_sign = not args.no_sign |
| 44 |
do_strip = not args.no_strip |
| 45 |
# __USE_MINGW_ANSI_STDIO lets %xd be used correctly when doing printf |
| 46 |
cc_flags = '-D__USE_MINGW_ANSI_STDIO=1' |
| 47 |
ext = ".exe" |
| 48 |
|
| 49 |
elif args.os == 'win64': |
| 50 |
prefix = 'x86_64-w64-mingw32-' |
| 51 |
do_res = True |
| 52 |
do_sign = not args.no_sign |
| 53 |
do_strip = not args.no_strip |
| 54 |
cc_flags = '-D__USE_MINGW_ANSI_STDIO=1' |
| 55 |
ext = ".exe" |
| 56 |
|
| 57 |
else: # linux |
| 58 |
prefix = '' |
| 59 |
do_res = False |
| 60 |
do_sign = False |
| 61 |
do_strip = False |
| 62 |
cc_flags = '' |
| 63 |
ext = '' |
| 64 |
|
| 65 |
windres = '%swindres' % (prefix) |
| 66 |
gcc = '%sgcc' % (prefix) |
| 67 |
ld = '%sld' % (prefix) |
| 68 |
strip = '%sstrip' % (prefix) |
| 69 |
|
| 70 |
indicators = [ |
| 71 |
# Indicators for the outer binary |
| 72 |
('22df_62ba_15c8_f482', '1a0d_301b_228d_56c8'), |
| 73 |
|
| 74 |
# Indicators for up to 24 inner binaries |
| 75 |
('ffa8_3994_1788_e8b6', 'e4d7_7b43_3155_040a'), |
| 76 |
('3a28_3628_9dd8_9c32', '3da0_c7bc_1948_39bd'), |
| 77 |
('d251_d598_8e5c_6f9d', 'ee01_7f34_e9c9_1ec1'), |
| 78 |
('6761_4a90_365b_b4c0', 'b8b0_d765_3df3_e550'), |
| 79 |
('7939_5658_544a_f991', '768b_f5e5_f5e4_1a3b'), |
| 80 |
('d765_7e8b_90b9_4a2f', '6cb8_1966_8bc3_9874'), |
| 81 |
('cef1_e295_6e46_f429', '0ef3_77f1_e811_2753'), |
| 82 |
('8f8a_f7f2_cdd1_9d0b', '99aa_e67d_afb6_3735'), |
| 83 |
('d183_4b76_bde9_f5fb', 'f5e2_acf2_ff78_88f7'), |
| 84 |
('3bce_e84b_be85_77bc', 'b155_ac2c_22f6_cb9e'), |
| 85 |
('6efd_bf13_3679_9d30', '00de_ce35_6606_114c'), |
| 86 |
('6ef7_8f1c_19f8_9746', '38d9_f9c7_45de_9931'), |
| 87 |
('54b5_a0a4_b852_d3ba', 'cc78_0d79_8e9d_6f8a'), |
| 88 |
('7468_e227_c130_b48d', '0c57_4d6a_e113_e03a'), |
| 89 |
('8272_e8e2_a133_2971', 'e113_d12a_266c_3253'), |
| 90 |
('b552_7c09_8b0f_01c7', '01c3_a69a_899f_0764'), |
| 91 |
('04a3_b125_45ed_2a8b', '58f8_6bf7_b1bb_c7b6'), |
| 92 |
('5749_2c8c_9df1_4c7e', 'c0a0_3f27_99c5_51f4'), |
| 93 |
('89df_0268_6c60_29fb', 'db58_4b5e_f2df_9b65'), |
| 94 |
('c2cd_3a28_5180_dc7a', '2c70_5359_0a46_06fa'), |
| 95 |
('6764_76f5_0e92_faa6', 'd07a_b1af_e36f_8894'), |
| 96 |
('4e45_18c0_40da_e74d', '6a1d_d382_068a_7e71'), |
| 97 |
('01c4_23f0_0e32_fad1', 'a2f0_7a46_5cf1_2e0d'), |
| 98 |
('1cec_e7c0_daac_517f', '448a_2336_facf_e5e7'), |
| 99 |
] |
| 100 |
|
| 101 |
if len(indicators)-1 > 32: |
| 102 |
raise Exception("More embedded binaries than extract.h can handle") |
| 103 |
|
| 104 |
try: |
| 105 |
os.mkdir('build') |
| 106 |
except OSError as exc: |
| 107 |
if exc.errno != errno.EEXIST: |
| 108 |
raise |
| 109 |
pass |
| 110 |
|
| 111 |
def run_cmd(cmd): |
| 112 |
print(cmd) |
| 113 |
if os.system(cmd): |
| 114 |
raise Exception("Command Failed: %s" % cmd) |
| 115 |
|
| 116 |
def gen_ca_cert(): |
| 117 |
run_cmd('openssl genrsa -out build/ca.key 4096') |
| 118 |
|
| 119 |
# TODO Explore making this cert have attributes that look more like |
| 120 |
# a real CA cert (ex: restrict its uses) |
| 121 |
subj = "/C=US/ST=Maryland/L=Fulton/O=Cisco Talos/OU=ClamAV Test CA %016x/[email protected]" % (random.randint(1,0xFFFFFFFFFFFFFFFF)) |
| 122 |
cmd = 'openssl req -new -x509 -days 73000 -key build/ca.key -out build/ca.crt -subj "%s"' % (subj) |
| 123 |
run_cmd(cmd) |
| 124 |
|
| 125 |
# https://blog.didierstevens.com/2008/12/30/howto-make-your-own-cert-with-openssl/ |
| 126 |
def gen_cs_cert(name, ext): |
| 127 |
key_name = 'build/%s%s.key' % (name, ext) |
| 128 |
csr_name = 'build/%s%s.csr' % (name, ext) |
| 129 |
crt_name = 'build/%s%s.crt' % (name, ext) |
| 130 |
|
| 131 |
run_cmd('openssl genrsa -out %s 4096' % (key_name)) |
| 132 |
|
| 133 |
# TODO Explore making this cert have attributes that look more like |
| 134 |
# a real CS cert (ex: restrict its uses) |
| 135 |
subj = "/C=US/ST=Maryland/L=Fulton/O=Cisco Talos/OU=ClamAV Test %016x/[email protected]" % (random.randint(1,0xFFFFFFFFFFFFFFFF)) |
| 136 |
cmd = 'openssl req -new -key %s -out %s -subj "%s"' % (key_name, csr_name, subj) |
| 137 |
run_cmd(cmd) |
| 138 |
|
| 139 |
cmd = 'openssl x509 -req -days 73000 -in %s -CA build/ca.crt -CAkey build/ca.key -out %s -set_serial %012d -extfile ./cs.extfile.cfg' % (csr_name, crt_name, random.randint(100000000000,999999999999)) |
| 140 |
run_cmd(cmd) |
| 141 |
|
| 142 |
return (key_name, crt_name) |
| 143 |
|
| 144 |
# https://blog.didierstevens.com/2018/09/24/quickpost-signing-windows-executables-on-kali/ |
| 145 |
def sign_exe(name, ext, cert_info): |
| 146 |
key_name = cert_info[0] |
| 147 |
crt_name = cert_info[1] |
| 148 |
orig_path = "build/%s%s" % (name, ext) |
| 149 |
signed_path = "build/%s-signed%s" % (name, ext) |
| 150 |
cmd = 'osslsigncode sign -certs %s -key %s -ts http://sha256timestamp.ws.symantec.com/sha256/timestamp -in %s -out %s' % (crt_name, key_name, orig_path, signed_path) |
| 151 |
run_cmd(cmd) |
| 152 |
os.unlink(orig_path) |
| 153 |
os.rename(signed_path, orig_path) |
| 154 |
|
| 155 |
def build_exe(name, ext, index, cc_flags, do_res, do_strip, sources=['./test.c']): |
| 156 |
|
| 157 |
if do_res: |
| 158 |
# TODO Generate a new icon and new version information per exe. The |
| 159 |
# icon can be generated with: |
| 160 |
# |
| 161 |
# head -c 245760 /dev/urandom | convert -depth 8 -size 320x256 RGB:- test.png |
| 162 |
# convert -background transparent test.png -define icon:auto-resize=16,32,48,64,256 test.ico |
| 163 |
# |
| 164 |
# NOTE ^^^ 245760 is 320x256x3 |
| 165 |
res_path = 'build/%s%s.res' % (name, ext) |
| 166 |
cmd = '%s ./test.rc -O coff -o %s' % (windres, res_path) |
| 167 |
run_cmd(cmd) |
| 168 |
sources.append(res_path) |
| 169 |
|
| 170 |
cmd = '%s -Os %s %s -DINDICATOR1=\\"%s\\" -DINDICATOR2=\\"%s\\" -DINDEX=%d -o build/%s%s' % (gcc, cc_flags, ' '.join([x for x in sources]), indicators[index][0], indicators[index][1], index, name, ext) |
| 171 |
run_cmd(cmd) |
| 172 |
|
| 173 |
if do_strip: |
| 174 |
cmd = '%s --strip-all build/%s%s' %(strip, name, ext) |
| 175 |
run_cmd(cmd) |
| 176 |
|
| 177 |
def package_exe(name, ext): |
| 178 |
# The name of the export symbols depends on the path, so we have to be in |
| 179 |
# the same directory as the exe we are packaging for the symbol name the |
| 180 |
# code expects to be generated. |
| 181 |
os.chdir('build') |
| 182 |
cmd = '%s -r -b binary %s%s -o %s%s.o' %(ld, name, ext, name, ext) |
| 183 |
run_cmd(cmd) |
| 184 |
os.chdir('..') |
| 185 |
|
| 186 |
# Generate a new CA cert for code-signing |
| 187 |
if do_sign: |
| 188 |
gen_ca_cert() |
| 189 |
|
| 190 |
outer_exe_sources = ['./test.c'] |
| 191 |
# Build the inner exes. They MUST be named 'exe#', where # is the index. They |
| 192 |
# can't have a file extension or else it breaks linking. |
| 193 |
for i in range(1, len(indicators)): |
| 194 |
name = 'exe%d' % (i) |
| 195 |
inner_exe_ext = ".exe" |
| 196 |
build_exe(name, inner_exe_ext, i, cc_flags, False, do_strip); |
| 197 |
if do_sign: |
| 198 |
cert_info = gen_cs_cert(name, inner_exe_ext) |
| 199 |
sign_exe(name, inner_exe_ext, cert_info) |
| 200 |
package_exe(name, inner_exe_ext) |
| 201 |
outer_exe_sources.append('build/%s%s.o' % (name, inner_exe_ext)) |
| 202 |
|
| 203 |
# Build the outer exe |
| 204 |
name = 'test' |
| 205 |
build_exe(name, ext, 0, cc_flags, do_res, do_strip, sources=outer_exe_sources) |
| 206 |
if do_sign: |
| 207 |
cert_info = gen_cs_cert(name, ext) |
| 208 |
sign_exe(name, ext, cert_info) |
| 209 |
|
| 210 |
# Delete unneeded artifacts |
| 211 |
if do_cleanup: |
| 212 |
cleanups = ['build/*.o'] |
| 213 |
if do_sign: |
| 214 |
cleanups += ['build/*.csr', 'build/*.key'] |
| 215 |
|
| 216 |
if do_res: |
| 217 |
cleanups += ['build/test.exe.res'] |
| 218 |
|
| 219 |
for cleanup in cleanups: |
| 220 |
cmd = 'rm %s' % (cleanup) |
| 221 |
try: |
| 222 |
run_cmd(cmd) |
| 223 |
except: |
| 224 |
pass |