Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Visual Basic Programming

Visual Basic Programming Discussion forum for Visual Basic, an event driven programming language and associated development environment from Microsoft for its COM programming model.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-29-2007, 11:00 AM
vinay vinay is offline
Newbie
 
Join Date: Dec 2007
Posts: 17
Rep Power: 3
vinay is on a distinguished road
Default SecurityAudit

let me explain my project ...
I was handed this project by the CIO of Henkel CAC Mumbai.
This project wants me to scan the computer for softwares installed in the pc as soon as a pen drive is inserted in the usb slot.Basically he wants the software to auto run.

The organization has certain policies regarding the softwares installed in their PC.They are allowed to install only certain software and the rest of the softwares apart from the ones that they have defined as authorised are to be considered as unauthorized and must not be present.

My software has to scan the computer for both authorised and unauthorised software.
1> If unauthorised software/s is found a message has to be flashed listing the name of the unauthorized software/s and the user should be able to uninstall the software from my software itself and not frm the add or remove.
2> A log file is to be maintained of the scans and the logs should contain name of the computer to differentiate the scans done by the software and the state of authorised and unauthorised software of a particular computer.

So far i am able to list the softwares installed in the computer.

Difficulties:-If an authorized software is not installed ... how am i supposed to search for it and flash a message saying that the software is not installed...Should a database be involved in handling this situation or can it be done without a database.


FORM Code:-

VB Code:
  1. Option Explicit
  2.  
  3.  
  4. Private Sub Command1_Click()
  5.     Unload Me
  6. End Sub
  7.  
  8. Private Sub Form_Load()
  9.     'Label1 = GetSettingString(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", "")
  10.  
  11. Dim SubKeys As Variant
  12. Dim KeyLoop As Integer
  13. Dim sDispName As String
  14. SubKeys = GetAllKeys(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall")
  15.  
  16. If VarType(SubKeys) = vbArray + vbString Then
  17.     For KeyLoop = 0 To UBound(SubKeys)
  18.         sDispName = GetSettingString(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & SubKeys(KeyLoop), "DisplayName")
  19.         If sDispName > "" And Left(SubKeys(KeyLoop), 1) <> "{" Then
  20.             List1.AddItem sDispName & " / " & SubKeys(KeyLoop)
  21.         End If
  22.     Next
  23. End If
  24. End Sub
  25.  
  26.  
  27. Sub CallCodeForGetAllValuesInAKey(ByVal sIn As String)
  28. Dim Values As Variant
  29. Dim KeyLoop As Integer
  30. Dim RegPath As String
  31. Dim HKCU As Long
  32. Dim sLine As String
  33. HKCU = HKEY_LOCAL_MACHINE 'to save typing
  34. RegPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & sIn
  35.  
  36. Values = GetAllValues(HKCU, RegPath)
  37.  
  38. If VarType(Values) = vbArray + vbVariant Then
  39.  
  40. For KeyLoop = 0 To UBound(Values)
  41.     sLine = Values(KeyLoop, 0) & " = "
  42.    
  43.     Select Case Values(KeyLoop, 1)
  44.     Case REG_DWORD
  45.         sLine = sLine & GetSettingLong(HKCU, RegPath, _
  46.         CStr(Values(KeyLoop, 0)))
  47.     Case REG_BINARY
  48.         sLine = sLine & GetSettingByte(HKCU, RegPath, _
  49.         Hex$(Values(KeyLoop, 0)))(0)
  50.     Case REG_SZ
  51.         sLine = sLine & GetSettingString(HKCU, RegPath, _
  52.         CStr(Values(KeyLoop, 0)))
  53.     End Select
  54.     List2.AddItem sLine
  55. Next KeyLoop
  56.  
  57. End If
  58.  
  59. End Sub
  60.  
  61. Private Sub List1_Click()
  62.     If List1.ListIndex < 0 Then
  63.         Exit Sub
  64.     End If
  65.     List2.Clear
  66.     CallCodeForGetAllValuesInAKey Mid(List1.Text, InStr(List1.Text, " / ") + 3)
  67.    
  68. End Sub


Module code:-
VB Code:
  1. Option Explicit
  2.  
  3. Public Const HKEY_CLASSES_ROOT = &H80000000
  4. Public Const HKEY_CURRENT_USER = &H80000001
  5. Public Const HKEY_LOCAL_MACHINE = &H80000002
  6. Public Const HKEY_USERS = &H80000003
  7. Public Const HKEY_CURRENT_CONFIG = &H80000005
  8. Public Const HKEY_DYN_DATA = &H80000006
  9. Public Const REG_SZ = 1 'Unicode nul terminated string
  10. Public Const REG_BINARY = 3 'Free form binary
  11. Public Const REG_DWORD = 4 '32-bit number
  12. Public Const ERROR_SUCCESS = 0&
  13.  
  14. Public Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" _
  15.     (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, _
  16.     lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, _
  17.     lpData As Any, lpcbData As Long) As Long
  18.  
  19. Public Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" _
  20. (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
  21.  
  22.  
  23. Public Declare Function RegCloseKey Lib "advapi32.dll" _
  24. (ByVal hKey As Long) As Long
  25.  
  26. Public Declare Function RegCreateKey Lib "advapi32.dll" _
  27. Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey _
  28. As String, phkResult As Long) As Long
  29.  
  30. Public Declare Function RegDeleteKey Lib "advapi32.dll" _
  31. Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey _
  32. As String) As Long
  33.  
  34. Public Declare Function RegDeleteValue Lib "advapi32.dll" _
  35. Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal _
  36. lpValueName As String) As Long
  37.  
  38. Public Declare Function RegOpenKey Lib "advapi32.dll" _
  39. Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey _
  40. As String, phkResult As Long) As Long
  41.  
  42. Public Declare Function RegQueryValueEx Lib "advapi32.dll" _
  43. Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName _
  44. As String, ByVal lpReserved As Long, lpType As Long, lpData _
  45. As Any, lpcbData As Long) As Long
  46.  
  47. Public Declare Function RegSetValueEx Lib "advapi32.dll" _
  48. Alias "RegSetValueExA" (ByVal hKey As Long, ByVal _
  49. lpValueName As String, ByVal Reserved As Long, ByVal _
  50. dwType As Long, lpData As Any, ByVal cbData As Long) As Long
  51.  
  52.  
  53. Public Function GetSettingString(hKey As Long, _
  54. strPath As String, strValue As String, Optional _
  55. Default As String) As String
  56. Dim hCurKey As Long
  57. Dim lResult As Long
  58. Dim lValueType As Long
  59. Dim strBuffer As String
  60. Dim lDataBufferSize As Long
  61. Dim intZeroPos As Integer
  62. Dim lRegResult As Long
  63.  
  64. 'Set up default value
  65. If Not IsEmpty(Default) Then
  66. GetSettingString = Default
  67. Else
  68. GetSettingString = ""
  69. End If
  70.  
  71. lRegResult = RegOpenKey(hKey, strPath, hCurKey)
  72. lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, _
  73. lValueType, ByVal 0&, lDataBufferSize)
  74.  
  75. If lRegResult = ERROR_SUCCESS Then
  76.  
  77. If lValueType = REG_SZ Then
  78.  
  79. strBuffer = String(lDataBufferSize, " ")
  80. lResult = RegQueryValueEx(hCurKey, strValue, 0&, 0&, _
  81. ByVal strBuffer, lDataBufferSize)
  82.  
  83. intZeroPos = InStr(strBuffer, Chr$(0))
  84. If intZeroPos > 0 Then
  85. GetSettingString = Left$(strBuffer, intZeroPos - 1)
  86. Else
  87. GetSettingString = strBuffer
  88. End If
  89.  
  90. End If
  91.  
  92. Else
  93. 'there is a problem
  94. End If
  95.  
  96. lRegResult = RegCloseKey(hCurKey)
  97. End Function
  98. Public Function GetSettingLong(ByVal hKey As Long, _
  99. ByVal strPath As String, ByVal strValue As String, _
  100. Optional Default As Long) As Long
  101.  
  102. Dim lRegResult As Long
  103. Dim lValueType As Long
  104. Dim lBuffer As Long
  105. Dim lDataBufferSize As Long
  106. Dim hCurKey As Long
  107.  
  108. 'Set up default value
  109. If Not IsEmpty(Default) Then
  110. GetSettingLong = Default
  111. Else
  112. GetSettingLong = 0
  113. End If
  114.  
  115. lRegResult = RegOpenKey(hKey, strPath, hCurKey)
  116. lDataBufferSize = 4 '4 bytes = 32 bits = long
  117.  
  118. lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, _
  119. lValueType, lBuffer, lDataBufferSize)
  120.  
  121. If lRegResult = ERROR_SUCCESS Then
  122.  
  123. If lValueType = REG_DWORD Then
  124. GetSettingLong = lBuffer
  125. End If
  126.  
  127. Else
  128. 'there is a problem
  129. End If
  130.  
  131. lRegResult = RegCloseKey(hCurKey)
  132. End Function
  133.  
  134. Public Sub SaveSettingLong(ByVal hKey As Long, ByVal _
  135. strPath As String, ByVal strValue As String, ByVal lData As Long)
  136. Dim hCurKey As Long
  137. Dim lRegResult As Long
  138.  
  139. lRegResult = RegCreateKey(hKey, strPath, hCurKey)
  140.  
  141. lRegResult = RegSetValueEx(hCurKey, strValue, 0&, _
  142. REG_DWORD, lData, 4)
  143.  
  144. If lRegResult <> ERROR_SUCCESS Then
  145. 'there is a problem
  146. End If
  147.  
  148. lRegResult = RegCloseKey(hCurKey)
  149. End Sub
  150. Public Sub SaveSettingString(hKey As Long, strPath _
  151. As String, strValue As String, strData As String)
  152. Dim hCurKey As Long
  153. Dim lRegResult As Long
  154.  
  155. lRegResult = RegCreateKey(hKey, strPath, hCurKey)
  156.  
  157. lRegResult = RegSetValueEx(hCurKey, strValue, 0, REG_SZ, _
  158. ByVal strData, Len(strData))
  159.  
  160. If lRegResult <> ERROR_SUCCESS Then
  161. 'there is a problem
  162. End If
  163.  
  164. lRegResult = RegCloseKey(hCurKey)
  165. End Sub
  166.  
  167. Public Function GetSettingByte(ByVal hKey As Long, _
  168. ByVal strPath As String, ByVal strValueName As String, _
  169. Optional Default As Variant) As Variant
  170. Dim lValueType As Long
  171. Dim byBuffer() As Byte
  172. Dim lDataBufferSize As Long
  173. Dim lRegResult As Long
  174. Dim hCurKey As Long
  175.  
  176. If Not IsEmpty(Default) Then
  177. If VarType(Default) = vbArray + vbByte Then
  178. GetSettingByte = Default
  179. Else
  180. GetSettingByte = 0
  181. End If
  182.  
  183. Else
  184. GetSettingByte = 0
  185. End If
  186.  
  187. lRegResult = RegOpenKey(hKey, strPath, hCurKey)
  188.  
  189. lRegResult = RegQueryValueEx(hCurKey, strValueName, 0&, _
  190. lValueType, ByVal 0&, lDataBufferSize)
  191.  
  192. If lRegResult = ERROR_SUCCESS Then
  193.  
  194. If lValueType = REG_BINARY Then
  195.  
  196. ReDim byBuffer(lDataBufferSize - 1) As Byte
  197. lRegResult = RegQueryValueEx(hCurKey, strValueName, 0&, _
  198. lValueType, byBuffer(0), lDataBufferSize)
  199. GetSettingByte = byBuffer
  200.  
  201. End If
  202.  
  203. Else
  204. 'there is a problem
  205. End If
  206.  
  207. lRegResult = RegCloseKey(hCurKey)
  208.  
  209. End Function
  210.  
  211.  
  212. Public Function GetAllValues(hKey As Long, _
  213. strPath As String) As Variant
  214. 'Returns: a 2D array.
  215. '(x,0) is value name
  216. '(x,1) is value type (see constants)
  217.  
  218. Dim lRegResult As Long
  219. Dim hCurKey As Long
  220. Dim lValueNameSize As Long
  221. Dim strValueName As String
  222. Dim lCounter As Long
  223. Dim byDataBuffer(4000) As Byte
  224. Dim lDataBufferSize As Long
  225. Dim lValueType As Long
  226. Dim strNames() As String
  227. Dim lTypes() As Long
  228. Dim intZeroPos As Integer
  229.  
  230. lRegResult = RegOpenKey(hKey, strPath, hCurKey)
  231.  
  232. Do
  233.     'Initialise bufffers
  234.     lValueNameSize = 255
  235.     strValueName = String$(lValueNameSize, " ")
  236.     lDataBufferSize = 4000
  237.    
  238.     lRegResult = RegEnumValue(hCurKey, lCounter, _
  239.     strValueName, lValueNameSize, 0&, lValueType, _
  240.     byDataBuffer(0), lDataBufferSize)
  241.    
  242.     If lRegResult = ERROR_SUCCESS Then
  243.    
  244.         'Save the type
  245.         ReDim Preserve strNames(lCounter) As String
  246.         ReDim Preserve lTypes(lCounter) As Long
  247.         lTypes(UBound(lTypes)) = lValueType
  248.        
  249.         'Tidy up string and save it
  250.         intZeroPos = InStr(strValueName, Chr$(0))
  251.         If intZeroPos > 0 Then
  252.             strNames(UBound(strNames)) = _
  253.             Left$(strValueName, intZeroPos - 1)
  254.