开发思路
分类
分成不同的类别。工控、木马、漏洞。每个类别下面,配套相应的模板。
txt
文档有固定的模板,和文字内容。需要替换一些特定的部分,比如sid,比如cve,比如msg。将这些需要替换的部分,写入变量,python运行的时候,可以在终端输入要填入的变量部分。然后写回txt。手动写回部分,可以适用于那些需要查资料才能写出来的东西。
rules
python也需要一个自动化填写的部分,依然是在txt里面设置变量。但是python也需要读取rule文件,将msg、sid、cve自动匹配读取出来,填入txt的相应变量中。这些过程自动实现,读取rule,写入txt。
最终思路
手动写回功能
适用于那些需要查资料的部分。这部分的py,最开始需要一个输出,读取rule文件,将msg、cve打印出来。
需要输入,按顺序来提示相应的txt变量部分。查资料后,手动将这部分内容输入。需要手动输入的内容有:msg的翻译,漏洞导致的结果、CVE内容、造成的影响(和msg翻译内容一样,所以采用一个变量,统一填入)、影响的系统、漏洞类型、攻击链(对于漏洞模板来说,等级、攻击链这些变动的不多,所以可以适当减少变量。根据模板不同来调整)、目标设备(和影响的系统一致,采用一个变量,统一填入)
自动写入功能
适用于可以从rule中直接读取,然后填入txt中的内容。py将会自动化实现。需要自动匹配的内容有:sid、CVE编号。读入rule,写进txt。
修改文件名
文件最终需要修改为sid.txt
自动生成language.txt
将获取到的手动输入的自动翻译,生成一个翻译文档
实现过程
难点一:解决txt中变量定义的问题
在固定位置设置特殊字符__SID__
来占位。之后用具体的数据来替换掉这个占位符,就可以起到一个变量的作用
难点二:一个rule文件有多条rule,怎么能准确匹配到需要的那个rule
在每个py功能执行前,都需要输入sid。在读取文本时,会先根据sid,再读特定的rule。去匹配。但是这个功能不好实现。暂时先不实现。手动将所有的rule都提取出来,变成一个单独的文本。
手动替换文档里的rule.txt。每次一条
难点三:获取文本中特定的内容
查找位置(返回的是查找内容所在数组位置)https://blog.csdn.net/weixin_43718786/article/details/114102454
分割(找到之后,如何将需要的内容提取出来) https://blog.csdn.net/weixin_43718786/article/details/114102454
输出特定位置的字符:https://blog.csdn.net/qq_51574759/article/details/116807925
难点四:更复杂的匹配
匹配想要内容的时候,可以用正则匹配【待定】
代码参考
已同步至GitHub,文档内容涉密,所以将文档部分改动。可以参考代码思想。
https://github.com/Gryffinbit/AutoDoc/tree/main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| import codecs import re import os, sys from shutil import copyfile
def sidReturn(): return sid
def cveReturn(): return cve
class Auto: def __init__(self): self.contents_rule = None self.contents_tmpl = None
def init(self): self.read() self.replace() self.test() sidReturn() cveReturn()
def read(self): with open('/Users/gryffinbit/PycharmProjects/AutoDoc/工控模板/rule.txt', 'r', encoding='gb2312') as f0: contents_rule = f0.read() self.contents_rule = contents_rule
with open("/Users/gryffinbit/PycharmProjects/AutoDoc/工控模板/Template_ICS.txt", 'r', encoding='gb2312') as f1: contents_tmpl = f1.read() self.contents_tmpl = contents_tmpl
def replace(self): sid_loc_rule = self.contents_rule.rfind('sid:') sid_row_rule = self.contents_rule[int(sid_loc_rule) + 4:] cve_loc_rule = self.contents_rule.rfind('cve') cve_row_rule = self.contents_rule[int(cve_loc_rule) + 4:] global sid global cve sid = sid_row_rule.split(";")[0] cve = str("CVE-") + cve_row_rule.split(";")[0] self.contents_tmpl = self.contents_tmpl.replace("__SID__", str(sid)) self.contents_tmpl = self.contents_tmpl.replace("__CVE__", str(cve))
with open('Template_ICS.txt', 'w', encoding='gb2312') as f2: f2.write(self.contents_tmpl)
def test(self): print("自动化替换成功\n-------------------")
class Interact: def __init__(self): self.msg_ch = None self.contents_tmpl_interact = None self.contents_rule_interact = None
def init(self): self.read() self.display() self.write() self.translate()
def read(self): with open('/Users/gryffinbit/PycharmProjects/AutoDoc/工控模板/rule.txt', 'r', encoding='gb2312') as f3: contents_rule_interact = f3.read() self.contents_rule_interact = contents_rule_interact
with open('Template_ICS.txt', 'r', encoding='gb2312') as f4: contents_tmpl_interact = f4.read() self.contents_tmpl_interact = contents_tmpl_interact
def display(self): print(cve) msg_loc_rule = self.contents_rule_interact.rfind('msg:') msg_row_rule = self.contents_rule_interact[int(msg_loc_rule) + 4:] msg = msg_row_rule.split(";")[0] print(msg)
def write(self): """ msg翻译内容 """ print("输入msg翻译") msg_ch = input() self.msg_ch = msg_ch self.contents_tmpl_interact = self.contents_tmpl_interact.replace("__MSG__", str(msg_ch)) with open('Template_ICS.txt', 'w', encoding='gb2312') as f5: f5.write(self.contents_tmpl_interact)
""" 漏洞造成的影响 """ print("输入漏洞造成的影响") info = input() self.contents_tmpl_interact = self.contents_tmpl_interact.replace("__info__", str(info)) with open('Template_ICS.txt', 'w', encoding='gb2312') as f5: f5.write(self.contents_tmpl_interact)
""" CVE内容 """ print("输入CVE的内容") cve_info = input() self.contents_tmpl_interact = self.contents_tmpl_interact.replace("__CVEINFO__", str(cve_info)) with open('Template_ICS.txt', 'w', encoding='gb2312') as f5: f5.write(self.contents_tmpl_interact)
""" 影响的系统 """ print("输入影响的系统") affect_s = input() self.contents_tmpl_interact = self.contents_tmpl_interact.replace("__AS__", str(affect_s)) with open('Template_ICS.txt', 'w', encoding='gb2312') as f5: f5.write(self.contents_tmpl_interact)
""" 漏洞类型(其他漏洞利用、信息泄露漏洞利用等)""" print("输入漏洞类型") cve_type = input() self.contents_tmpl_interact = self.contents_tmpl_interact.replace("__Type__", str(cve_type)) with open('Template_ICS.txt', 'w', encoding='gb2312') as f5: f5.write(self.contents_tmpl_interact)
""" 所属攻击链(可属于不同阶段、执行等)""" print("输入所属攻击链") kill_chain = input() self.contents_tmpl_interact = self.contents_tmpl_interact.replace("__KCType__", str(kill_chain)) with open('Template_ICS.txt', 'w', encoding='gb2312') as f5: f5.write(self.contents_tmpl_interact)
def translate(self): f = open("/Users/gryffinbit/PycharmProjects/AutoDoc/输出文档/ips_language.txt", "a", encoding="gb2312") f.write(str(sid) + ';' + '检测到' + self.msg_ch + '(' + str(cveReturn()) + ')') f.write('\n') f.close()
if __name__ == '__main__': Auto().init() Interact().init() name = sidReturn() + ".txt" os.rename("Template_ICS.txt", "/Users/gryffinbit/PycharmProjects/AutoDoc/输出文档/ipsdocs/" + name)
|