Skip to content

Instantly share code, notes, and snippets.

@aayla-secura
Last active March 10, 2021 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aayla-secura/3665e777bcf6f6f29b54b3e7cd7a337f to your computer and use it in GitHub Desktop.
Save aayla-secura/3665e777bcf6f6f29b54b3e7cd7a337f to your computer and use it in GitHub Desktop.
Show disassembly of given opcodes or assembly instructions
#!/bin/bash
# See -h for help
INTERACTIVE=1
READ_ARGS=()
NASM_ARGS=()
OBJDUMP_ARGS=()
# Determine if interactive or stdin is redirected from file/heredoc/command
# output/etc
read -t 0 _
if [[ $? -eq 0 ]] ; then
INTERACTIVE=0
READ_ARGS=(-t 0.1)
fi
function usage {
cat <<EOF
Usage "${BASH_SOURCE[0]}" [<options>]
Options:
-f FORMAT format, e.g. elf64
-32 equivalent to -f elf32
-64 equivalent to -f elf64
-M SYNTAX syntax, e.g. intel
-m ARCH architecture, e.g. arm
Example:
"${BASH_SOURCE[0]}" -64 -M intel <<<'jmp rsp'
EOF
exit 1
}
function disasm {
local basef="${1}"
nasm "${NASM_ARGS[@]}" "${basef}.nasm" -o "${basef}.o" || return $?
objdump --no-addresses -d -j .text "${OBJDUMP_ARGS[@]}" "${basef}.o" | \
sed '1,/^<\.text>/{d};s/^\s*//'
rm "${basef}".o
}
function readin {
# return 0 if more input expected (i.e. interactive and enter was pressed),
# 1 otherwise
local basef=$(mktemp opcode_to_asm.XXXXXXXX) line rc=1
while IFS= read "${READ_ARGS[@]}" -r -p '> ' line ; do
# quit on a blank line only in interactive mode
if [ "${INTERACTIVE}" -eq 1 -a -z "${line}" ] ; then
rc=0
break
fi
echo -E "${line}" >> "${basef}.nasm"
done
if [ -f "${basef}.nasm" -a -s "${basef}.nasm" ] ; then
disasm "${basef}"
rm "${basef}".nasm
fi
return ${rc}
}
####################
while [ $# -gt 0 ] ; do
case "${1}" in
-32|-64)
NASM_ARGS+=(-f "elf${1#-}")
shift
;;
-f)
NASM_ARGS+=("${1}" "${2}")
shift 2
;;
-M|-m)
OBJDUMP_ARGS+=("${1}" "${2}")
shift 2
;;
*)
usage
;;
esac
done
while readin ; do
:
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment