搜索
查看: 475|回复: 0

WhatFormat.py【某类ctf隐藏信息发掘神器】

[复制链接]

1839

主题

2255

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11913
发表于 2014-10-18 20:00:20 | 显示全部楼层 |阅读模式
   近几年各种ctf赛事频发,让人应接不暇!
                其中有一类型题目叫做“隐写术”,
                “隐写术”中的一种题目类型是将两种或两种以上格式的文件“拼接”在一起
                让参赛者寻找隐藏在正常文件里的其他文件,并找到flag。
                该类题目解题思路虽然简单,但是有个问题就是得记住常见的“文件头”,
                又或假如题目是不常见的文件头呢?
                宝贵的大脑怎么能拿来记这种毫无意义的东西呢?
                于是,写了这个“文件类型”识别脚本,自动识别隐藏文件并“提取”出来。

                该脚本基于“字典”模式设计,依赖默认的字典格式文件,方便新格式(后缀)的增加和误报率过高格式的删除。
                字典格式为extension  ::  description ::  hexdump(后缀 :: 描述 :: hex文件头)
                支持“#”注释 ,hexdump可使用空格间隔(便于肉眼识别)
                默认字典如下:




            更多格式hexdump请到http://file-extension.net/seeker/搜索,或者自行添加


识别效果如下








核心源码如下


  1. #coding=utf-8

  2. '''
  3. WhatFormat.py
  4. '''

  5. import os
  6. import sys
  7. import binascii

  8. #dict = r'C:\MyTools\whatFormat.dic'
  9. dict = 'whatFormat.dic'

  10. def usage():
  11.     data = '''
  12. [+] This script help you to find outthe real format of the file or hide data from the file!
  13. [+] the result file save at 'output' dir, go and search it!
  14. [+] [url=http://hi.baidu.com/l34rn]http://hi.baidu.com/l34rn[/url]
  15. [+] cnh4ckff [at] gmail.com

  16. [+] usage: %s <target file>
  17.     '''% sys.argv[0].split('\\')[-1]
  18.     print data
  19.      
  20. def loadDict(dict):
  21.     dictList = []
  22.     with open(dict,'r') as lines:
  23.         for line in lines:
  24.             if line.strip() != '':
  25.                 if not line.startswith('#'):
  26.                     ext,des,hexDump = line.split('::')
  27.                     dictList.append([ext,des,hexDump])
  28.     return dictList
  29.      
  30. def loadFile(file):
  31.     size = os.path.getsize(file)
  32.     print '''
  33. [+] File:               %s
  34. [+] Size:               %s [Kb]
  35.     '''%(file,str(size/1024))
  36.     with open(file,'rb') as f:
  37.         data = f.read()
  38.         hexData = binascii.hexlify(data)
  39.     return hexData
  40.      
  41. def checkFormat(hexData,dictList):
  42.     resList = []
  43.     for dict in dictList:
  44.         starup = 0
  45.         hexDump = ''
  46.         for hexDumpTmp in dict[2].strip():
  47.             hexDumpTmp = hexDumpTmp.strip()
  48.             if hexDumpTmp != '':
  49.                 hexDump += hexDumpTmp.lower()
  50.         while True:
  51.             code = hexData.find(hexDump,starup)
  52.             if code != -1:
  53.                 starup = code+1
  54.                 resList.append([dict[0].strip(),dict[1].strip(),code])
  55.             else:
  56.                 break
  57.     return resList
  58.      
  59. def output(resList,hexData):
  60.     i = 0
  61.     for res in resList:
  62.         i += 1
  63.         num = str(i)
  64.         ext = res[0]
  65.         des = res[1]
  66.         startup = int(res[2])
  67.         fileName = num+'.'+ext
  68.         data = binascii.unhexlify(hexData[startup:])
  69.         saveFile(fileName,data)
  70.         print '''
  71. [+] Number:             %s
  72. [+] Extension:          %s
  73. [+] Description:        %s
  74. [+] Startup:            %s
  75. [+] Saveas:             %s
  76.         '''%(num ,ext,des,startup,fileName)
  77.          
  78. def saveFile(fileName,data):
  79.     if not os.path.exists('output'):
  80.         os.mkdir('output')
  81.     with open('output/'+fileName,'wb') as f:
  82.         f.write(data)

  83. def main():
  84.     if len(sys.argv) < 2:
  85.         usage()
  86.         exit()
  87.     file = sys.argv[1]
  88.     hexData = loadFile(file)
  89.     dictList = loadDict(dict)
  90.     resList = checkFormat(hexData, dictList)
  91.     output(resList,hexData)
  92.      
  93.      
  94. if __name__ == '__main__':
  95.     try:
  96.         main()   
  97.     except Exception,e:
  98.         print '[+] ',e
复制代码

源码及字典、测试文件打包


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?Join BUC

x
过段时间可能会取消签到功能了
您需要登录后才可以回帖 登录 | Join BUC

本版积分规则

Powered by Discuz!

© 2012-2015 Baiker Union of China.

快速回复 返回顶部 返回列表