Skip to content

Commit 04d786d

Browse files
committed
Add constant renderer API
1 parent dc1094f commit 04d786d

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed

binaryninjacore.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ extern "C"
311311
typedef struct BNStringRef BNStringRef;
312312
typedef struct BNIndirectBranchInfo BNIndirectBranchInfo;
313313
typedef struct BNArchitectureAndAddress BNArchitectureAndAddress;
314+
typedef struct BNConstantRenderer BNConstantRenderer;
314315

315316
typedef bool(*BNProgressFunction)(void*, size_t, size_t);
316317

@@ -3798,6 +3799,15 @@ extern "C"
37983799
char* value;
37993800
} BNTypeAttribute;
38003801

3802+
typedef struct BNCustomConstantRenderer
3803+
{
3804+
void* context;
3805+
void (*freeObject)(void* ctxt);
3806+
bool (*isValidForType)(void* ctxt, BNHighLevelILFunction* hlil, BNType* type);
3807+
bool (*renderConstantPointer)(void* ctxt, BNHighLevelILFunction* hlil, size_t expr, BNType* type, int64_t val,
3808+
BNHighLevelILTokenEmitter* tokens, BNDisassemblySettings* settings, BNOperatorPrecedence precedence);
3809+
} BNCustomConstantRenderer;
3810+
38013811
BINARYNINJACOREAPI char* BNAllocString(const char* contents);
38023812
BINARYNINJACOREAPI char* BNAllocStringWithLength(const char* contents, size_t len);
38033813
BINARYNINJACOREAPI void BNFreeString(char* str);
@@ -8661,6 +8671,14 @@ extern "C"
86618671
BINARYNINJACOREAPI const char* BNGetStringRefContents(BNStringRef* ref);
86628672
BINARYNINJACOREAPI size_t BNGetStringRefSize(BNStringRef* ref);
86638673

8674+
// Constant Renderers
8675+
BINARYNINJACOREAPI BNConstantRenderer* BNRegisterConstantRenderer(
8676+
const char* name, BNCustomConstantRenderer* renderer);
8677+
BINARYNINJACOREAPI BNConstantRenderer* BNGetConstantRendererByName(const char* name);
8678+
BINARYNINJACOREAPI BNConstantRenderer** BNGetConstantRendererList(size_t* count);
8679+
BINARYNINJACOREAPI void BNFreeConstantRendererList(BNLanguageRepresentationFunctionType** renderers);
8680+
BINARYNINJACOREAPI char* BNGetConstantRendererName(BNConstantRenderer* renderer);
8681+
86648682
#ifdef __cplusplus
86658683
}
86668684
#endif

python/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
from .languagerepresentation import *
8383
from .lineformatter import *
8484
from .renderlayer import *
85+
from .constantrenderer import *
8586
# We import each of these by name to prevent conflicts between
8687
# log.py and the function 'log' which we don't import below
8788
from .log import (

python/constantrenderer.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Copyright (c) 2015-2025 Vector 35 Inc
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to
5+
# deal in the Software without restriction, including without limitation the
6+
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7+
# sell copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19+
# IN THE SOFTWARE.
20+
21+
import traceback
22+
import ctypes
23+
from typing import Optional
24+
25+
import binaryninja
26+
from . import _binaryninjacore as core
27+
from . import function
28+
from . import enums
29+
from .log import log_error_for_exception
30+
from . import types
31+
from . import highlevelil
32+
from . import languagerepresentation
33+
34+
35+
class ConstantRenderer:
36+
_registered_renderers = []
37+
renderer_name = None
38+
39+
def __init__(self, handle=None):
40+
if handle is not None:
41+
self.handle = core.handle_of_type(handle, core.BNConstantRenderer)
42+
43+
def register(self):
44+
if self.__class__.renderer_name is None:
45+
raise ValueError("Renderer name is missing")
46+
self._cb = core.BNCustomConstantRenderer()
47+
self._cb.context = 0
48+
self._cb.isValidForType = self._cb.isValidForType.__class__(self._is_valid_for_type)
49+
self._cb.renderConstantPointer = self._cb.renderConstantPointer.__class__(self._render_constant_pointer)
50+
self.handle = core.BNRegisterConstantRenderer(self.__class__.renderer_name, self._cb)
51+
self.__class__._registered_renderers.append(self)
52+
53+
def _is_valid_for_type(self, ctxt, hlil, type):
54+
try:
55+
hlil = highlevelil.HighLevelILFunction(handle=core.BNNewHighLevelILFunctionReference(hlil))
56+
type = types.Type.create(handle=core.BNNewTypeReference(type))
57+
return self.is_valid_for_type(hlil, type)
58+
except:
59+
log_error_for_exception("Unhandled Python exception in ConstantRenderer._is_valid_for_type")
60+
return False
61+
62+
def _render_constant_pointer(self, ctxt, hlil, expr, type, val, tokens, settings, precedence):
63+
try:
64+
hlil = highlevelil.HighLevelILFunction(handle=core.BNNewHighLevelILFunctionReference(hlil))
65+
type = types.Type.create(handle=core.BNNewTypeReference(type))
66+
tokens = languagerepresentation.HighLevelILTokenEmitter(core.BNNewHighLevelILTokenEmitterReference(tokens))
67+
if settings is not None:
68+
settings = function.DisassemblySettings(core.BNNewDisassemblySettingsReference(settings))
69+
instr = hlil.get_expr(highlevelil.ExpressionIndex(expr))
70+
return self.render_constant_pointer(instr, type, val, tokens, settings, precedence)
71+
except:
72+
log_error_for_exception("Unhandled Python exception in ConstantRenderer._render_constant_pointer")
73+
return False
74+
75+
def is_valid_for_type(self, func: 'highlevelil.HighLevelILInstruction', type: 'types.Type') -> bool:
76+
return False
77+
78+
def render_constant_pointer(
79+
self, instr: 'highlevelil.HighLevelILInstruction', type: 'types.Type', val: int,
80+
tokens: 'languagerepresentation.HighLevelILTokenEmitter',
81+
settings: Optional['function.DisassemblySettings'], precedence: 'enums.OperatorPrecedence'
82+
) -> bool:
83+
return False

python/examples/encoded_strings.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from binaryninja import (ConstantRenderer, PointerType, InstructionTextToken, InstructionTextTokenType, DataBuffer)
2+
3+
4+
class EncodedStringConstantRenderer(ConstantRenderer):
5+
renderer_name = "encoded_strings"
6+
decoders = {
7+
"xor_encoded": lambda encoded, key: encoded ^ key,
8+
"sub_encoded": lambda encoded, key: (encoded - key) & 0xff,
9+
"add_encoded": lambda encoded, key: (encoded + key) & 0xff
10+
}
11+
12+
def is_valid_for_type(self, func, type):
13+
if not isinstance(type, PointerType):
14+
return False
15+
for name in self.__class__.decoders.keys():
16+
if name in type.target.attributes:
17+
return True
18+
return False
19+
20+
def render_constant_pointer(self, instr, type, val, tokens, settings, precedence):
21+
if not isinstance(type, PointerType):
22+
return False
23+
24+
values = None
25+
decoder = None
26+
for name in self.__class__.decoders.keys():
27+
if name in type.target.attributes:
28+
try:
29+
values = bytes.fromhex(type.target.attributes[name])
30+
decoder = self.__class__.decoders[name]
31+
except:
32+
return False
33+
if values is None or decoder is None:
34+
return False
35+
36+
encoded_null = "encoded_null" in type.target.attributes
37+
38+
result = b""
39+
i = 0
40+
while True:
41+
byte = instr.function.view.read(val + i, 1)
42+
if len(byte) != 1:
43+
return False
44+
if not encoded_null and byte[0] == 0:
45+
break
46+
byte = decoder(byte[0], values[i % len(values)])
47+
if byte == 0:
48+
break
49+
result += bytes([byte])
50+
i += 1
51+
52+
tokens.append(InstructionTextToken(InstructionTextTokenType.BraceToken, "\""))
53+
tokens.append(InstructionTextToken(InstructionTextTokenType.StringToken, DataBuffer(result).escape()))
54+
tokens.append(InstructionTextToken(InstructionTextTokenType.BraceToken, "\"_enc"))
55+
return True
56+
57+
58+
EncodedStringConstantRenderer().register()

0 commit comments

Comments
 (0)