Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Deleting a file off a Drive, beyond kill

0 views
Skip to first unread message

Peter

unread,
Aug 13, 2004, 1:18:32 PM8/13/04
to
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

Peter M

unread,
Aug 13, 2004, 2:29:04 PM8/13/04
to
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


Herfried K. Wagner [MVP]

unread,
Aug 13, 2004, 2:26:12 PM8/13/04
to
* pe...@mclinn.com (Peter) scripsit:

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/>

Peter M

unread,
Aug 13, 2004, 3:03:40 PM8/13/04
to
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-spa...@gmx.at> wrote in message
news:O058zNWg...@tk2msftngp13.phx.gbl...

no potted meat@hotmail.com David Browne

unread,
Aug 13, 2004, 4:57:47 PM8/13/04
to

"Peter" <pe...@mclinn.com> wrote in message
news:dcde2a5a.0408...@posting.google.com...


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


Peter

unread,
Aug 16, 2004, 8:48:36 AM8/16/04
to
Is there nothing that can be done to secure win95-WinMe deletions?

Peter

unread,
Aug 18, 2004, 4:48:28 PM8/18/04
to
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

Peter

unread,
Aug 18, 2004, 4:56:51 PM8/18/04
to
Cancel that last order.

Peter

unread,
Aug 18, 2004, 5:42:52 PM8/18/04
to
Cancel that last order.
0 new messages