逻辑
文件路径
同目录下“.jianou”文件,多个文件会提示,默认使用第一个(后续考虑改为修改时间最新的)。
配置文件设计
为了便于前台操作(复选框),改为MarkDown语法的编辑页。
显示
运行结果
数据合法有效时
!数据合法有效时


主要不同点在于,如果字符串类型的Key没有对应个数的Value,会将Value全部设为空字符串。
代码
🐍Python🐍
# encoding = utf-8import osimport pathlibimport refrom addict import DictmsgStart = r"——————————Python脚本:Start——————————"strN = '\n'strMarkCheck = '- ['title2 = '\n## 'title3 = '### 'strAnchorEnd = "\"></a>"strBR = r"<br />"strNull = ""msgEnd = r"——————————Python脚本:End——————————"# 读取配置文件字符串def ReadMarkDownText():pathSelf = os.getcwd()print("当前工作目录:", pathSelf)listFile = list(pathlib.Path(pathSelf).glob('*.jianou'))nJianou = len(listFile)if nJianou == 0:print("无配置文件“*.jianou”")else:pathConf = listFile[0]if nJianou == 1:print("使用配置文件:", pathConf)else:print("有多个文件:", listFile)print("默认使用第一个:", pathConf)with open(pathConf, 'r+', encoding='utf-8') as f:textMarkDown = f.read()return textMarkDowndef ExtractConf(textMarkDown):listLevel2 = textMarkDown.split(title2)# print("2级标题列表:", listLevel2)listValueAll = []for strLevel2 in listLevel2:# print("2级标题字符串:", strLevel2)if title3 in strLevel2:strTypeLevel2 = strLevel2.split(strN)[0]# print("2级标题类型:", strTypeLevel2)listValue2Filtered = []listValue2 = re.split(r"\n|<br />", strLevel2)# print("2级标题的值:", listValue2)for iValue in listValue2:if strAnchorEnd in iValue:passelif iValue == strNull:# 输入输出目录可能为空passelif "//" in iValue:# 备注passelse:if strBR in iValue:iValue = iValue.strip(strBR)listValue2Filtered.append(iValue)# print("过滤后的Level2:", listValue2Filtered)listValueAll.append(listValue2Filtered)# print("3级标题列表:", listValueAll)listKey = []listValue = []dictConfAll = { }for iLevel2 in listValueAll:# print(iLevel2)strTypeLevel2 = iLevel2[0]listValueLevel2 = iLevel2[1:]listConfKey = []listConfValue = []for iValueLevel2 in listValueLevel2:if title3 in iValueLevel2:strLevel3 = iValueLevel2.split(title3)[1:][0].strip()# print(strLevel3)listConfKey.append(strLevel3)elif strMarkCheck in iValueLevel2:if '[x]' in iValueLevel2:listConfValue.append(True)elif '[ ]' in iValueLevel2:listConfValue.append(False)elif iValueLevel2 == strNull:# 如:文件夹listConfValue.append(iValueLevel2)else:if CaseStrIn(iValueLevel2, "True"):listConfValue.append(True)elif CaseStrIn(iValueLevel2, "False"):listConfValue.append(False)else:# 如:文件夹listConfValue.append(iValueLevel2)# print(listConfKey)# print(listConfValue)listKey.append(listConfKey)listValue.append(listConfValue)if len(listConfKey) != len(listConfValue):print("字典的Key列表和Value列表个数不一致:")print(listConfKey)listConfValue = []for i in range(len(listConfKey)):listConfValue.append(strNull)print("Value列表值被置为空字符串")zipConf = zip(listConfKey, listConfValue)# print(listConf)dictConf = dict(zipConf)# print(dictConf)dictConfAll.update(dictConf)# print(dictConfAll)return dictConfAlldef CaseStrIn(str2CMP, strBeCMP):if strBeCMP.lower() in str2CMP.lower():bCMP = Trueelse:bCMP = Falsereturn bCMPdef DictObj():textConf = ReadMarkDownText()dictConf = ExtractConf(textConf)objCof = Dict(dictConf)# print(objCof)return objCofif __name__ == '__main__':print(msgStart)objCof = DictObj()print(msgEnd)
📝MarkDown📝
有一些锚点,懒得删了,在代码里过滤了
# 配置文件**命名**:*.jianou<br />**格式**:以MarkDown文本格式<br />**路径**:exe同目录<br />**选项**:复选框以及大小写不敏感的True和False<a name="CP9TX"></a>## 显示:窗口消息<a name="xAWsM"></a>### 单个组织- [ ] <br /><a name="ASPlp"></a>### 全部组织- [ ] <br /><a name="MeEtg"></a>## 异常提醒<a name="Dm1HK"></a>### Table数据异常- [x] <br /><a name="Nn8vz"></a>### Code非常见类型- [x] <br /><a name="V2qh5"></a>### Code有异常符号- [x] <br /><a name="iFsK0"></a>## 导出文件<a name="b3NRZ"></a>### IoCs表格- [ ] <br /><a name="qBbRD"></a>### 完成后打开IoCs表格- [ ] <br /><a name="fLncF"></a>### 规则和代码- [x] <br /><a name="wCdf9"></a>### 原文链接- [x] <br /><a name="WRv52"></a>### 所有组织的IoCs数量统计- [x] <br /><a name="pM7jE"></a>### 所有需要提取IoCs的MarkDown文件路径支持大写的Bool值:TRUE<a name="xQn4E"></a>### 代码有异常符号- [x] 支持复选框<a name="igKf7"></a>### 不符合IoCs标题的文件也支持小写的Bool值:false<a name="Ks1xA"></a>## 文件夹<a name="fYnvY"></a>### 输入C:\Users\Administrator\Desktop<a name="wzTIn"></a>### 输出C:\Users\Administrator\Desktop\APT情报输出<br />// 缺省:exe同目录
参考
虽然没有采用,但是下次比较急的时候我应该会直接用现成的库和其对应的配置文件格式。这次自己构建自己的方式也是当练练Python语法。

