unicorn engine integration for shikata ga nai decoding

While working on proper interpretation of shellcode payloads from [1] we
found some shikata ga nai-encoded payloads. Using Unicorn Engine we
attempt to unpack this first layer to reach the actual shellcode. For
now this only works partially due to one or more suspected bugs in
Unicorn Engine's support for self-modifying code [2].

[1]: http://researchcenter.paloaltonetworks.com/2017/03/unit42-pulling-back-the-curtains-on-encodedcommand-powershell-attacks/
[2]: https://github.com/unicorn-engine/unicorn/issues/820
This commit is contained in:
Jurriaan Bremer
2017-05-27 23:06:31 +02:00
parent 351d789009
commit 9fb6003dd3
10 changed files with 121 additions and 0 deletions
+77
View File
@@ -0,0 +1,77 @@
# Copyright (C) 2017 Cuckoo Foundation.
# This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org
# See the file 'docs/LICENSE' for copying permission.
import logging
import unicorn
import unicorn.x86_const as x86
log = logging.getLogger(__name__)
archs = {
"x86": unicorn.UC_ARCH_X86,
}
modes = {
32: unicorn.UC_MODE_32,
}
class UcX86(unicorn.Uc):
@property
def esp(self):
return self.reg_read(x86.UC_X86_REG_ESP)
@esp.setter
def esp(self, value):
self.reg_write(x86.UC_X86_REG_ESP, value)
class ShellcodeX86(object):
def __init__(self, arch, mode, sc=None):
self.emu = UcX86(arch, mode)
self.addr = None
self.sc = sc
self.init()
def init(self):
pass
def map_memory(self, addr=0x1000, memsize=2*1024*1024):
self.addr = addr
self.emu.mem_map(addr, memsize)
self.emu.mem_write(addr, self.sc)
self.emu.esp = addr + memsize / 2
def run(self, addr=None, end=0, count=None):
try:
self.emu.emu_start(addr or self.addr, end, count=count)
except unicorn.UcError as e:
log.error("Error emulating shellcode: %s", e)
class ShikataX86(ShellcodeX86):
def init(self):
self.bblcount = 0
self.start = None
self.emu.hook_add(unicorn.UC_HOOK_BLOCK, self.hook_block)
def hook_block(self, uc, addr, size, user_data):
if not size:
return
self.bblcount += 1
if self.bblcount == 2:
self.start = addr
return False
def result(self):
start = self.start or self.addr
return self.emu.mem_read(start, len(self.sc) - start + self.addr)
def Shellcode(arch="x86", mode=32, sc=None, cls=ShellcodeX86):
# TODO For now only 32-bit x86 shellcode is supported.
return cls(archs[arch], modes[mode], sc)
def shikata(sc):
s = Shellcode(sc=sc, cls=ShikataX86)
s.map_memory()
s.run(count=0x1000)
return str(s.result())
+1
View File
@@ -6,6 +6,7 @@ testpaths = tests
norecursedirs = tests/darwin
django_find_project = false
python_paths = . cuckoo/web
xfail_strict = true
[aliases]
test = pytest
+1
View File
@@ -177,6 +177,7 @@ do_setup(
"python-magic==0.4.12",
"sflock>=0.2.12, <0.3",
"sqlalchemy==1.0.8",
"unicorn==1.0.0",
"wakeonlan==0.2.2",
],
extras_require={
+2
View File
@@ -0,0 +1,2 @@
╫Ъ┤}"ыюыt$Т[)и╠Z1k┐КЭkr│йc}z В÷:cКmЁГ╧│8╔)Lb]▓ЗTP#V╓С╖╓Ыс√g ч∙ЩG╥рPx╪╝hС▌?ИЮG>ь╤эЗ90Ё!U␍ы╜у▄ Э"r0Е:╡ВIй ╗J qvч┼яЩxwЦрЭО÷TZЛ╦п╚?7≥О⌠а╢┌╞:тд··╒⌠Э╙▀и┼*;eEрщ╢уSЬCN5≈╤#etj╚Ё,У▄;V│╘╔
vFF╟x√~Ex√~yJ╝LЭМнЮ√╕G÷║╥█)КF*фzyu)L-/╔≥└А║РhW╒ЭZT\Щс{6ЫЁьW[⌠═и╓Ь╔rP┐P╦╖$┴=≈╝8qb┬U}9┬П┌■╖╪(=Е~H=╔~U}зх@┌В|ы.~e┴╦─J69сэ^+Ei|╢╪ОA?С{FахЫ┴╢+Yиh[/2id╖╩А╢;!|╬рйЕFe■Еu╝┘ТА1lЙ
Binary file not shown.
Binary file not shown.
+4
View File
@@ -0,0 +1,4 @@
¾Øm`pÙÉÙt$ôX)ɱN1ppƒÀÜ�•Œ4ÍVmIJ߈õò„Ù¥ÂÏŒI¨‚$ÚÜ
JkjmelÇMäî‚ÆÏÔ×UÁFˆJfà4²Œµ0ˆ‡ï’*D„š4‰¡UÎy]d°žËg}m¯¹Ž`Ùº3sÁïö…a{ a�¨7áž3­‚˜�ž
7a<Ž1]—ø”bÇ£IǃI�zÎR·ñÕüÀ‚ç£z␍K+¥Ê¬DS©bL—ý2æ>~Ùö¿«N§o/ÏôÇ}À+÷} D�³ká÷×’Ã'ëzH#eð¾Å‚¾qvB‹wFxA”zÌÑ„Ž�_bÚ� <r7¶ã¸Ž²#2=Bí³HP™3
K½!¯Ù:àøuAÕÎÙº0EÓ.û1¿ûÁJÕû©*�¯Ì4Ü]¡£µ2bÌ;mDSÃXT¯¤"Á¦
Binary file not shown.
Binary file not shown.
+36
View File
@@ -0,0 +1,36 @@
# Copyright (C) 2017 Cuckoo Foundation.
# This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org
# See the file 'docs/LICENSE' for copying permission.
import pytest
from cuckoo.common.shellcode import Shellcode, shikata
class TestShikata(object):
def setup(self):
self.sc = Shellcode()
def shikata(self, filename):
return shikata(
open("tests/files/shellcode/shikata/%s" % filename, "rb").read()
)
def test_unicorn100_good(self):
assert self.shikata("1.bin").startswith("\xfc\xe8\x82")
assert self.shikata("3.bin").startswith("\xfc\xe8\x82")
assert self.shikata("4.bin").startswith("\xfc\xe8\x82")
@pytest.mark.xfail
def test_unicorn100_bad1(self):
assert self.shikata("2.bin").startswith("\xfc\xe8\x82")
@pytest.mark.xfail
def test_unicorn100_bad2(self):
assert self.shikata("5.bin").startswith("\xfc\xe8\x82")
@pytest.mark.xfail
def test_unicorn100_bad3(self):
assert self.shikata("6.bin").startswith("\xfc\xe8\x82")
def test_infinite_loop(self):
assert shikata("\xeb\xfe") == "\xeb\xfe"