Skip to content

Commit

Permalink
code_optimization_action
Browse files Browse the repository at this point in the history
  • Loading branch information
grayddq authored and grayddq committed May 9, 2019
1 parent d00c344 commit b4e343d
Show file tree
Hide file tree
Showing 17 changed files with 666 additions and 324 deletions.
118 changes: 98 additions & 20 deletions lib/core/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding:utf-8
from __future__ import print_function
import os, sys, json, re, time
import os, sys, json, re, time, pwd
from imp import reload
from lib.core.ip.ip import *
from lib.core.globalvar import *
Expand All @@ -23,7 +23,8 @@
malware_infos = []


# 颜色打印
# 颜色打印前端,根据特征赋予字符不同的颜色
# 用于用户端视觉效果的打印。
def pringf(strings, security=False, suspicious=False, malice=False):
if security:
# 安全显示绿色
Expand All @@ -40,7 +41,8 @@ def pringf(strings, security=False, suspicious=False, malice=False):
file_write((u'%s ' % strings) + ' ]\n')


# 获取字符串宽度
# 获取字符串宽度,包含汉语、字符、数字等
# 返回:字符串长度大小
def get_str_width(string):
widths = [
(126, 1), (159, 0), (687, 1), (710, 0), (711, 1),
Expand Down Expand Up @@ -72,7 +74,8 @@ def get_str_width(string):
return width


# 对齐字符串,返回对齐后字符串
# 对齐字符串,用于用户视觉上的打印
# 返回:对其后字符串
def align(string, width=40):
width = 40
string_width = get_str_width(string)
Expand All @@ -88,7 +91,8 @@ def string_output(string):
file_write(align(string, 30) + u'[ ')


# 数组去重
# 数组去重功能
# 返回:去重后数组
def reRepeat(old):
new_li = []
for i in old:
Expand All @@ -97,11 +101,75 @@ def reRepeat(old):
return new_li


# 获取文件的最近的改动时间
# 返回:文件更改时间戳
def get_file_attribute(file):
try:
# 文件最近修改时间
ctime = os.stat(file).st_mtime
cctime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ctime))
# 文件所属者uid
uid = os.stat(file).st_uid
username = pwd.getpwuid(uid).pw_name
return cctime, username
except:
return "", ""


# 获取进程的开始时间
# 返回:进程开始时间
def get_process_start_time(pid):
try:
pro_info = os.popen("ps -eo pid,user,lstart 2>/dev/null| grep -v 'grep'|grep " + pid).read().splitlines()
for infos in pro_info:
info = infos.strip()
if pid == info.split(' ')[0].strip():
user = info.split(' ', 2)[1].strip()
stime = info.split(' ', 2)[2].strip()
sstime = os.popen("date -d " + stime + " '+%Y-%m-%d %H:%M:%S' 2>/dev/null").read().splitlines()
return user, sstime[0]
return "",""
except:
return "",""


# 检测风险结果,进行全局变量结果录入
# 每个风险详情包含几项
# 1、风险检测大项 checkname
# 2、风险名称 vulname
# 3、异常文件 file
# 4、异常进程 pid
# 4、所属用户 user
# 4、异常信息 info
# 6、异常时间 mtime
# 7、风险等级 level 存在风险-可疑
# 7、建议手工确认步骤 consult
# 返回:检测项恶意信息数组
def malice_result(checkname, vulname, file, pid, info, consult, level, mtime='', user=''):
mtime_temp, user_temp = '', ''
if file:
mtime_temp, user_temp = get_file_attribute(file)
if pid:
mtime_temp, user_temp = get_process_start_time(pid)
if not mtime: mtime = mtime_temp
if not user: user = user_temp
malice_info = {u'检测项': checkname, u'风险名称': vulname, u'异常文件': file, u'进程PID': pid, u'异常时间': mtime, u'所属用户': user,
u'异常信息': info, u'手工排查确认': consult, u'风险级别': level}
result_info = get_value('RESULT_INFO')
result_info.append(malice_info)
set_value('RESULT_INFO', result_info)


# 结果内容输出到文件
def result_output_file(tag, result):
def result_output_file(tag):
DEBUG = get_value('DEBUG')
if len(result) > 0:
new = reRepeat(result)
RESULT_INFO = get_value('RESULT_INFO')
info = []
for result in RESULT_INFO:
if result[u'检测项'] == tag:
info.append(result)
if len(info) > 0:
new = reRepeat(info)
file_write('-' * 30 + '\n')
file_write(tag + '\n')
if DEBUG: print(tag)
Expand All @@ -111,6 +179,7 @@ def result_output_file(tag, result):
if DEBUG: print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))


# 分析结果输出,用于用户视觉效果
def result_output_tag(suspicious=False, malice=False, skip=False):
if malice:
pringf(u'存在风险', malice=True)
Expand Down Expand Up @@ -251,14 +320,24 @@ def check_ip(ip):
return False


# 分析字符是否包含反弹shell特征、境外ip类信息
# 分析一串字符串是否包含反弹shell、获取对应字串内可能存在的文件,并判断文件是否存在恶意特征。
# 匹配成功则返回恶意特征信息
# 否则返回空
def analysis_strings(strings):
def analysis_strings(contents):
try:
mal = check_shell(strings)
if mal: return mal
if check_contents_ip(strings): return strings
content = contents.replace('\n', '')
# 反弹shell类
if check_shell(content):
return u"反弹shell类:%s" % content
# 境外IP操作类
elif check_contents_ip(content):
return u"境外ip操作类:%s" % content
else:
for file in content.split(' '):
if not os.path.exists(file): continue
if os.path.isdir(file): continue
malware = analysis_file(file)
if malware: return u"引用恶意文件%s,可疑内容:%s" % (file, malware)
return ""
except:
return ""
Expand All @@ -267,7 +346,7 @@ def analysis_strings(strings):
# 分析文件是否包含恶意特征、反弹shell特征、境外ip类信息
# 存在返回恶意特征
# 不存在返回空
def analysis_file(file):
def analysis_file(file, mode='fast'):
try:
SCAN_TYPE = get_value('SCAN_TYPE')
DEBUG = get_value('DEBUG')
Expand All @@ -283,21 +362,20 @@ def analysis_file(file):

time.sleep(0.01)
for str in strings:
mal = check_shell(str)
if mal:
if DEBUG: print(u'文件:%s ,bash shell :%s' % file, mal)
return mal
if check_shell(str):
if DEBUG: print(u'文件:%s ,bash shell :%s' % file, str)
return u"反弹shell类:%s" % str
# 完全扫描会带入恶意特征扫描
if SCAN_TYPE == 2:
time.sleep(0.01)
for malware in malware_infos:
if malware.replace('\n', '') in str:
if DEBUG: print(u'文件:%s ,恶意特征 :%s' % file, malware)
return malware
return u"恶意特征类:%s,匹配规则:%s" % (str, malware)
if Overseas: continue
if check_contents_ip(str):
if DEBUG: print(u'文件:%s ,境外IP操作类 :%s' % file, str)
return str
return u"境外ip操作类:%s" % str
return ""
except:
return ""
61 changes: 61 additions & 0 deletions lib/core/data_aggregation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# coding:utf-8
from __future__ import print_function
import os, time, sys, json, re, operator, datetime
from lib.core.common import *


# 作者:咚咚呛
# 功能:根据已知的异常风险,进行信息聚合,根据时间线排序,获取黑客的行动轨迹

class Data_Aggregation:
def __init__(self):
self.result_infos = []

def cmp_datetime(self, a, b):
try:
a_datetime = datetime.datetime.strptime(a, '%Y-%m-%d %H:%M:%S')
b_datetime = datetime.datetime.strptime(b, '%Y-%m-%d %H:%M:%S')

if a_datetime > b_datetime:
return 1
elif a_datetime < b_datetime:
return -1
else:
return 0
except:
return 1

def run(self):
self.result_infos = get_value('RESULT_INFO')
print(u'-' * 30)
say_info = u'根据系统分析的情况,溯源后的行动轨迹为:'
print(u'\033[1;31m根据系统分析的情况,溯源后的行动轨迹为:\033[0m')
self.result_infos.sort(cmp=self.cmp_datetime, key=operator.itemgetter(u'异常时间'))
i = 1
for result_info in self.result_infos:
if result_info[u'检测项'] == u'常规后门检测':
print(u"[%d] 黑客在%s时间,进行了%s植入" % (i, result_info[u'异常时间'], result_info[u'风险名称']))
if result_info[u'检测项'] == u'配置类安全检测':
print(u"[%d] 黑客在%s时间,进行了%s变更,%s" % (
i, result_info[u'异常时间'], result_info[u'风险名称'], result_info[u'异常信息']))
if result_info[u'检测项'] == u'文件类安全检测':
print(u"[%d] 黑客在%s时间,植入了恶意文件%s,%s" % (
i, result_info[u'异常时间'], result_info[u'异常文件'], result_info[u'异常信息']))
if result_info[u'检测项'] == u'主机历史操作类安全检测':
print(u"[%d] 黑客在%s时间,进行了恶意操作,%s" % (i, result_info[u'异常时间'], result_info[u'异常信息']))
if result_info[u'检测项'] == u'日志类安全检测':
print(u"[%d] 黑客在%s时间,进行了主机登陆,%s" % (i, result_info[u'异常时间'], result_info[u'异常信息']))
if result_info[u'检测项'] == u'网络链接类安全检测':
print(u"[%d] 黑客在%s时间,%s" % (i, result_info[u'异常时间'],result_info[u'异常信息']))
if result_info[u'检测项'] == u'进程类安全检测':
print(
u"[%d] 黑客在%s时间,启动进程%s,%s" % (i, result_info[u'异常时间'], result_info[u'进程PID'], result_info[u'异常信息']))
if result_info[u'检测项'] == u'Rootkit类安全检测':
print(u"[%d] 黑客在%s时间,植入Rootkit后门,%s" % (i, result_info[u'异常时间'], result_info[u'异常信息']))
if result_info[u'检测项'] == u'系统初始化检测':
print(u"[%d] 黑客在%s时间,设置了系统命令别名,%s" % (i, result_info[u'异常时间'], result_info[u'异常信息']))
if result_info[u'检测项'] == u'账户类安全检测':
print(u"[%d] 黑客在%s时间,进行了账户修改设置,%s" % (i, result_info[u'异常时间'], result_info[u'异常信息']))
if result_info[u'检测项'] == u'Webshell安全检测':
print(u"[%d] 黑客在%s时间,植入了webshell文件%s" % (i, result_info[u'异常时间'], result_info[u'异常文件']))
i += 1
8 changes: 5 additions & 3 deletions lib/core/option.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from lib.plugins.Webshell_Analysis import *
from lib.plugins.Sys_Init import *
from lib.plugins.Search_File import *
from lib.core.data_aggregation import *


def main(path):
Expand All @@ -38,8 +39,6 @@ def main(path):

options, _ = parser.parse_args()

options.time = '2019-05-07 12:00:00~2019-05-07 17:00:00'

# 初始化全局模块
init()
# 设置调试模式
Expand All @@ -50,7 +49,7 @@ def main(path):
set_value('SCAN_TYPE', 2 if options.full_scan else 1)
set_value('SYS_PATH', path)
set_value('LOG_PATH', path + "/log/gscan.log")

set_value('RESULT_INFO', [])

if options.logdir:
print(u'\033[1;32m开始备份整个系统安全日志...\033[0m\n')
Expand Down Expand Up @@ -96,6 +95,9 @@ def main(path):
Webshell_Analysis().run()
# 漏洞扫描

#路径追溯
Data_Aggregation().run()

# 输出报告
print(u'-' * 30)
print(u'\033[1;32m扫描完毕,扫描结果已记入到 %s 文件中,请及时查看\033[0m' % get_value('LOG_PATH'))
Loading

0 comments on commit b4e343d

Please sign in to comment.