1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-23 17:34:34 +03:00
samba-mirror/examples/winexe/wscript_build

110 lines
3.3 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python
import samba_utils
def generate_winexesvc_c_from_exe(t):
winexe: Fix translation of the winexesvc binaries to C Two small Windows binaries that winexe uses to execute commands on a remote system, winexesvc32.exe and winexesvc64.exe, are compiled then translated into a C byte array as hex so that they can be embedded into the winexe binary. Although the winexesvc binaries were built properly, the Python method that does the translation to C tried to open them in text mode, which would have worked in Python 2 before the concept of bytearrays was introduced, but instead raises an exception in Python 3. The exception was unfortunately suppressed, so the build didn't stop, and the winexe binary that was produced was effectively useless because it didn't contain either winexesvc binary as expected. After winexe successfully authenticated with a Windows host, it showed the error message below rather than executing the given command on the remote system. $ ./bin/winexe -U karl%password1 -d 2 //192.168.56.3 cmd winexe_svc_install: dcerpc_svcctl_StartServiceW failed: WERR_BAD_EXE_FORMAT main: winexe_svc_install failed: NT_STATUS_BAD_INITIAL_PC This commit fixes that problem by opening the winexesvc binaries in binary mode rather than text mode when the winexe build script reads them to translate them to C. Furthermore it adds an additional sanity check that will cause the winexesvc binary generator commands to fail if the winexesvc binaries cannot be opened or read correctly to guarantee that the build does not silently "succeed" if something like this ever happens again. Signed-off-by: Karl Lenz <xorangekiller@gmail.com> Reviewed-by: Andrew Bartlett <abartlet@samba.org> Reviewed-by: Gary Lockyer <gary@catalyst.net.nz>
2019-07-05 01:30:44 +03:00
'''generate a C source file with the contents of the given binary'''
src = t.inputs[0].bldpath(t.env)
tgt = t.outputs[0].bldpath(t.env)
fn = t.env.SAMBA_GENERATOR_VARS['WINEXE_FN']
winexe: Fix translation of the winexesvc binaries to C Two small Windows binaries that winexe uses to execute commands on a remote system, winexesvc32.exe and winexesvc64.exe, are compiled then translated into a C byte array as hex so that they can be embedded into the winexe binary. Although the winexesvc binaries were built properly, the Python method that does the translation to C tried to open them in text mode, which would have worked in Python 2 before the concept of bytearrays was introduced, but instead raises an exception in Python 3. The exception was unfortunately suppressed, so the build didn't stop, and the winexe binary that was produced was effectively useless because it didn't contain either winexesvc binary as expected. After winexe successfully authenticated with a Windows host, it showed the error message below rather than executing the given command on the remote system. $ ./bin/winexe -U karl%password1 -d 2 //192.168.56.3 cmd winexe_svc_install: dcerpc_svcctl_StartServiceW failed: WERR_BAD_EXE_FORMAT main: winexe_svc_install failed: NT_STATUS_BAD_INITIAL_PC This commit fixes that problem by opening the winexesvc binaries in binary mode rather than text mode when the winexe build script reads them to translate them to C. Furthermore it adds an additional sanity check that will cause the winexesvc binary generator commands to fail if the winexesvc binaries cannot be opened or read correctly to guarantee that the build does not silently "succeed" if something like this ever happens again. Signed-off-by: Karl Lenz <xorangekiller@gmail.com> Reviewed-by: Andrew Bartlett <abartlet@samba.org> Reviewed-by: Gary Lockyer <gary@catalyst.net.nz>
2019-07-05 01:30:44 +03:00
try:
with open(src, 'rb') as f:
src_blob = f.read()
f.close()
except:
print('Failed to read %s to convert to C array' % (src))
return -1
def c_array(src):
N = 0
result = ''
while src:
l = src[:8]
src = src[8:]
winexe: Fix translation of the winexesvc binaries to C Two small Windows binaries that winexe uses to execute commands on a remote system, winexesvc32.exe and winexesvc64.exe, are compiled then translated into a C byte array as hex so that they can be embedded into the winexe binary. Although the winexesvc binaries were built properly, the Python method that does the translation to C tried to open them in text mode, which would have worked in Python 2 before the concept of bytearrays was introduced, but instead raises an exception in Python 3. The exception was unfortunately suppressed, so the build didn't stop, and the winexe binary that was produced was effectively useless because it didn't contain either winexesvc binary as expected. After winexe successfully authenticated with a Windows host, it showed the error message below rather than executing the given command on the remote system. $ ./bin/winexe -U karl%password1 -d 2 //192.168.56.3 cmd winexe_svc_install: dcerpc_svcctl_StartServiceW failed: WERR_BAD_EXE_FORMAT main: winexe_svc_install failed: NT_STATUS_BAD_INITIAL_PC This commit fixes that problem by opening the winexesvc binaries in binary mode rather than text mode when the winexe build script reads them to translate them to C. Furthermore it adds an additional sanity check that will cause the winexesvc binary generator commands to fail if the winexesvc binaries cannot be opened or read correctly to guarantee that the build does not silently "succeed" if something like this ever happens again. Signed-off-by: Karl Lenz <xorangekiller@gmail.com> Reviewed-by: Andrew Bartlett <abartlet@samba.org> Reviewed-by: Gary Lockyer <gary@catalyst.net.nz>
2019-07-05 01:30:44 +03:00
# Even files opened in binary mode are read as type "str" in
# Python 2, so we need to get the integer ordinal of each
# character in the string before we try to convert it to hex.
if isinstance(l, str):
h = ' '.join(["0x%02X," % ord(x) for x in l])
# Files opened in binary mode are read as type "bytes" in
# Python 3, so we can convert each individual integer in the
# array of bytes to hex directly.
else:
h = ' '.join(["0x%02X," % x for x in l])
result += "\t\t%s\n" % (h)
return result
src_array = c_array(src_blob)
winexe: Fix translation of the winexesvc binaries to C Two small Windows binaries that winexe uses to execute commands on a remote system, winexesvc32.exe and winexesvc64.exe, are compiled then translated into a C byte array as hex so that they can be embedded into the winexe binary. Although the winexesvc binaries were built properly, the Python method that does the translation to C tried to open them in text mode, which would have worked in Python 2 before the concept of bytearrays was introduced, but instead raises an exception in Python 3. The exception was unfortunately suppressed, so the build didn't stop, and the winexe binary that was produced was effectively useless because it didn't contain either winexesvc binary as expected. After winexe successfully authenticated with a Windows host, it showed the error message below rather than executing the given command on the remote system. $ ./bin/winexe -U karl%password1 -d 2 //192.168.56.3 cmd winexe_svc_install: dcerpc_svcctl_StartServiceW failed: WERR_BAD_EXE_FORMAT main: winexe_svc_install failed: NT_STATUS_BAD_INITIAL_PC This commit fixes that problem by opening the winexesvc binaries in binary mode rather than text mode when the winexe build script reads them to translate them to C. Furthermore it adds an additional sanity check that will cause the winexesvc binary generator commands to fail if the winexesvc binaries cannot be opened or read correctly to guarantee that the build does not silently "succeed" if something like this ever happens again. Signed-off-by: Karl Lenz <xorangekiller@gmail.com> Reviewed-by: Andrew Bartlett <abartlet@samba.org> Reviewed-by: Gary Lockyer <gary@catalyst.net.nz>
2019-07-05 01:30:44 +03:00
if len(src_array) <= 0:
print('Failed to convert %s to C array' % (src))
return -1
contents = '''
#include "replace.h"
#include "lib/util/data_blob.h"
const DATA_BLOB *%s(void);
const DATA_BLOB *%s(void)
{
\tstatic const uint8_t array[] = {
%s
\t};
\tstatic const DATA_BLOB blob = {
\t\t.data = discard_const_p(uint8_t, array),
\t\t.length = ARRAY_SIZE(array),
\t};
\treturn &blob;
}
''' % (fn, fn, src_array)
winexe: Fix translation of the winexesvc binaries to C Two small Windows binaries that winexe uses to execute commands on a remote system, winexesvc32.exe and winexesvc64.exe, are compiled then translated into a C byte array as hex so that they can be embedded into the winexe binary. Although the winexesvc binaries were built properly, the Python method that does the translation to C tried to open them in text mode, which would have worked in Python 2 before the concept of bytearrays was introduced, but instead raises an exception in Python 3. The exception was unfortunately suppressed, so the build didn't stop, and the winexe binary that was produced was effectively useless because it didn't contain either winexesvc binary as expected. After winexe successfully authenticated with a Windows host, it showed the error message below rather than executing the given command on the remote system. $ ./bin/winexe -U karl%password1 -d 2 //192.168.56.3 cmd winexe_svc_install: dcerpc_svcctl_StartServiceW failed: WERR_BAD_EXE_FORMAT main: winexe_svc_install failed: NT_STATUS_BAD_INITIAL_PC This commit fixes that problem by opening the winexesvc binaries in binary mode rather than text mode when the winexe build script reads them to translate them to C. Furthermore it adds an additional sanity check that will cause the winexesvc binary generator commands to fail if the winexesvc binaries cannot be opened or read correctly to guarantee that the build does not silently "succeed" if something like this ever happens again. Signed-off-by: Karl Lenz <xorangekiller@gmail.com> Reviewed-by: Andrew Bartlett <abartlet@samba.org> Reviewed-by: Gary Lockyer <gary@catalyst.net.nz>
2019-07-05 01:30:44 +03:00
if not samba_utils.save_file(tgt, contents):
print('Failed to write C source file %s' % (tgt))
return -1
return 0
winexesvc_binaries = ''
if bld.env.WINEXE_CC_WIN32:
bld.SAMBA_GENERATOR(
'winexesvc32_exe',
source='winexesvc.c',
target='winexesvc32.exe',
rule='${WINEXE_CC_WIN32} ${SRC} -o ${TGT} ${WINEXE_LDFLAGS}')
vars = {"WINEXE_FN": "winexesvc32_exe_binary"}
bld.SAMBA_GENERATOR(
'winexesvc32_exe_binary',
source='winexesvc32.exe',
target='winexesvc32_exe_binary.c',
group='build_source',
vars=vars,
rule=generate_winexesvc_c_from_exe)
winexesvc_binaries += ' winexesvc32_exe_binary.c'
if bld.env.WINEXE_CC_WIN64:
bld.SAMBA_GENERATOR(
'winexesvc64_exe',
source='winexesvc.c',
target='winexesvc64.exe',
rule='${WINEXE_CC_WIN64} ${SRC} -o ${TGT} ${WINEXE_LDFLAGS}')
vars = {"WINEXE_FN": "winexesvc64_exe_binary"}
bld.SAMBA_GENERATOR(
'winexesvc64_exe_binary',
source='winexesvc64.exe',
target='winexesvc64_exe_binary.c',
group='build_source',
vars=vars,
rule=generate_winexesvc_c_from_exe)
winexesvc_binaries += ' winexesvc64_exe_binary.c'
if winexesvc_binaries != '':
bld.SAMBA3_BINARY('winexe',
source='winexe.c ' + winexesvc_binaries,
deps='''
popt
samba-credentials
LOADPARM_CTX
libsmb
msrpc3
''')