本小节示例有向图识别WebShell。
WebShell有很多访问特征,其中与有向图有关的是
(1)入度和出度均为0
(2)入度和出度均为1且自己指向自己
附上我对这段话的理解:一个网页文件的入度衡量的是访客是否从其他页面跳转到该页面,同理,一个网页文件的出度衡量的是访客是否会从该页面跳转到其他页面。正常网站页面会互相链接,因此会有一定的出入度,而Webshell通常与其他网站页面没有超链接,也就是一个孤立的页面,通常出入度为0。这一点通常可以在请求头中有无refer字段判断。
完整处理流程如下所示
注意,这里要写对数据库的密码
from neo4j import GraphDatabase, basic_auth
driver = GraphDatabase.driver("bolt://localhost:7687",auth=basic_auth("neo4j","7474"))
session = driver.session()
逐行读取数据,并生产节点以及关联关系
file_object = open('r-graph.txt', 'r')
try:
for line in file_object:
matchObj = re.match( r'(\S+) -> (\S+)', line, re.M|re.I)
if matchObj:
path = matchObj.group(1);
ref = matchObj.group(2);
if path in nodes.keys():
path_node = nodes[path]
else:
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)
finally:
file_object.close( )
四、查询数据
把入度和出度作为节点的属性,更新节点的出度入度属性。
if ref in nodes.keys():
ref_node = nodes[ref]
else:
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)
print (sql)
sql = "create (%s)-[:IN]->(%s)" %(path_node,ref_node)
session.run(sql)
print (sql)
sql = "match (n:Page {url:\"%s\"}) SET n.out=n.out+1" % path
session.run(sql)
print (sql)
sql = "match (n:Page {url:\"%s\"}) SET n.in=n.in+1" % ref
session.run(sql)
print (sql)
五、运行结果
create (Page1:Page {url:"http://180.76.190.79/wordpress/wp-admin/" , id:"1",in:0,out:0})
create (Page2:Page {url:"http://180.76.190.79/wordpress/wp-login.php",id:"2",in:0,out:0})
create (Page1)-[:IN]->(Page2)
match (n:Page {url:"http://180.76.190.79/wordpress/wp-admin/"}) SET n.out=n.out+1
match (n:Page {url:"http://180.76.190.79/wordpress/wp-login.php"}) SET n.in=n.in+1
网页关联的数据如下所示:
查询出入度=0或者出入度均为1但是指向自己的节点,满足条件的疑似webshell部分如下所示:
本文为互联网自动采集或经作者授权后发布,本文观点不代表立场,若侵权下架请联系我们删帖处理!文章出自:https://blog.csdn.net/mooyuan/article/details/122772207