Computing.Net > Forums > Programming > File Properties (VB)

File Properties (VB)

Reply to Message Icon

Original Message
Name: evenstar
Date: June 20, 2002 at 13:17:19 Pacific
Subject: File Properties (VB)
Comment:

I'm creating a program through VB 6 that monitors file access (aka if someone's been messing with your stuff). Basically it involves choosing the files to be monitored, obtaining properties such as file size, last modified, last accessed, etc. and running that information through the md5 algorithm to create a default hash. The properties will be hashed every x minutes and compared to the default hashes, and if any differences occur a message box/email/phone message will be sent to the computer's administrator.

What I'm looking for is some way I can hash the file/folder's properties. In VB the only thing I could find that is remotely close is the Attributes property (which only involves things like readonly, archive, etc). I can't find a way to make these properties into something that can be run through md5 and updated continuously. If anyone has an idea I would greatly appreciate it. I have the whole program planned out but I'm just stuck on this.


Report Offensive Message For Removal


Response Number 1
Name: Jeff J
Date: June 20, 2002 at 15:28:04 Pacific
Reply: (edit)

MD5 is pretty strong; I hope you're calling a C function for that. VB6 is terrible at hashing, especially since there's no shift operator. I'm just wondering if you need to hash at all. Let's see, if all you need to do is check if any of the properties have changed, then perhaps just adding the attributes mask and the sizes would accomplish what you need, but much faster. Thus, if any of the properties change, the sum will change too. The odds of a change plus an inverse numeric change are extremely improbable, so it seems doable. It's not like you're doing a checksum.

If you planned on processing the entire file's guts with MD5, then there would be no need to check attributes, but it would take forever. It would be a very reliable way to check for tampering, though, since otherwise you won't be able to check for same-size alterations, where the hacker is sharp enough to restore the file's modification time.

I must admit, there's not much to hash with file attributes (all in a single integer), but it's easy to grab all 3 file times using the Win32 API:

Private Const FILE_SHARE_READ = 1
Private Const OPEN_EXISTING = 3

Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private Declare Function GetFileTime Lib "kernel32" Alias "GetFileTime" (ByVal hFile As Long, pCreationTime As FILETIME, pLastAccessTime As FILETIME, pLastWriteTime As FILETIME) As Long

Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal pFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, pSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" (ByVal hObj As Long) As Long


Sub GetTimes(sFilePath As String)

Dim ftCreat As FILETIME
Dim ftAccess As FILETIME
Dim ftWrite As FILETIME
Dim hFile As Long
Dim lSuccess As Long

hFile = CreateFile(sFilePath, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0)

If hFile = INVALID_HANDLE_VALUE Then
Exit Sub 'failure
End If

lSuccess = GetFileTime(hFile, ftCreat, ftAccess, ftWrite)
Call CloseHandle(hFile)

If lSuccess = 0 Then Exit Sub 'failed

'do what you want with file times...

End Sub


Your project sounds really cool, as you can tell by my interest :)

Cheers


Report Offensive Follow Up For Removal

Response Number 2
Name: evenstar
Date: July 16, 2002 at 11:58:35 Pacific
Reply: (edit)

Jeff J, your advice helped, I never would have found those API calls on my own. I tried to use them but I'm lost because I'm a complete beginner. This is my first real VB program anyway. Here is what I have so far. It seems I can't run those calls even though I put in the code...its really frustrating.


Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Public Declare Function GetFileSize Lib "kernel32.dll" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long

Public Declare Function GetFileTime Lib "kernel32.dll" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long

Private Declare Function MakeMD5Digest _
Lib "di_MD5DLL.dll" _
(ByVal sData As String, ByVal sDigest As String) As Long

Public Function MD5HexDigest(sData As String) As String
Dim iRet As Long
Dim sDigest As String
' Set sDigest to be 32 chars
sDigest = String(32, " ")
iRet = MakeMD5Digest(sData, sDigest)
MD5HexDigest = Trim(sDigest)
End Function


Private Sub cmdRun_Click()

Dim FileInfo As String
Dim StandardComp As String
Dim Size As String
Dim lastacc As String

Size = GetFileSize(txtFolder.Text)
lastacc = GetFileTime(txtFolder.Text)
FileInfo = Size + lastacc
StandardComp = MD5HexDigest(FileInfo)

End Sub

Private Sub Command1_Click()
listFolders.AddItem txtFolder.Text
End Sub
'thats just to add file names to a list box.

I just want to make the md5 compare itself to StandardComp every x minutes and alert if they arent equal.

Help would be great from anyone.



Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: File Properties (VB)

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge