From 806f9029d160c5f47f0b49db288f469718424f7b Mon Sep 17 00:00:00 2001
From: Malfurious <m@lfurio.us>
Date: Sat, 24 Dec 2022 08:02:29 -0500
Subject: shellcode: Update Makefile

This patch brings various improvements to the shellcoding experience:

    - There is no longer a hardcoded assembly sample that gets built

        Although the default was pretty sane, it will be more convenient
        to experiment, or build more complex shellcodes using a new
        untracked filename as the main build target: code.asm

        If code.asm is missing, then as before, it will be created from
        shell64.asm (the old hard default).

        The Makefile targets will compile code.* files.

    - Hex string generation and bad char detection are improved

        grep is used to highlight detected bad chars right in place.
        This entire feature is now implemented directly in the Makefile
        using a couple command lines, making shelltool deprecated.

    - Builtin disassembly

        Just run 'make disas' instead of manually invoking objdump.  The
        output is also filtered through grep for bad char detection.

    - ELF executable is optional

        Rather than linking an executable all the time, just run
        'make elf' when you need it.

Signed-off-by: Malfurious <m@lfurio.us>
---
 templates/shellcode/Makefile | 38 ++++++++++++++++++++++++++------------
 1 file changed, 26 insertions(+), 12 deletions(-)

diff --git a/templates/shellcode/Makefile b/templates/shellcode/Makefile
index 2e67adc..757878f 100644
--- a/templates/shellcode/Makefile
+++ b/templates/shellcode/Makefile
@@ -1,17 +1,31 @@
-.PHONY: all
+LDFLAGS?=
+FORMAT?=elf64
+CODE?=examples/shell64.asm
+GREP=00|0a
 
-all: shell32.elf shell64.elf
-	@objdump -d shell32.elf | ./shelltool.py
-	@objdump -d shell64.elf | ./shelltool.py
+.PHONY: all elf disas
 
-shell32.o: shell32.asm
-	nasm -f elf shell32.asm -o shell32.o
+# Format bytecode as an escaped string, highlight bad bytes
+all: code.bin
+	@xxd -i -c 16 <code.bin \
+		| sed 's/,//g;s/ 0/\\/g;s/^ */"/g;s/$$/"/g' \
+		| grep --color=always -E '$(GREP)|$$'
 
-shell32.elf: shell32.o
-	ld -melf_i386 shell32.o -o shell32.elf
+elf: code.o
+	ld $(LDFLAGS) code.o -o code.elf
 
-shell64.o: shell64.asm
-	nasm -f elf64 shell64.asm -o shell64.o
+disas: code.o
+	@objdump -d code.o \
+		| grep --color=always -E '$(GREP)|$$'
 
-shell64.elf: shell64.o
-	ld shell64.o -o shell64.elf
+code.bin: code.o
+	objcopy -O binary code.o code.bin
+
+code.o: code.asm
+	nasm -f '$(FORMAT)' code.asm -o code.o
+
+code.asm:
+	cp '$(CODE)' code.asm
+
+
+# -melf_i386
-- 
cgit v1.2.3