Skip to content

Instantly share code, notes, and snippets.

@mfornos
Last active October 17, 2022 15:47
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 mfornos/7848c7d9d67eefe0e41418981a52212d to your computer and use it in GitHub Desktop.
Save mfornos/7848c7d9d67eefe0e41418981a52212d to your computer and use it in GitHub Desktop.
Heuristics on Substrate contracts pallet source language detection
import sys
from ppci import wasm
m = wasm.Module(sys.stdin.buffer.read())
sections = m.get_definitions_per_section()
imports = sections['import']
start = sections['start']
customs = sections['custom']
mem = next(filter(lambda x : x.kind == 'memory', imports))
def hasName(name):
lambda x : x.name == name
# Tentative heuristics
# (!) NOT tested with a reasonable sample set of binaries
# Solang
# * emits memory as the first import
# * has custom sections named 'name' and 'producers'
isSolang = bool(
imports[0].kind == 'memory'
and not start
and customs
and any(filter(hasName('name'), customs))
)
# Ask!
# * has a start section
# * has custom section named 'sourceMappingURL'
# * imports declare memory after functions
isAsk = bool(
imports[0].kind != 'memory'
and start
and customs
and any(filter(hasName('sourceMappingURL'), customs))
)
# Ink!
# * no custom sections
# * no start section
# * imports declare memory after functions
isInk = bool(
imports[0].kind != 'memory'
and not start
and not customs
)
print(f"""
Detection results
-----------------
Ink! = {isInk}
Ask! = {isAsk}
Sol = {isSolang}
""")
❯ python3 contract-lang-detect.py < blobs/flipper.ink.wasm
Detection results
-----------------
Ink! = True
Ask! = False
Sol = False
❯ python3 contract-lang-detect.py < blobs/flipper.ask.wasm
Detection results
-----------------
Ink! = False
Ask! = True
Sol = False
❯ python3 contract-lang-detect.py < blobs/flipper.sol.wasm
Detection results
-----------------
Ink! = False
Ask! = False
Sol = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment