python解釋NTFS runlist的代碼

代碼如下:

#!/usr/bin/python3
#http://www.frombyte.com 張宇
import os
import sys
import random
import hashlib
import struct
import zlib
import re
 
def help_exit():
    print("  命令格式:")
    print("  python3 %s <File name> <Start bytes> <Start LCN> <Start VCN>:" % sys.argv[0])
    print("      File name:要解釋的包含runlist的文件名稱")
    print("      Start bytes:文件中要解釋runlist的起始位置")
    print("      Start LCN:runlist開始的參考LCN值,如果是一段完整的runlist,這個值應爲0.")
    print("      Start VCN:runlist開始的參考VCN值,如果是一段沒有0x20的runlist,這個值多數爲0.")
    print("      *返回值:一個二維隊列,打印結果。\n")
    exit()
#通過拋出異常判斷第一個參數是否是A-F
def is_num_by_except(s):
    try:
        a=int(s,16)
        if (a>0 and a<=16): return True
        else: return False
    except ValueError:
        return False
    
if len(sys.argv)!= 5 :
    print("  ***參數數量或格式錯誤!")
    help_exit()

if sys.argv[2].isdigit():
    spoi = int(sys.argv[2])
    if spoi<0:
      print("***錯誤,起始字節位置不能取負值")
      help_exit()
else:
    print("***錯誤,起始字節位置應爲非負整數")
    help_exit()

if sys.argv[3].isdigit():
    slcn = int(sys.argv[3])
    if slcn<0:
      print("***錯誤,起始LCN不能取負值")
      help_exit()
else:
    print("***錯誤,起始LCN應爲非負整數")
    help_exit()

if sys.argv[4].isdigit():
    svcn = int(sys.argv[4])
    if svcn<0:
      print("***錯誤,起始VCN不能取負值")
      help_exit()
else:
    print("***錯誤,起始VCN應爲非負整數")
    help_exit()

def get_i(vl,ilen):
    q=0
    for i in range(0,ilen):
        q = q | ( vl[0][i] << i*8 )
    
    #若爲負數
    if vl[0][ilen-1] > 0x80:
        q = q - (1 << ilen*8 )
    return q

f = open("%s"%sys.argv[1],'rb')
f.seek(spoi)
data = f.read(1024)
v1 = 1
i = 0
lists = [[0 for i in range(2)] ]
del lists[0]

while True:
  t = struct.unpack_from('B',data,i)
  v1 = t[0]
  if v1 == 0:
    break

  v1_p = (v1 & 0xF0) >> 4
  v1_l = (v1 &0xF)
  if (v1_l >=8) or (v1_p >=8) or (v1_l == 0):
    print("***偏移%d:run list長度和位置字節有錯誤!***"%(i+spoi))
    break

  i = i+1
  if (i+8) >= 1024:
    break
  t = struct.unpack_from('8s',data,i)
  v1_dl = get_i(t,v1_l)
  if v1_dl < 0:
    print("***偏移%d:run片斷長度不能爲負!***"%(i+spoi))
    break 
  
  i = i+ v1_l
  if (i+8) >= 1024:
    break
  t = struct.unpack_from('8s',data,i)
  v1_dp = get_i(t,v1_p)
  slcn = slcn + v1_dp
  lists.append([slcn,v1_dl])

  i = i + v1_p
  #print("%x,%x:%x,%x"%(v1_l,v1_p,v1_dl,slcn))

print("Runlist(共%d個片斷):"%len(lists))
print("%20s%20s%20s"%("VCN","LCN","LEN"))
for i in lists:
  print("%20d%20d%20d"%(svcn,i[0],i[1]))
  svcn += i[1]

f.close()


執行效果如下:

root@zhangyu-VirtualBox:~/NTFS-5# python3 read_runlist.py mft_source.img

  ***參數數量或格式錯誤!

  命令格式:

  python3 read_runlist.py <File name> <Start bytes> <Start LCN> <Start VCN>:

      File name:要解釋的包含runlist的文件名稱

      Start bytes:文件中要解釋runlist的起始位置

      Start LCN:runlist開始的參考LCN值,如果是一段完整的runlist,這個值應爲0.

      Start VCN:runlist開始的參考VCN值,如果是一段沒有0x20的runlist,這個值多數爲0.

      *返回值:一個二維隊列,打印結果。


root@zhangyu-VirtualBox:~/NTFS-5# python3 read_runlist.py mft_source.img 5688 0 0

Runlist(共18個片斷):

                 VCN                 LCN                 LEN

                   0               32212                   1

                   1              157952                   2

                   3              207115                   3

                   6              244046                   3

                   9              122523                   1

                  10              157991                   1

                  11              170296                   3

                  14               40552                   5

                  19              149853                   2

                  21              122721                   2

                  23              141674                   1

                  24              145783                   3

                  27              158109                   3

                  30              145820                   1

                  31              240236                   1

                  32              154081                   1

                  33              166379                   3

                  36              178711                   3


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章