MCPcopy
hub / github.com/Gallopsled/pwntools / asm

Function asm

pwnlib/asm.py:740–893  ·  view source on GitHub ↗

r"""asm(code, vma = 0, extract = True, shared = False, ...) -> str Runs :func:`cpp` over a given shellcode and then assembles it into bytes. To see which architectures or operating systems are supported, look in :mod:`pwnlib.context`. Assembling shellcode requires that the GNU ass

(shellcode, vma = 0, extract = True, shared = False)

Source from the content-addressed store, hash-verified

738
739@LocalContext
740def asm(shellcode, vma = 0, extract = True, shared = False):
741 r"""asm(code, vma = 0, extract = True, shared = False, ...) -> str
742
743 Runs :func:`cpp` over a given shellcode and then assembles it into bytes.
744
745 To see which architectures or operating systems are supported,
746 look in :mod:`pwnlib.context`.
747
748 Assembling shellcode requires that the GNU assembler is installed
749 for the target architecture.
750 See :doc:`Installing Binutils </install/binutils>` for more information.
751
752 Arguments:
753 shellcode(str): Assembler code to assemble.
754 vma(int): Virtual memory address of the beginning of assembly
755 extract(bool): Extract the raw assembly bytes from the assembled
756 file. If :const:`False`, returns the path to an ELF file
757 with the assembly embedded.
758 shared(bool): Create a shared object.
759 kwargs(dict): Any attributes on :data:`.context` can be set, e.g.set
760 ``arch='arm'``.
761
762 Examples:
763
764 >>> asm("mov eax, SYS_select", arch = 'i386', os = 'freebsd')
765 b'\xb8]\x00\x00\x00'
766 >>> asm("mov eax, SYS_select", arch = 'amd64', os = 'linux')
767 b'\xb8\x17\x00\x00\x00'
768 >>> asm("mov rax, SYS_select", arch = 'amd64', os = 'linux')
769 b'H\xc7\xc0\x17\x00\x00\x00'
770 >>> asm("mov r0, #SYS_select", arch = 'arm', os = 'linux', bits=32)
771 b'R\x00\xa0\xe3'
772 >>> asm("mov #42, r0", arch = 'msp430')
773 b'0@*\x00'
774 >>> asm("la %r0, 42", arch = 's390', bits=64)
775 b'A\x00\x00*'
776
777 The output is cached:
778
779 >>> start = time.time()
780 >>> asm("lea rax, [rip+0]", arch = 'amd64', cache_dir = None) # force uncached time
781 b'H\x8d\x05\x00\x00\x00\x00'
782 >>> uncached_time = time.time() - start
783 >>> asm("lea rax, [rip+0]", arch = 'amd64') # cache it
784 b'H\x8d\x05\x00\x00\x00\x00'
785 >>> start = time.time()
786 >>> asm("lea rax, [rip+0]", arch = 'amd64')
787 b'H\x8d\x05\x00\x00\x00\x00'
788 >>> cached_time = time.time() - start
789 >>> uncached_time > cached_time
790 True
791 """
792 result = b''
793
794 assembler = _assembler()
795 linker = _linker()
796 objcopy = _objcopy() + ['-j', '.shellcode', '-Obinary']
797 code = ''

Callers 9

__call__Method · 0.90
__call__Method · 0.90
make_elf_from_assemblyFunction · 0.85
make_elfFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85
asmMethod · 0.85
asm.pyFile · 0.85

Calls 15

_encodeFunction · 0.90
_assemblerFunction · 0.85
_linkerFunction · 0.85
_objcopyFunction · 0.85
_arch_headerFunction · 0.85
cppFunction · 0.85
_runFunction · 0.85
_execstackFunction · 0.85
which_binutilsFunction · 0.85
formatMethod · 0.80
existsMethod · 0.80
mkdtempMethod · 0.80

Tested by

no test coverage detected