def extract_file_arch(pe, **kwargs): if pe.FILE_HEADER.Machine == pefile.MACHINE_TYPE["IMAGE_FILE_MACHINE_I386"]: yield Arch(ARCH_I386), 0x0 elif pe.FILE_HEADER.Machine == pefile.MACHINE_TYPE["IMAGE_FILE_MACHINE_AMD64"]: yield Arch(ARCH_AMD64), 0x0 else: logger.warning("unsupported architecture: %s", pefile.MACHINE_TYPE[pe.FILE_HEADER.Machine])
def extract_file_arch(pe: dnfile.dnPE, **kwargs) -> Iterator[Tuple[Arch, int]]: # to distinguish in more detail, see https://stackoverflow.com/a/23614024/10548020 # .NET 4.5 added option: any CPU, 32-bit preferred if pe.net.Flags.CLR_32BITREQUIRED and pe.PE_TYPE == pefile.OPTIONAL_HEADER_MAGIC_PE: yield Arch(ARCH_I386), 0x0 elif not pe.net.Flags.CLR_32BITREQUIRED and pe.PE_TYPE == pefile.OPTIONAL_HEADER_MAGIC_PE_PLUS: yield Arch(ARCH_AMD64), 0x0 else: yield Arch(ARCH_ANY), 0x0
def extract_file_arch(elf, **kwargs): # TODO merge with capa.features.extractors.elf.detect_elf_arch() arch = elf.get_machine_arch() if arch == "x86": yield Arch(ElfArch.I386), 0x0 elif arch == "x64": yield Arch(ElfArch.AMD64), 0x0 else: logger.warning("unsupported architecture: %s", arch)
def extract_arch(smda_report): if smda_report.architecture == "intel": if smda_report.bitness == 32: yield Arch(ARCH_I386), 0x0 elif smda_report.bitness == 64: yield Arch(ARCH_AMD64), 0x0 else: # we likely end up here: # 1. handling a new architecture (e.g. aarch64) # # for (1), this logic will need to be updated as the format is implemented. logger.debug("unsupported architecture: %s", smda_report.architecture) return
def extract_arch(vw): if isinstance(vw.arch, envi.archs.amd64.Amd64Module): yield Arch(ARCH_AMD64), 0x0 elif isinstance(vw.arch, envi.archs.i386.i386Module): yield Arch(ARCH_I386), 0x0 else: # we likely end up here: # 1. handling a new architecture (e.g. aarch64) # # for (1), this logic will need to be updated as the format is implemented. logger.debug("unsupported architecture: %s", vw.arch.__class__.__name__) return
def test_arch_features(): rule = textwrap.dedent( """ rule: meta: name: test rule scope: file features: - and: - arch: amd64 """ ) r = capa.rules.Rule.from_yaml(rule) children = list(r.statement.get_children()) assert (Arch(ARCH_AMD64) in children) == True assert (Arch(ARCH_I386) not in children) == True
def extract_arch(): info = idaapi.get_inf_structure() if info.procName == "metapc" and info.is_64bit(): yield Arch(ARCH_AMD64), 0x0 elif info.procName == "metapc" and info.is_32bit(): yield Arch(ARCH_I386), 0x0 elif info.procName == "metapc": logger.debug("unsupported architecture: non-32-bit nor non-64-bit intel") return else: # we likely end up here: # 1. handling a new architecture (e.g. aarch64) # # for (1), this logic will need to be updated as the format is implemented. logger.debug("unsupported architecture: %s", info.procName) return
def extract_arch(buf): if buf.startswith(b"MZ"): yield from capa.features.extractors.pefile.extract_file_arch( pe=pefile.PE(data=buf)) elif buf.startswith(b"\x7fELF"): with contextlib.closing(io.BytesIO(buf)) as f: arch = capa.features.extractors.elf.detect_elf_arch(f) if arch not in capa.features.common.VALID_ARCH: logger.debug("unsupported arch: %s", arch) return yield Arch(arch), 0x0 else: # we likely end up here: # 1. handling shellcode, or # 2. handling a new file format (e.g. macho) # # for (1) we can't do much - its shellcode and all bets are off. # we could maybe accept a futher CLI argument to specify the arch, # but i think this would be rarely used. # rules that rely on arch conditions will fail to match on shellcode. # # for (2), this logic will need to be updated as the format is implemented. logger.debug("unsupported file format: %s, will not guess Arch", binascii.hexlify(buf[:4]).decode("ascii")) return
capa.features.common.Characteristic("calls from"), False), # function/characteristic(calls to) ("mimikatz", "function=0x40105D", capa.features.common.Characteristic("calls to"), True), # before this we used ambiguous (0x4556E5, False), which has a data reference / indirect recursive call, see #386 ("mimikatz", "function=0x456BB9", capa.features.common.Characteristic("calls to"), False), # file/function-name ("pma16-01", "file", capa.features.file.FunctionName("__aulldiv"), True ), # os & format & arch ("pma16-01", "file", OS(OS_WINDOWS), True), ("pma16-01", "file", OS(OS_LINUX), False), ("pma16-01", "function=0x404356", OS(OS_WINDOWS), True), ("pma16-01", "function=0x404356,bb=0x4043B9", OS(OS_WINDOWS), True), ("pma16-01", "file", Arch(ARCH_I386), True), ("pma16-01", "file", Arch(ARCH_AMD64), False), ("pma16-01", "function=0x404356", Arch(ARCH_I386), True), ("pma16-01", "function=0x404356,bb=0x4043B9", Arch(ARCH_I386), True), ("pma16-01", "file", Format(FORMAT_PE), True), ("pma16-01", "file", Format(FORMAT_ELF), False), # elf support ("7351f.elf", "file", OS(OS_LINUX), True), ("7351f.elf", "file", OS(OS_WINDOWS), False), ("7351f.elf", "file", Format(FORMAT_ELF), True), ("7351f.elf", "file", Format(FORMAT_PE), False), ("7351f.elf", "file", Arch(ARCH_I386), False), ("7351f.elf", "file", Arch(ARCH_AMD64), True), ("7351f.elf", "function=0x408753", capa.features.common.String("/dev/null"), True), ("7351f.elf", "function=0x408753,bb=0x408781",