Last active
October 17, 2022 15:47
-
-
Save mfornos/7848c7d9d67eefe0e41418981a52212d to your computer and use it in GitHub Desktop.
Heuristics on Substrate contracts pallet source language detection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | |
""") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
❯ 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