pcode-graph 3.0.0__tar.gz → 3.0.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/CHANGELOG +8 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/PKG-INFO +9 -1
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/README.md +8 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/lief_importer.py +10 -2
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/simplifier.py +20 -21
- pcode_graph-3.0.2/tests/lief_importer_test.py +40 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/.github/workflows/release.yml +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/.github/workflows/test.yml +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/.gitignore +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/LICENSE +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/__init__.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/__main__.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/analysis.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/arch.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/asm.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/basic_blocks.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/bit_utils.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/characterization.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/checker.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/cli.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/emulator.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/gnn_exporter.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/graph.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/log.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/maker.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/memory.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/nx_exporter.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/pcode.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/registers.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/torch_utils.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/translator.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/view.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pcode_graph/visitor.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/pyproject.toml +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/requirements.txt +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/tests/__init__.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/tests/analysis_test.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/tests/basic_block_test.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/tests/callother_test.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/tests/charac_test.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/tests/cli_test.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/tests/conftest.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/tests/emulator_test.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/tests/exporter_test.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/tests/fixtures.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/tests/helpers.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/tests/maker_test.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/tests/memory_test.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/tests/translator_test.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/tests/utils_test.py +0 -0
- {pcode_graph-3.0.0 → pcode_graph-3.0.2}/todo.md +0 -0
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
# 3.0.2
|
|
2
|
+
|
|
3
|
+
- Improve simplifier complexity (for graph with many edges.)
|
|
4
|
+
|
|
5
|
+
# 3.0.1
|
|
6
|
+
|
|
7
|
+
- Fix bug in lief_exporter.iter_functions for non relocatable files.
|
|
8
|
+
|
|
1
9
|
# 3.0.0
|
|
2
10
|
|
|
3
11
|
- Add stack memory analysis to resolve loads whenever it possible, mainly for code compiled with -O0.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: pcode_graph
|
|
3
|
-
Version: 3.0.
|
|
3
|
+
Version: 3.0.2
|
|
4
4
|
Summary: Build, display and vectorize semantic graphs from binary code
|
|
5
5
|
Project-URL: Homepage, https://github.com/quarkslab/pcode_graph
|
|
6
6
|
Project-URL: Issues, https://github.com/quarkslab/pcode_graph/issues
|
|
@@ -30,6 +30,14 @@ Description-Content-Type: text/markdown
|
|
|
30
30
|
This library helps to build, display and vectorize the Control-Data Graph (CDG) of a
|
|
31
31
|
chunk of binary code, using Ghidra P-Code representation.
|
|
32
32
|
|
|
33
|
+
It ams at producing compact graphs, with a diameter as low as possible, but without losing the accurate semantics of the code.
|
|
34
|
+
|
|
35
|
+
Analyses remain low-level: no assumption is done on the used ABI in order to support exotic code for various task:
|
|
36
|
+
- semantic-based similarity research
|
|
37
|
+
- static code deobfuscation
|
|
38
|
+
- ROP gadget chaining
|
|
39
|
+
|
|
40
|
+
|
|
33
41
|
## Command-line interface
|
|
34
42
|
|
|
35
43
|
A `cdg` tool is provided to play with binary of assembly files.
|
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
This library helps to build, display and vectorize the Control-Data Graph (CDG) of a
|
|
4
4
|
chunk of binary code, using Ghidra P-Code representation.
|
|
5
5
|
|
|
6
|
+
It ams at producing compact graphs, with a diameter as low as possible, but without losing the accurate semantics of the code.
|
|
7
|
+
|
|
8
|
+
Analyses remain low-level: no assumption is done on the used ABI in order to support exotic code for various task:
|
|
9
|
+
- semantic-based similarity research
|
|
10
|
+
- static code deobfuscation
|
|
11
|
+
- ROP gadget chaining
|
|
12
|
+
|
|
13
|
+
|
|
6
14
|
## Command-line interface
|
|
7
15
|
|
|
8
16
|
A `cdg` tool is provided to play with binary of assembly files.
|
|
@@ -170,6 +170,10 @@ def iter_functions(binary: Path | lief.Binary) -> Iterator[FunctionCode]:
|
|
|
170
170
|
cache: dict[Any, tuple[bytes, int]] = {}
|
|
171
171
|
sym: lief.ELF.Symbol
|
|
172
172
|
|
|
173
|
+
# In relocatable objects, symbol values are offsets in their section, whereas
|
|
174
|
+
# in executables and shared libraries they are virtual addresses.
|
|
175
|
+
relocatable = binary.header.file_type == lief.ELF.Header.FILE_TYPE.REL # type: ignore
|
|
176
|
+
|
|
173
177
|
for sym in binary.symtab_symbols: # type: ignore
|
|
174
178
|
if sym.type != lief.ELF.Symbol.TYPE.FUNC or sym.size == 0:
|
|
175
179
|
continue
|
|
@@ -182,12 +186,16 @@ def iter_functions(binary: Path | lief.Binary) -> Iterator[FunctionCode]:
|
|
|
182
186
|
cache[section_id] = (bytes(section.content), section.virtual_address)
|
|
183
187
|
|
|
184
188
|
section_content, section_address = cache[section_id]
|
|
189
|
+
offset = sym.value if relocatable else sym.value - section_address
|
|
190
|
+
if offset < 0 or offset + sym.size > len(section_content):
|
|
191
|
+
logger.warning(f"Skip function {sym.name}: out of section {section_id}")
|
|
192
|
+
continue
|
|
185
193
|
|
|
186
194
|
assert isinstance(sym.name, str)
|
|
187
195
|
yield FunctionCode(
|
|
188
196
|
sym.name,
|
|
189
|
-
section_address +
|
|
190
|
-
section_content[
|
|
197
|
+
section_address + offset,
|
|
198
|
+
section_content[offset : offset + sym.size],
|
|
191
199
|
)
|
|
192
200
|
|
|
193
201
|
|
|
@@ -13,6 +13,9 @@ from pathlib import Path
|
|
|
13
13
|
|
|
14
14
|
|
|
15
15
|
class GraphSimplifier:
|
|
16
|
+
"""This simplifier is built to work with flat CFG, Basic Block nodes
|
|
17
|
+
and Membership edges are not taken into account.
|
|
18
|
+
"""
|
|
16
19
|
|
|
17
20
|
def __init__(
|
|
18
21
|
self,
|
|
@@ -64,27 +67,23 @@ class GraphSimplifier:
|
|
|
64
67
|
self.graph.nodes[index].kind == NodeKinds.Phi and len(set(data_preds)) == 1
|
|
65
68
|
), "More than one data predecessor to the node to remove"
|
|
66
69
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if e.
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
else:
|
|
85
|
-
remove = True
|
|
86
|
-
|
|
87
|
-
if remove:
|
|
70
|
+
|
|
71
|
+
for i in list(proxy.out_edge_indexes):
|
|
72
|
+
e = self.graph.edges[i]
|
|
73
|
+
if e.kind == EdgeKinds.Data:
|
|
74
|
+
self._proxy[e.source_node].out_edge_indexes.remove(i)
|
|
75
|
+
e.source_node = data_preds[0]
|
|
76
|
+
self._proxy[e.source_node].out_edge_indexes.append(i)
|
|
77
|
+
else:
|
|
78
|
+
self._remove_edge(i)
|
|
79
|
+
|
|
80
|
+
for i in list(proxy.in_edge_indexes):
|
|
81
|
+
e = self.graph.edges[i]
|
|
82
|
+
if e.kind == EdgeKinds.Control:
|
|
83
|
+
self._proxy[e.destination_node].in_edge_indexes.remove(i)
|
|
84
|
+
e.destination_node = control_succs[0]
|
|
85
|
+
self._proxy[e.destination_node].in_edge_indexes.append(i)
|
|
86
|
+
else:
|
|
88
87
|
self._remove_edge(i)
|
|
89
88
|
|
|
90
89
|
# Patch the graph
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import shutil
|
|
2
|
+
import subprocess
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
import lief
|
|
6
|
+
import pytest
|
|
7
|
+
|
|
8
|
+
from pcode_graph.lief_importer import FunctionCode, iter_functions, parse_binary
|
|
9
|
+
|
|
10
|
+
SOURCE = """
|
|
11
|
+
int leaf(int value) { return value * 3 + 7; }
|
|
12
|
+
int main(int argc, char **argv) { return leaf(argc); }
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def compile_source(directory: Path, output_name: str, *options: str) -> Path:
|
|
17
|
+
source_path = directory / "source.c"
|
|
18
|
+
source_path.write_text(SOURCE)
|
|
19
|
+
output_path = directory / output_name
|
|
20
|
+
subprocess.run(["gcc", "-O0", *options, "-o", output_path, source_path], check=True)
|
|
21
|
+
return output_path
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def get_leaf(binary_path: Path) -> FunctionCode:
|
|
25
|
+
return next(f for f in iter_functions(binary_path) if f.name == "leaf")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@pytest.mark.skipif(shutil.which("gcc") is None, reason="gcc not available")
|
|
29
|
+
@pytest.mark.parametrize("link_options", [["-pie"], ["-no-pie"]])
|
|
30
|
+
def test_iter_functions_executable(tmp_path: Path, link_options: list[str]):
|
|
31
|
+
object_leaf = get_leaf(compile_source(tmp_path, "source.o", "-c"))
|
|
32
|
+
executable_path = compile_source(tmp_path, "program", *link_options)
|
|
33
|
+
executable_leaf = get_leaf(executable_path)
|
|
34
|
+
|
|
35
|
+
symbol = parse_binary(executable_path).get_symbol("leaf")
|
|
36
|
+
assert isinstance(symbol, lief.ELF.Symbol)
|
|
37
|
+
assert executable_leaf.address == symbol.value
|
|
38
|
+
# Leaf function code has no relocation: identical in object and executable
|
|
39
|
+
assert len(executable_leaf.content) == symbol.size
|
|
40
|
+
assert executable_leaf.content == object_leaf.content
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|