无法使用的BAT脚本
很奇怪,我之前写的BAT脚本改了半天都不行:
BAT - 编译YARA规则的脚本
可能是因为我换电脑(且为Windows11)的原因?改Python重新写一遍💢💢💢
脚本逻辑
- 获取目录下所有的.yar文件
- 将*.yar中的内容保存到“AllYARAs.yar”
- yarac编译“AllYARAs.yar”
msgStart = r”——————————Python脚本开始——————————“ msgEnd = r”——————————Python脚本结束——————————“
strSpace = “ “ bNewLine = b”\r\n”
YARA不支持带空格的路径,所以另用桌面路径输出
pathExport = r”C:\Users\Administrator\Desktop” pathYar = r”D:\Software Files\Python Project\PyQT5\从MarkDown提取IoCs\Code\YARA Rule” pathAllYARAs2Compile = pathExport + “\AllYARAs.yar” pathAllYARAsPass = pathExport + “\AllYARAs.Pass”
def EnumAllYars2Compile(pathDir):
# print("需要遍历的路径:", pathDir)f2Compile = open(pathAllYARAs2Compile, 'wb+')listPathYar = []# 遍历.Yar文件for WindowsPath in Path(pathDir).rglob('*.yar'):with open(WindowsPath, 'rb') as fYar:strCode = fYar.read()# 加个换行符(byte)f2Compile.write(strCode + bNewLine)fileYar = str(WindowsPath)listPathYar.append(fileYar)f2Compile.close()print("文件已保存:" + pathAllYARAs2Compile)
print(““.yar”文件列表:”)
print(listPathYar)
def CompileYar(pathAllYARAsYar):
# yarac -w [需要编译的文件] [编译后输出的文件]cmdCompile = "yarac -w " + pathAllYARAsYar + strSpace + pathAllYARAsPass# print("编译命令:", cmdCompile)os.system(cmdCompile)
if name == ‘main‘: print(msgStart)
if os.path.exists(pathAllYARAs2Compile):# print("删除上次存在的总文件:" + pathAllYARAs2Compile)os.remove(pathAllYARAs2Compile)# 遍历目标文件夹中的.yar文件保存为单个.yar文件EnumAllYars2Compile(pathYar)# 编译拥有所有Yar代码的.yar文件CompileYar(pathAllYARAs2Compile)
print(msgEnd) ```
