Skip to content

Commit 7b45ef6

Browse files
committed
Applying changes. Blocks -Begin .. End- deleted
1 parent 8473aaf commit 7b45ef6

File tree

1 file changed

+75
-87
lines changed

1 file changed

+75
-87
lines changed

modules/post/windows/manage/sdel.rb

Lines changed: 75 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -41,130 +41,118 @@ def initialize(info={})
4141

4242

4343
def run
44-
begin
45-
type = 1
46-
file = datastore['FILE']
47-
48-
if datastore['ZERO']==true
49-
type = 0
50-
print_status("The file will be overwritten with null bytes")
51-
end
52-
53-
if !file_exist?(file)
54-
print_error("File #{file} does not exist")
55-
return
56-
elsif comp_encr(file)
57-
print_status("File compress or encrypted. Content could not be overwritten")
58-
end
59-
file_overwrite(file,type)
44+
type = 1
45+
file = datastore['FILE']
46+
47+
if datastore['ZERO']==true
48+
type = 0
49+
print_status("The file will be overwritten with null bytes")
50+
end
51+
52+
if !file_exist?(file)
53+
print_error("File #{file} does not exist")
54+
return
55+
elsif comp_encr(file)
56+
print_status("File compress or encrypted. Content could not be overwritten")
6057
end
58+
file_overwrite(file,type)
6159
end
6260

6361

6462
#Function to calculate the size of the cluster
6563
def size_cluster()
66-
begin
67-
drive = expand_path("%SystemDrive%")
68-
r = client.railgun.kernel32.GetDiskFreeSpaceA(drive,4,4,4,4)
69-
cluster = r["lpBytesPerSector"] * r["lpSectorsPerCluster"]
70-
print_status("Cluster Size: #{cluster}")
64+
drive = expand_path("%SystemDrive%")
65+
r = client.railgun.kernel32.GetDiskFreeSpaceA(drive,4,4,4,4)
66+
cluster = r["lpBytesPerSector"] * r["lpSectorsPerCluster"]
67+
print_status("Cluster Size: #{cluster}")
7168

72-
return cluster
73-
end
69+
return cluster
7470
end
7571

7672

7773
#Function to calculate the real file size on disk (file size + slack space)
7874
def size_on_disk(file)
79-
begin
80-
size_file = client.fs.file.stat(file).size;
81-
print_status("Size of the file: #{size_file}")
75+
size_file = client.fs.file.stat(file).size;
76+
print_status("Size of the file: #{size_file}")
8277

83-
if (size_file<800)
84-
print_status("The file is too small. If it's store in the MTF (NTFS) sdel will not overwrite it")
85-
end
86-
87-
sizeC= size_cluster()
88-
size_ = size_file.divmod(sizeC)
78+
if (size_file<800)
79+
print_status("The file is too small. If it's store in the MTF (NTFS) sdel will not overwrite it")
80+
end
8981

90-
if size_.last != 0
91-
real_size = (size_.first * sizeC) + sizeC
92-
else
93-
real_size = size_.first * sizeC
94-
end
82+
sizeC= size_cluster()
83+
size_ = size_file.divmod(sizeC)
9584

96-
print_status("Size on disk: #{real_size}")
97-
return real_size
85+
if size_.last != 0
86+
real_size = (size_.first * sizeC) + sizeC
87+
else
88+
real_size = size_.first * sizeC
9889
end
90+
91+
print_status("Size on disk: #{real_size}")
92+
return real_size
9993
end
10094

10195

10296
#Change MACE attributes. Get a fake date by subtracting N days from the current date
10397
def change_mace(file)
104-
begin
105-
rsec= Rex::Text.rand_text_numeric(7,bad='012')
106-
date = Time.now - rsec.to_i
107-
print_status("Changing MACE attributes")
108-
client.priv.fs.set_file_mace(file, date,date,date,date)
109-
end
98+
rsec= Rex::Text.rand_text_numeric(7,bad='012')
99+
date = Time.now - rsec.to_i
100+
print_status("Changing MACE attributes")
101+
client.priv.fs.set_file_mace(file, date,date,date,date)
110102
end
111103

112104

113105
#Function to overwrite the file
114106
def file_overwrite(file,type)
115-
begin
116-
#FILE_FLAG_WRITE_THROUGH: Write operations will go directly to disk
117-
r = client.railgun.kernel32.CreateFileA(file, "GENERIC_WRITE", "FILE_SHARE_READ|FILE_SHARE_WRITE", nil, "OPEN_EXISTING", "FILE_FLAG_WRITE_THROUGH", 0)
118-
handle=r['return']
119-
real_size=size_on_disk(file)
120-
121-
#http://msdn.microsoft.com/en-us/library/windows/desktop/aa365541(v=vs.85).aspx
122-
client.railgun.kernel32.SetFilePointer(handle,0,nil,"FILE_BEGIN")
123-
124-
if type==0
125-
random="\0"*real_size
126-
else
127-
random=Rex::Text.rand_text(real_size,nil)
128-
end
107+
#FILE_FLAG_WRITE_THROUGH: Write operations will go directly to disk
108+
r = client.railgun.kernel32.CreateFileA(file, "GENERIC_WRITE", "FILE_SHARE_READ|FILE_SHARE_WRITE", nil, "OPEN_EXISTING", "FILE_FLAG_WRITE_THROUGH", 0)
109+
handle=r['return']
110+
real_size=size_on_disk(file)
111+
112+
#http://msdn.microsoft.com/en-us/library/windows/desktop/aa365541(v=vs.85).aspx
113+
client.railgun.kernel32.SetFilePointer(handle,0,nil,"FILE_BEGIN")
114+
115+
if type==0
116+
random="\0"*real_size
117+
else
118+
random=Rex::Text.rand_text(real_size,nil)
119+
end
129120

130-
#http://msdn.microsoft.com/en-us/library/windows/desktop/aa365747(v=vs.85).aspx
131-
w=client.railgun.kernel32.WriteFile(handle,random,real_size,4,nil)
121+
#http://msdn.microsoft.com/en-us/library/windows/desktop/aa365747(v=vs.85).aspx
122+
w=client.railgun.kernel32.WriteFile(handle,random,real_size,4,nil)
132123

133-
if w['return']==false
134-
print_error("The was an error writing to disk, check permissions")
135-
return
136-
end
124+
if w['return']==false
125+
print_error("The was an error writing to disk, check permissions")
126+
return
127+
end
137128

138-
print_status("#{w['lpNumberOfBytesWritten']} bytes overwritten")
139-
client.railgun.kernel32.CloseHandle(handle)
129+
print_status("#{w['lpNumberOfBytesWritten']} bytes overwritten")
130+
client.railgun.kernel32.CloseHandle(handle)
140131

141-
change_mace(file)
132+
change_mace(file)
142133

143-
#Generate a long random file name before delete it
144-
newname = Rex::Text.rand_text_alpha(200,nil)
145-
print_status("Changing file name")
134+
#Generate a long random file name before delete it
135+
newname = Rex::Text.rand_text_alpha(200,nil)
136+
print_status("Changing file name")
146137

147-
#http://msdn.microsoft.com/en-us/library/windows/desktop/aa365239(v=vs.85).aspx
148-
client.railgun.kernel32.MoveFileA(file,newname)
138+
#http://msdn.microsoft.com/en-us/library/windows/desktop/aa365239(v=vs.85).aspx
139+
client.railgun.kernel32.MoveFileA(file,newname)
149140

150-
file_rm(newname)
151-
print_good("File erased!")
152-
end
141+
file_rm(newname)
142+
print_good("File erased!")
153143
end
154144

155145
#Check if the file is encrypted or compressed
156146
def comp_encr(file)
157-
begin
158-
#http://msdn.microsoft.com/en-us/library/windows/desktop/aa364944(v=vs.85).aspx
159-
handle=client.railgun.kernel32.GetFileAttributesA(file)
160-
type= handle['return']
161-
162-
#FILE_ATTRIBUTE_COMPRESSED=0x800
163-
#FILE_ATTRIBUTE_ENCRYPTED=0x4000
164-
if ( type & (0x4800)).nonzero?
165-
return true
166-
end
167-
return false
147+
#http://msdn.microsoft.com/en-us/library/windows/desktop/aa364944(v=vs.85).aspx
148+
handle=client.railgun.kernel32.GetFileAttributesA(file)
149+
type= handle['return']
150+
151+
#FILE_ATTRIBUTE_COMPRESSED=0x800
152+
#FILE_ATTRIBUTE_ENCRYPTED=0x4000
153+
if ( type & (0x4800)).nonzero?
154+
return true
168155
end
156+
return false
169157
end
170158
end

0 commit comments

Comments
 (0)