Skip to content

Commit eee045f

Browse files
committed
Add constant renderer API
1 parent 8a9be0f commit eee045f

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed

binaryninjacore.h

Lines changed: 19 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

@@ -3806,6 +3807,16 @@ extern "C"
38063807
char* value;
38073808
} BNTypeAttribute;
38083809

3810+
typedef struct BNCustomConstantRenderer
3811+
{
3812+
void* context;
3813+
void (*freeObject)(void* ctxt);
3814+
bool (*isValidForType)(void* ctxt, BNHighLevelILFunction* hlil, BNType* type);
3815+
bool (*renderConstantPointer)(void* ctxt, BNHighLevelILFunction* hlil, size_t expr, BNType* type, int64_t val,
3816+
BNHighLevelILTokenEmitter* tokens, BNDisassemblySettings* settings, BNSymbolDisplayType symbolDisplay,
3817+
BNOperatorPrecedence precedence);
3818+
} BNCustomConstantRenderer;
3819+
38093820
BINARYNINJACOREAPI char* BNAllocString(const char* contents);
38103821
BINARYNINJACOREAPI char* BNAllocStringWithLength(const char* contents, size_t len);
38113822
BINARYNINJACOREAPI void BNFreeString(char* str);
@@ -8675,6 +8686,14 @@ extern "C"
86758686
BINARYNINJACOREAPI const char* BNGetStringRefContents(BNStringRef* ref);
86768687
BINARYNINJACOREAPI size_t BNGetStringRefSize(BNStringRef* ref);
86778688

8689+
// Constant Renderers
8690+
BINARYNINJACOREAPI BNConstantRenderer* BNRegisterConstantRenderer(
8691+
const char* name, BNCustomConstantRenderer* renderer);
8692+
BINARYNINJACOREAPI BNConstantRenderer* BNGetConstantRendererByName(const char* name);
8693+
BINARYNINJACOREAPI BNConstantRenderer** BNGetConstantRendererList(size_t* count);
8694+
BINARYNINJACOREAPI void BNFreeConstantRendererList(BNLanguageRepresentationFunctionType** renderers);
8695+
BINARYNINJACOREAPI char* BNGetConstantRendererName(BNConstantRenderer* renderer);
8696+
86788697
#ifdef __cplusplus
86798698
}
86808699
#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: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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, symbol_display, 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+
symbol_display = enums.SymbolDisplayType(symbol_display)
70+
instr = hlil.get_expr(highlevelil.ExpressionIndex(expr))
71+
return self.render_constant_pointer(instr, type, val, tokens, settings, symbol_display, precedence)
72+
except:
73+
log_error_for_exception("Unhandled Python exception in ConstantRenderer._render_constant_pointer")
74+
return False
75+
76+
def is_valid_for_type(self, func: 'highlevelil.HighLevelILFunction', type: 'types.Type') -> bool:
77+
return True
78+
79+
def render_constant_pointer(
80+
self, instr: 'highlevelil.HighLevelILInstruction', type: 'types.Type', val: int,
81+
tokens: 'languagerepresentation.HighLevelILTokenEmitter',
82+
settings: Optional['function.DisassemblySettings'], symbol_display: 'enums.SymbolDisplayType',
83+
precedence: 'enums.OperatorPrecedence'
84+
) -> bool:
85+
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)