I've found a bug in signature remove code: pesign would exit before removing signature if you don't specify signum explicitly with -u option.
The patch is trivial:
diff --git a/src/file_pe.c b/src/file_pe.c
index 805e614..407d27d 100644
--- a/src/file_pe.c
+++ b/src/file_pe.c
@@ -231,6 +231,8 @@ pe_handle_action(pesign_context *ctxp, int action, int padding)
open_input(ctxp);
open_output(ctxp);
close_input(ctxp);
+ if (ctxp->signum < 0)
+ ctxp->signum = 0;
if(ctxp->signum < 0 ||
ctxp->signum >= ctxp->cms_ctx->num_signatures) {
warnx("Invalid signature number %d.",
By a lucky coincidence original code did exactly what was expected. open_output function created a copy of the input file and then did pe_clearcert after which pesign exited with error.
But fixing this bug uncovered that continuing REMOVE_SIGNATURE action calls close_output->finalize_signatures->implant_cert_list->pe_alloccert which populates Security Data Directory with zero size data:
Hi,
I've found a bug in signature remove code: pesign would exit before removing signature if you don't specify signum explicitly with -u option.
The patch is trivial:
By a lucky coincidence original code did exactly what was expected.
open_output
function created a copy of the input file and then didpe_clearcert
after which pesign exited with error.But fixing this bug uncovered that continuing REMOVE_SIGNATURE action calls
close_output
->finalize_signatures
->implant_cert_list
->pe_alloccert
which populates Security Data Directory with zerosize
data:I would assume that somewhere in this call chain should be a check for
size == 0
but not sure exactly where.The other issue is that removing signature does not produce the original file because of the alignment: