pcode-graph 3.0.0__tar.gz → 3.0.1__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.
Files changed (51) hide show
  1. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/CHANGELOG +4 -0
  2. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/PKG-INFO +9 -1
  3. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/README.md +8 -0
  4. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/lief_importer.py +10 -2
  5. pcode_graph-3.0.1/tests/lief_importer_test.py +40 -0
  6. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/.github/workflows/release.yml +0 -0
  7. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/.github/workflows/test.yml +0 -0
  8. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/.gitignore +0 -0
  9. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/LICENSE +0 -0
  10. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/__init__.py +0 -0
  11. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/__main__.py +0 -0
  12. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/analysis.py +0 -0
  13. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/arch.py +0 -0
  14. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/asm.py +0 -0
  15. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/basic_blocks.py +0 -0
  16. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/bit_utils.py +0 -0
  17. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/characterization.py +0 -0
  18. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/checker.py +0 -0
  19. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/cli.py +0 -0
  20. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/emulator.py +0 -0
  21. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/gnn_exporter.py +0 -0
  22. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/graph.py +0 -0
  23. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/log.py +0 -0
  24. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/maker.py +0 -0
  25. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/memory.py +0 -0
  26. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/nx_exporter.py +0 -0
  27. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/pcode.py +0 -0
  28. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/registers.py +0 -0
  29. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/simplifier.py +0 -0
  30. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/torch_utils.py +0 -0
  31. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/translator.py +0 -0
  32. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/view.py +0 -0
  33. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pcode_graph/visitor.py +0 -0
  34. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/pyproject.toml +0 -0
  35. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/requirements.txt +0 -0
  36. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/tests/__init__.py +0 -0
  37. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/tests/analysis_test.py +0 -0
  38. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/tests/basic_block_test.py +0 -0
  39. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/tests/callother_test.py +0 -0
  40. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/tests/charac_test.py +0 -0
  41. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/tests/cli_test.py +0 -0
  42. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/tests/conftest.py +0 -0
  43. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/tests/emulator_test.py +0 -0
  44. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/tests/exporter_test.py +0 -0
  45. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/tests/fixtures.py +0 -0
  46. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/tests/helpers.py +0 -0
  47. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/tests/maker_test.py +0 -0
  48. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/tests/memory_test.py +0 -0
  49. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/tests/translator_test.py +0 -0
  50. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/tests/utils_test.py +0 -0
  51. {pcode_graph-3.0.0 → pcode_graph-3.0.1}/todo.md +0 -0
@@ -1,3 +1,7 @@
1
+ # 3.0.1
2
+
3
+ - Fix bug in lief_exporter.iter_functions for non relocatable files.
4
+
1
5
  # 3.0.0
2
6
 
3
7
  - 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.0
3
+ Version: 3.0.1
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 + sym.value,
190
- section_content[sym.value : sym.value + sym.size],
197
+ section_address + offset,
198
+ section_content[offset : offset + sym.size],
191
199
  )
192
200
 
193
201
 
@@ -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