PC Review


Reply
Thread Tools Rate Thread

Deleting a file off a Drive, beyond kill

 
 
Peter
Guest
Posts: n/a
 
      13th Aug 2004
I'm trying to write a proceedure that will delete a file completely
off a hardrive. I just don't want to kill the file. I want to
rewrite over the data with 1's and 0's 8 times. Anyone have an idea
or sample source?

-Peter
 
Reply With Quote
 
 
 
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      13th Aug 2004
* (E-Mail Removed) (Peter) scripsit:
> I'm trying to write a proceedure that will delete a file completely
> off a hardrive. I just don't want to kill the file. I want to
> rewrite over the data with 1's and 0's 8 times. Anyone have an idea
> or sample source?


Take a look at the 'System.IO.BinaryWriter' class. Notice that there is
no guarantee that the data is overwritten.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
 
Reply With Quote
 
Peter M
Guest
Posts: n/a
 
      13th Aug 2004
Would something like this work if I just resaved over the file a couple
times? Or does this re-write to a different spot on the drive?

'Pass in file to delete path.....

dim WriteToFile(9) as string

for i as integer = 0 to 8

WriteToFile(i) = SecureDelete("c:\MyFileToBeWiped")

next

....blah blah blah...open and rewrite file.....Save....?

---------------------------------

Public Function SecureDelete() As String '(ByVal PassedPath As String)

Dim i As Integer

Dim DimFill As String

For i = 1 To 10021

DimFill += rand().ToString

Next

DimFill += DimFill + DimFill

Return DimFill

End Function

-------------------------------------------------------------

Public Function rand() As Integer

Dim obj As New System.Random(Now.Millisecond)

Return obj.Next(0, 2)

End Function


 
Reply With Quote
 
Peter M
Guest
Posts: n/a
 
      13th Aug 2004
Herfried,

Then would it be possible to find the exact location of the bytes on the
drive, and overwrite them?

-Peter

"Herfried K. Wagner [MVP]" <hirf-spam-me-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> * (E-Mail Removed) (Peter) scripsit:
> > I'm trying to write a proceedure that will delete a file completely
> > off a hardrive. I just don't want to kill the file. I want to
> > rewrite over the data with 1's and 0's 8 times. Anyone have an idea
> > or sample source?

>
> Take a look at the 'System.IO.BinaryWriter' class. Notice that there is
> no guarantee that the data is overwritten.
>
> --
> M S Herfried K. Wagner
> M V P <URL:http://dotnet.mvps.org/>
> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>



 
Reply With Quote
 
David Browne
Guest
Posts: n/a
 
      13th Aug 2004

"Peter" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I'm trying to write a proceedure that will delete a file completely
> off a hardrive. I just don't want to kill the file. I want to
> rewrite over the data with 1's and 0's 8 times. Anyone have an idea
> or sample source?
>
> -Peter



Copying over the file almost certianly won't work, as the new file will be
created somewhere different, and then the directory updated. Otherwise if
the copy failed half way through, the old file would be lost.

Here's a sample which opens the file, writes zeros over its contents and
closes it multiple times. It's important to close the file, and important
to use certian parameters when opening the file.

The tricky thing is that if write caching is turned on for the disk then all
of your writing may really be to memory and bits may only be written to disk
once. To try and guarantee that the phisical disk is overwritten each time
we have to open the file with the FILE_FLAG_WRITE_THROUGH attribute, which
means, in turn, that we have to open the file with the Win32 CreateFile API.

Anyway here's a sample which should work on local NTFS volumes. There's no
way to tell how other types of storage would work. For instance a USP flash
device. We don't know what's actually written where. And anyway without
intimate knoledge of the storage hardware, this is about all we can do.

David


Imports System.IO
Imports System.Runtime.InteropServices

Public Class EraseFile
Shared ones(1024 * 8) As Byte
Shared zeros(1024 * 8) As Byte
Shared Sub New()
For i As Integer = 0 To ones.Length - 1
ones(i) = 1
Next
End Sub

Private Declare Function CreateFile _
Lib "kernel32" Alias "CreateFileA" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Integer, _
ByVal dwShareMode As Integer, _
<MarshalAs(UnmanagedType.Struct)> _
ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, _
ByVal dwCreationDisposition As Integer, _
ByVal dwFlagsAndAttributes As Integer, _
ByVal hTemplateFile As Integer) As Integer

<StructLayout(LayoutKind.Sequential)> _
Private Structure SECURITY_ATTRIBUTES
Public nLength As Integer
Public lpSecurityDescriptor As Integer
Public bInheritHandle As Integer
End Structure
Public Const GENERIC_WRITE = &H40000000
Public Const OPEN_ALWAYS = 4
Public Const FILE_FLAG_WRITE_THROUGH = &H80000000


Private Shared Function _
OpenFileWriteThrough(ByVal FileName As String) As FileStream
Dim fh As Integer = CreateFile(FileName, _
GENERIC_WRITE, _
0, _
Nothing, _
OPEN_ALWAYS, _
FILE_FLAG_WRITE_THROUGH, _
0)
Return New FileStream(New IntPtr(fh), FileAccess.Write)

End Function


Public Shared Sub EraseFile(ByVal FileName As String)

For i As Integer = 0 To 8
Dim f As FileStream
f = OpenFileWriteThrough(FileName)
Dim flen As Long = f.Length
f.Position = 0
Do While f.Position < flen
f.Write(ones, 0, Math.Min(ones.Length, flen - f.Position))
Loop
f.Close()
f = OpenFileWriteThrough(FileName)
f.Position = 0
Do While f.Position < flen
f.Write(zeros, 0, Math.Min(ones.Length, flen - f.Position))
Loop
f.Close()
Next
File.Delete(FileName)


End Sub
Public Shared Sub Main(ByVal args() As String)
EraseFile("d:\victim.dat")
End Sub

End Class


 
Reply With Quote
 
Peter
Guest
Posts: n/a
 
      16th Aug 2004
Is there nothing that can be done to secure win95-WinMe deletions?
 
Reply With Quote
 
Peter
Guest
Posts: n/a
 
      18th Aug 2004
I found this on Planet Source Code. It is writen in vb6. I take it
that the binary read in vb6 grabs the exact bytes and the binary
reader in vb.net doesn't? What do you think of this code?

Function DeleteFile(Path As String)
'This is an extremely quick file delete
' developed
'by me in about 5 minutes.
'overwrites the file 21 times then delet
' es it
'clean off your disk :-)
Dim i As Integer 'variable For times To overwrite
Dim Data1 As String, Data2 As String, Data3 As String, Data4 As
String, Data5 As String, Data6 As String, Data7 As String, Data8 As
String, Data9 As String, Data10 As String, Data11 As String, Data12 As
String, Data13 As String, Data14 As String, Data15 As String, Data16
As String, Data17 As String, Data18 As String, Data19 As String,
Data20 As String
'^^^ all 20 data variables, which hold t
' he information to overwrite the file wit
' h
Dim FinalByte As Byte 'just a byte To Do the final overwrite With
Data1 = Chr(85) 'the variables information
Data2 = Chr(170) 'the variables information
Data3 = Chr(74) 'the variables information
Data4 = Chr(99) 'the variables information
Data5 = Chr(71) 'the variables information
Data6 = Chr(92) 'the variables information
Data7 = Chr(101) 'the variables information
Data8 = Chr(112) 'the variables information
Data9 = Chr(1) 'the variables information
Data10 = Chr(61) 'the variables information
Data11 = Chr(97) 'the variables information
Data12 = Chr(119) 'the variables information
Data13 = Chr(86) 'the variables information
Data14 = Chr(79) 'the variables information
Data15 = Chr(109) 'the variables information
Data16 = Chr(72) 'the variables information
Data17 = Chr(90) 'the variables information
Data18 = Chr(0) 'the variables information
Data19 = Chr(255) 'the variables information
Data20 = Chr(212) 'the variables information
Open Path For Binary Access Write As #1 'open the path so we can
overwrite it


For i = 1 To 10 'a Loop
Put #1, , Data1 'overwrite
Next i 'stop Loop


For i = 1 To 10 'another Loop
Put #1, , Data2 'overwrite
Next i 'stop Loop


For i = 1 To 10 'another Loop
Put #1, , Data3 'overwrite
Next i 'stop Loop


For i = 1 To 10 'another Loop
Put #1, , Data4 'overwrite
Next i 'stop Loop


For i = 1 To 10 'another Loop
Put #1, , Data5 'overwrite
Next i 'stop Loop


For i = 1 To 10 'Im sure you Get the point from here on!
'that this is just the overwriting stage
' !
Put #1, , Data6
Next i


For i = 1 To 10
Put #1, , Data7
Next i


For i = 1 To 10
Put #1, , Data8
Next i


For i = 1 To 10
Put #1, , Data9
Next i


For i = 1 To 10
Put #1, , Data10
Next i


For i = 1 To 10
Put #1, , Data11
Next i


For i = 1 To 10
Put #1, , Data12
Next i


For i = 1 To 10
Put #1, , Data13
Next i


For i = 1 To 10
Put #1, , Data14
Next i


For i = 1 To 10
Put #1, , Data15
Next i


For i = 1 To 10
Put #1, , Data16
Next i


For i = 1 To 10
Put #1, , Data17
Next i


For i = 1 To 10
Put #1, , Data18
Next i


For i = 1 To 10
Put #1, , Data19
Next i


For i = 1 To 10
Put #1, , Data20
Next i


For i = 1 To 10 'the final Loop
Put #1, , FinalByte 'the final overwrite
Next i 'stop final Loop
Close #1 'close the file
Kill Path 'delete it
MsgBox "All Done Wiping The File!", vbInformation + vbOKOnly, "All
Done!" 'duh
End Function
 
Reply With Quote
 
Peter
Guest
Posts: n/a
 
      18th Aug 2004
Cancel that last order.
 
Reply With Quote
 
Peter
Guest
Posts: n/a
 
      18th Aug 2004
Cancel that last order.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
deleting files using kill statement toddr Microsoft Access VBA Modules 2 23rd Sep 2008 11:48 PM
Problem deleting .avi file from hard drive Kevin Miller Windows XP Hardware 2 13th Dec 2003 05:33 AM
Deleting a file on C drive using VBA Todd Huttenstine Microsoft Excel Programming 7 19th Nov 2003 03:26 AM
Kill Command & deleting from an ftp site Dobsons Microsoft Access VBA Modules 1 4th Sep 2003 06:48 PM
Deleting old Word Perfect file/kill messenger etc. popups Ron Gustafson Windows XP General 3 11th Jul 2003 11:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:16 PM.