Skip to content

Commit

Permalink
adding first cypher output!
Browse files Browse the repository at this point in the history
Signed-off-by: vsoch <vsoch@users.noreply.github.com>
  • Loading branch information
vsoch committed Jan 23, 2022
1 parent 4244cd7 commit 6cea9b0
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 28 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,44 @@ would be missing a symbol.

```bash
$ elfcall gen data/libfoo.so --fmt cypher
CREATE (omyaovuh:ELF {name: 'libfoo.so', label: 'libfoo.so'}),
(ilfrbqrc:ELF {name: 'libstdc++.so.6', label: 'libstdc++.so.6'}),
(vyiefgcr:ELF {name: 'libm.so.6', label: 'libm.so.6'}),
(gnxoyhkm:ELF {name: 'libc.so.6', label: 'libc.so.6'}),
(fvynaahi:ELF {name: 'ld-linux-x86-64.so.2', label: 'ld-linux-x86-64.so.2'}),
(hsrlkhie:ELF {name: 'libgcc_s.so.1', label: 'libgcc_s.so.1'}),
(kgyffmqn:SYMBOL {name: '__cxa_finalize', label: '__cxa_finalize', type: 'FUNC'}),
(bieoloch:SYMBOL {name: '_ZNSt8ios_base4InitC1Ev', label: '_ZNSt8ios_base4InitC1Ev', type: 'FUNC'}),
(owpwqsyl:SYMBOL {name: '__cxa_atexit', label: '__cxa_atexit', type: 'FUNC'}),
(stndoxns:SYMBOL {name: '_ZNSt8ios_base4InitD1Ev', label: '_ZNSt8ios_base4InitD1Ev', type: 'FUNC'}),
(omyaovuh)-[:LINKSWITH]->(ilfrbqrc),
(omyaovuh)-[:LINKSWITH]->(lxtmuvsv),
(ilfrbqrc)-[:LINKSWITH]->(vyiefgcr),
(ilfrbqrc)-[:LINKSWITH]->(gnxoyhkm),
(ilfrbqrc)-[:LINKSWITH]->(fvynaahi),
(ilfrbqrc)-[:LINKSWITH]->(hsrlkhie),
(lxtmuvsv)-[:LINKSWITH]->(fvynaahi),
(ilfrbqrc)-[:EXPORTS]->(bieoloch),
(ilfrbqrc)-[:EXPORTS]->(stndoxns),
(lxtmuvsv)-[:EXPORTS]->(kgyffmqn),
(lxtmuvsv)-[:EXPORTS]->(owpwqsyl),
(omyaovuh)-[:NEEDS]->(kgyffmqn),
(omyaovuh)-[:NEEDS]->(owpwqsyl),
(omyaovuh)-[:NEEDS]->(bieoloch),
(omyaovuh)-[:NEEDS]->(stndoxns);
```

If you test the output in [https://sandbox.neo4j.com/](https://sandbox.neo4j.com/) by first running the code to generate nodes
and then doing:

```cypher
MATCH (n) RETURN (n)
```

You should see:

![docs/assets/img/cypher.png](docs/assets/img/cypher.png)

Note that this is under development, and eventually we will have different graph generation
options (right now we print to the screen).

Expand Down
Binary file added docs/assets/img/cypher.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions elfcall/main/graph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import secrets
import string
import sys
import os


class GraphBase:
Expand All @@ -16,6 +17,7 @@ def __init__(self, target, results, outfile=None):
self.uids = {}
self.symbol_uids = {}
self.linked_libs = {}
self.fullpaths = {}
self.target = target
self.parse()
self._outfile = outfile
Expand Down Expand Up @@ -60,6 +62,17 @@ def parse(self):
self.uids[meta["lib"]["fullpath"]] = self.generate_placeholder()
self.linked_libs[meta["lib"]["fullpath"]] = meta["linked_libs"]

# We only need fullpaths for linked libs (not from needed) to get fullpath
basename = os.path.basename(meta["lib"]["fullpath"])
if (
basename in self.fullpaths
and self.fullpaths[basename] != meta["lib"]["fullpath"]
):
logger.warning(
"Warning: a library of the same name (and different path) exists, graph output might not be correct."
)
self.fullpaths[basename] = meta["lib"]["fullpath"]

for filename, linked_libs in self.linked_libs.items():
for linked_lib in linked_libs:
self.uids[linked_lib] = self.generate_placeholder()
Expand Down
66 changes: 38 additions & 28 deletions elfcall/main/graph/cypher.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,47 @@ def generate(self):
"(%s:ELF {name: '%s', label: '%s'}),\n"
% (
self.uids[self.target["name"]],
self.target["name"],
os.path.basename(self.target["name"]),
os.path.basename(self.target["name"]),
)
)

# Create elf for main files and those linked to
seen_libs = set()
seen_libs.add(self.target["name"])
for filename, symbols in self.organized.items():
fd.write(
"(%s:ELF {name: '%s', label: '%s'}),\n"
% (self.uids[filename], filename, os.path.basename(filename))
)
if os.path.basename(filename) in self.fullpaths:
filename = self.fullpaths[os.path.basename(filename)]
if filename not in seen_libs:
fd.write(
"(%s:ELF {name: '%s', label: '%s'}),\n"
% (
self.uids[filename],
os.path.basename(filename),
os.path.basename(filename),
)
)
seen_libs.add(filename)

# Now Record linked dependencies
for linked_lib in self.linked_libs[filename]:

# linked_libs often are just the basename, but we don't want to add twice
# so we store the fullpaths here to resolve to fullpath
if linked_lib in self.fullpaths:
linked_lib = self.fullpaths[linked_lib]
if linked_lib in seen_libs:
continue
seen_libs.add(linked_lib)
fd.write(
"(%s:ELF {name: '%s', label: '%s'}),\n"
% (
self.uids[linked_lib],
os.path.basename(linked_lib),
os.path.basename(linked_lib),
)
)
seen_libs.add(linked_lib)

# Found symbols plus needed imported (not necessarily all are found)
imported = self.get_found_imported()
Expand Down Expand Up @@ -87,6 +118,8 @@ def generate(self):
# Now Record linked dependencies
for filename, symbols in self.organized.items():
for linked_lib in self.linked_libs[filename]:
if linked_lib in self.fullpaths:
linked_lib = self.fullpaths[linked_lib]
fd.write(
"(%s)-[:LINKSWITH]->(%s),\n"
% (
Expand Down Expand Up @@ -118,26 +151,3 @@ def generate(self):

if self.outfile != sys.stdout:
fd.close()


"""
CREATE (ubbqekvj:ELF {name: '/usr/lib/x86_64-linux-gnu/libstdc++.so.6', label: 'libstdc++.so.6'}),
(bhtpddun:SYMBOL {name: '__cxa_finalize', label: '__cxa_finalize', type: 'FUNC'}),
(jsbdirzz:SYMBOL {name: '_ZNSt8ios_base4InitD1Ev', label: '_ZNSt8ios_base4InitD1Ev', type: 'FUNC'}),
(pgrcwngj:SYMBOL {name: '_ZNSt8ios_base4InitC1Ev', label: '_ZNSt8ios_base4InitC1Ev', type: 'FUNC'}),
(neuyhhih:SYMBOL {name: '__cxa_atexit', label: '__cxa_atexit', type: 'FUNC'}),
(ubbqekvj)-[:LINKSWITH]->(vygfepln),
(ubbqekvj)-[:LINKSWITH]->(luhufmsq),
(ubbqekvj)-[:LINKSWITH]->(minajnwv),
(ubbqekvj)-[:LINKSWITH]->(xavckaxz),
(smajchxe)-[:LINKSWITH]->(minajnwv),
(smajchxe)-[:EXPORTS]->(bhtpddun),
(smajchxe)-[:EXPORTS]->(jsbdirzz),
(smajchxe)-[:EXPORTS]->(neuyhhih),
(smajchxe)-[:EXPORTS]->(pgrcwngj),
(ubbqekvj)-[:USES]->(pgrcwngj),
(ubbqekvj)-[:USES]->(jsbdirzz),
(smajchxe)-[:USES]->(bhtpddun),
(smajchxe)-[:USES]->(neuyhhih);
MATCH (n) RETURN (n)
"""

0 comments on commit 6cea9b0

Please sign in to comment.