123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- from neo4j import GraphDatabase, basic_auth
- import re
- def import_data(session):
- nodes = {}
- index = 1
-
- file_object = open('data/r-graph.txt', 'r')
- try:
- for line in file_object:
- match_obj = re.match(r'(\S+) -> (\S+)', line, re.M | re.I)
- if match_obj:
- path = match_obj.group(1)
- ref = match_obj.group(2)
-
- if path not in nodes.keys():
- path_node = "Page%d" % index
- nodes[path] = path_node
- sql = "create (%s:Page {url:\"%s\" , id:\"%d\",in:0,out:0})" % (path_node, path, index)
- index = index + 1
- session.run(sql)
-
-
- if ref not in nodes.keys():
- ref_node = "Page%d" % index
- nodes[ref] = ref_node
- sql = "create (%s:Page {url:\"%s\",id:\"%d\",in:0,out:0})" % (ref_node, ref, index)
- index = index + 1
- session.run(sql)
-
-
- sql = "MATCH (out:Page {url:\"%s\"}), (in:Page {url:\"%s\"}) MERGE (out)-[:IN]->(in)" % (path, ref)
-
- session.run(sql)
-
-
- sql = "match (n:Page {url:\"%s\"}) SET n.out=n.out+1" % path
- session.run(sql)
-
-
- sql = "match (n:Page {url:\"%s\"}) SET n.in=n.in+1" % ref
- session.run(sql)
-
- finally:
- file_object.close()
- def main():
-
- driver = GraphDatabase.driver(
- "bolt://52.90.194.108:7687",
- auth=basic_auth("neo4j", "transmitters-amusements-saturdays"))
- session = driver.session()
-
- import_data(session)
-
- sql = "MATCH (n:Page) where (n.in=1 and n.out=0) or (n.in=1 and n.out=1) RETURN n.url"
- results = session.run(sql)
- for result in results:
- print("疑是webshell: %s" % result["n.url"])
-
- session.close()
- if __name__ == "__main__":
- main()
|