1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
| '/////////////////////////////////////////////////////////////////////////////////
'///////////////////////////////// MAJ 28/10/09
'/////////////////////////////////////////////////////////////////////////////////
Imports Microsoft.VisualBasic.CompilerServices
Imports Microsoft.VisualBasic
Imports Microsoft.Win32
Imports System.ComponentModel
Imports System
Imports System.Runtime.InteropServices
Namespace Registre
Friend NotInheritable Class mReg
Public Shared MainRK As RegistryKey
Public Shared ObjLVI As ListViewItem
Public Shared ObjRK As RegistryKey
Public Shared EachItem As String
Private Shared hBlob As IntPtr
Public Const HKEY_LOCAL_MACHINE As UInt32 = 2147483650
Private Shared MyToken As IntPtr
Private Shared regKey As RegistryKey
Private Const SE_BACKUP_NAME As String = "SeBackupPrivilege"
Private Const SE_PRIVILEGE_ENABLED As UInt32 = 2
Private Const SE_RESTORE_NAME As String = "SeRestorePrivilege"
Private Shared strKeyName As String
Private Const TOKEN_ADJUST_PRIVLEGES As UInt32 = 32
Private Const TOKEN_QUERY As UInt32 = 8
Private Shared TP1 As TOKEN_PRIVILEGES
Private Shared TP2 As TOKEN_PRIVILEGES
Public Shared WIMt As Boolean
Private Structure LUID
Private lowPart As UInt32
Private highPart As Integer
End Structure
Private Structure LUID_AND_ATTRIBUTES
Public luid As mReg.LUID
Public attributes As UInt32
End Structure
Private Structure TOKEN_PRIVILEGES
Public PrivilegeCount As UInt32
Public Privileges As mReg.LUID_AND_ATTRIBUTES
Public Function Size() As Integer
Return Marshal.SizeOf(Me)
End Function
End Structure
Shared Sub New()
mReg.WIMt = False
End Sub
<DllImportAttribute("advapi32", EntryPoint:="AdjustTokenPrivileges", CharSet:=CharSet.Ansi, ExactSpelling:=False, SetLastError:=True, PreserveSig:=True, CallingConvention:=CallingConvention.Winapi, _
BestFitMapping:=False, ThrowOnUnmappableChar:=False)> _
<PreserveSigAttribute()> _
Private Shared Function AdjustTokenPrivileges(ByVal TokenHandle As IntPtr, ByVal DisableAllPrivileges As Integer, ByRef NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Integer, ByRef PreviousState As TOKEN_PRIVILEGES, ByRef ReturnLength As Integer) As Integer
End Function
<DllImportAttribute("advapi32", EntryPoint:="LookupPrivilegeValue", CharSet:=CharSet.Ansi, ExactSpelling:=False, SetLastError:=True, PreserveSig:=True, CallingConvention:=CallingConvention.Winapi, _
BestFitMapping:=False, ThrowOnUnmappableChar:=False)> _
<PreserveSigAttribute()> _
Private Shared Function LookupPrivilegeValue(ByVal lpSystemName As String, ByVal lpName As String, ByRef lpLuid As LUID) As Integer
End Function
<DllImportAttribute("advapi32", EntryPoint:="OpenProcessToken", CharSet:=CharSet.None, ExactSpelling:=False, SetLastError:=True, PreserveSig:=True, CallingConvention:=CallingConvention.Winapi, _
BestFitMapping:=False, ThrowOnUnmappableChar:=False)> _
<PreserveSigAttribute()> _
Private Shared Function OpenProcessToken(ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As UInt32, ByRef TokenHandle As IntPtr) As Integer
End Function
<DllImportAttribute("advapi32", EntryPoint:="RegUnLoadKey", CharSet:=CharSet.Ansi, ExactSpelling:=False, SetLastError:=True, PreserveSig:=True, CallingConvention:=CallingConvention.Winapi, _
BestFitMapping:=False, ThrowOnUnmappableChar:=False)> _
<PreserveSigAttribute()> _
Public Shared Function RegUnLoadKey(ByVal hKey As UInt32, ByVal lpSubKey As String) As Integer
End Function
<DllImportAttribute("advapi32", EntryPoint:="RegLoadKey", CharSet:=CharSet.Ansi, ExactSpelling:=False, SetLastError:=True, PreserveSig:=True, CallingConvention:=CallingConvention.Winapi, _
BestFitMapping:=False, ThrowOnUnmappableChar:=False)> _
<PreserveSigAttribute()> _
Public Shared Function RegLoadKey(ByVal hKey As UInt32, ByVal lpSubKey As String, ByVal lpFile As String) As Integer
End Function
<DllImportAttribute("kernel32", EntryPoint:="GetCurrentProcess", CharSet:=CharSet.None, ExactSpelling:=False, SetLastError:=False, PreserveSig:=True, CallingConvention:=CallingConvention.Winapi, _
BestFitMapping:=False, ThrowOnUnmappableChar:=False)> _
<PreserveSigAttribute()> _
Private Shared Function GetCurrentProcess() As IntPtr
End Function
Public Shared Sub DelKey(ByVal Loc As RegistryKey, ByVal Key As String)
Dim string1 As String = Nothing
Dim registryKey2 As RegistryKey = Nothing
Dim string2 As String = Nothing
Try
Dim registryKey1 As RegistryKey = Loc.OpenSubKey(Key, True)
If Not registryKey1 Is Nothing Then
string2 = Key
While string2.Contains("\")
string2 = string2.Substring(1)
End While
string1 = Key.Substring(0, (DirectCast((Key.Length - (string2.Length + 1)), Integer)))
registryKey2 = Loc.OpenSubKey(string1, True)
registryKey2.DeleteSubKeyTree(string2)
End If
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
ProjectData.ClearProjectError()
End Try
End Sub
Public Shared Sub DelValue(ByVal Loc As RegistryKey, ByVal Key As String, ByVal Reg As String)
Dim exception1 As Exception = Nothing
Try
mReg.regKey = Loc.OpenSubKey(Key, True)
mReg.regKey.DeleteValue(Reg, False)
mReg.regKey.Close()
Catch exception2 As Exception
exception1 = exception2
ProjectData.SetProjectError(exception1)
Dim msgBoxResult1 As MsgBoxResult = Interaction.MsgBox(exception1.Message, MsgBoxStyle.Critical, "Error!")
ProjectData.ClearProjectError()
End Try
End Sub
Public Shared Function RegCheckMounted(ByVal Key As String) As Object
Dim registryKey1 As RegistryKey = Nothing
Dim b1 As Boolean = False
Try
registryKey1 = Registry.LocalMachine.OpenSubKey(Key, RegistryKeyPermissionCheck.ReadSubTree)
Dim object2 As Object = registryKey1.GetValue("(Default)")
b1 = True
registryKey1.Close()
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
b1 = False
ProjectData.ClearProjectError()
End Try
Return b1
End Function
Public Shared Sub RegStart()
Dim lUID1 As LUID = New LUID()
Dim tOKEN_PRIVILEGES1 As TOKEN_PRIVILEGES = New TOKEN_PRIVILEGES()
Dim lUID2 As LUID = New LUID()
Dim intPtr1 As IntPtr = mReg.GetCurrentProcess()
If mReg.OpenProcessToken(intPtr1, 40, mReg.MyToken) = 0 Then
Throw New Win32Exception()
End If
If mReg.LookupPrivilegeValue((DirectCast(Nothing, String)), "SeRestorePrivilege", lUID2) = 0 Then
Throw New Win32Exception()
End If
If mReg.LookupPrivilegeValue((DirectCast(Nothing, String)), "SeBackupPrivilege", lUID1) = 0 Then
Throw New Win32Exception()
End If
mReg.TP1.PrivilegeCount = 1
mReg.TP1.Privileges.attributes = 2
mReg.TP1.Privileges.luid = lUID2
Dim i1 As Integer = 0
If mReg.AdjustTokenPrivileges(mReg.MyToken, 0, mReg.TP1, mReg.TP1.Size(), tOKEN_PRIVILEGES1, i1) = 0 Then
Throw New Win32Exception()
Else
mReg.TP2.PrivilegeCount = 1
mReg.TP2.Privileges.attributes = 2
mReg.TP2.Privileges.luid = lUID1
If mReg.AdjustTokenPrivileges(mReg.MyToken, 0, mReg.TP2, mReg.TP2.Size(), tOKEN_PRIVILEGES1, i1) = 0 Then
Throw New Win32Exception()
End If
End If
End Sub
Public Shared Sub RegStop()
Dim tOKEN_PRIVILEGES1 As TOKEN_PRIVILEGES = New TOKEN_PRIVILEGES()
Dim tOKEN_PRIVILEGES2 As TOKEN_PRIVILEGES = New TOKEN_PRIVILEGES()
Dim i1 As Integer = 0
tOKEN_PRIVILEGES2 = tOKEN_PRIVILEGES1
If mReg.AdjustTokenPrivileges(mReg.MyToken, 0, mReg.TP1, mReg.TP1.Size(), tOKEN_PRIVILEGES2, i1) = 0 Then
Throw New Win32Exception()
Else
tOKEN_PRIVILEGES2 = tOKEN_PRIVILEGES1
If mReg.AdjustTokenPrivileges(mReg.MyToken, 0, mReg.TP2, mReg.TP2.Size(), tOKEN_PRIVILEGES2, i1) = 0 Then
Throw New Win32Exception()
End If
End If
End Sub
Public Shared Sub TakeOwn(ByVal F As String, ByVal Chemin As String)
Dim string1 As String = Nothing
Dim string2 As String = Nothing
If Operators.CompareString(F, "", False) = 0 Then
string1 = Chemin & "\Windows\System32\Config\"
string2 = Chemin & "\Users\Default\"
Dim i1 As Integer = Interaction.Shell(("takeown.exe /F """ + string1 + "SOFTWARE"" /A /R /D O"), AppWinStyle.Hide, False, -1)
Dim i2 As Integer = Interaction.Shell(("takeown.exe /F """ + string1 + "COMPONENTS"" /A /R /D O"), AppWinStyle.Hide, False, -1)
Dim i3 As Integer = Interaction.Shell(("takeown.exe /F """ + string1 + "SAM"" /A /R /D O"), AppWinStyle.Hide, False, -1)
Dim i4 As Integer = Interaction.Shell(("takeown.exe /F """ + string1 + "DEFAULT"" /A /R /D O"), AppWinStyle.Hide, False, -1)
Dim i5 As Integer = Interaction.Shell(("takeown.exe /F """ + string1 + "SECURITY"" /A /R /D O"), AppWinStyle.Hide, False, -1)
Dim i6 As Integer = Interaction.Shell(("takeown.exe /F """ + string1 + "SYSTEM"" /A /R /D O"), AppWinStyle.Hide, False, -1)
Dim i7 As Integer = Interaction.Shell(("takeown.exe /F """ + string2 + "NTUSER.DAT"" /A /R /D O"), AppWinStyle.Hide, False, -1)
Module1.WaitToFinish("takeown")
Dim i8 As Integer = Interaction.Shell(("icacls " + string1 + "SOFTWARE /grant administrateurs:F /t"), AppWinStyle.Hide, False, -1)
Dim i9 As Integer = Interaction.Shell(("icacls " + string1 + "COMPONENTS /grant administrateurs:F /t"), AppWinStyle.Hide, False, -1)
Dim i10 As Integer = Interaction.Shell(("icacls " + string1 + "SAM /grant administrateurs:F /t"), AppWinStyle.Hide, False, -1)
Dim i11 As Integer = Interaction.Shell(("icacls " + string1 + "DEFAULT /grant administrateurs:F /t"), AppWinStyle.Hide, False, -1)
Dim i12 As Integer = Interaction.Shell(("icacls " + string1 + "SECURITY /grant administrateurs:F /t"), AppWinStyle.Hide, False, -1)
Dim i13 As Integer = Interaction.Shell(("icacls " + string1 + "SYSTEM /grant administrateurs:F /t"), AppWinStyle.Hide, False, -1)
Dim i14 As Integer = Interaction.Shell(("icacls " + string2 + "NTUSER.DAT /grant administrateurs:F /t"), AppWinStyle.Hide, False, -1)
Module1.WaitToFinish("icacls")
Else
Dim i15 As Integer = Interaction.Shell(("takeown.exe /F """ + F + """ /A /R /D O"), AppWinStyle.Hide, False, -1)
Module1.WaitToFinish("takeown")
Dim i16 As Integer = Interaction.Shell(("icacls """ + F + """ /grant administrateurs:F /t"), AppWinStyle.Hide, False, -1)
Module1.WaitToFinish("icacls")
End If
End Sub
Public Shared Sub WriteKey(ByVal Loc As RegistryKey, ByVal Key As String, ByVal Reg As String, ByVal Value As String, ByVal Kind As RegistryValueKind)
Dim registryKey2 As RegistryKey = Nothing
Dim string1 As String = Nothing
Dim string2 As String = Nothing
Dim exception1 As Exception = Nothing
Dim string3 As String = Nothing
If mReg.WIMt Then
string3 = Loc.ToString()
If Operators.CompareString(string3, "HKEY_CURRENT_USER", False) = 0 Then
Loc = Registry.LocalMachine
Key = ("WIM_Default\" + Key)
ElseIf Operators.CompareString(string3, "HKEY_LOCAL_MACHINE", False) = 0 Then
If Key.ToUpper().StartsWith("SOFTWARE") Then
While Not Key.StartsWith("\")
Key = Key.Substring(1)
End While
End If
Key = ("WIM_Software\" + Key)
End If
End If
Dim registryKey1 As RegistryKey = Loc.OpenSubKey(Key, True)
If Not registryKey1 Is Nothing Then
string1 = Key
While string1.Contains("\")
string1 = string1.Substring(0, (DirectCast((string1.Length - 1), Integer)))
End While
string2 = Key
While (Not string2.StartsWith("\"))
string2 = string2.Substring(1)
End While
While string2.StartsWith("\")
string2 = string2.Substring(1)
End While
registryKey2 = Loc.OpenSubKey(string1, True)
Dim registryKey3 As RegistryKey = registryKey2.CreateSubKey(string2)
End If
mReg.regKey = Loc.OpenSubKey(Key, True)
Try
mReg.regKey.SetValue(Reg, Value, Kind)
mReg.regKey.Close()
Catch exception2 As Exception
exception1 = exception2
ProjectData.SetProjectError(exception1)
Dim msgBoxResult1 As MsgBoxResult = Interaction.MsgBox(exception1.Message, MsgBoxStyle.Critical, "Error!")
ProjectData.ClearProjectError()
End Try
End Sub
End Class
Public Class MyClsBlnREG_DWORD
Public Shared Function RegIsSeted(ByVal SubKey As String, ByVal ValueName As String) As Boolean
Dim flag As Boolean
Try
mReg.ObjRK = mReg.MainRK.OpenSubKey(SubKey)
If Operators.ConditionalCompareObjectEqual(mReg.ObjRK.GetValue(ValueName), 1, False) Then
Return True
End If
flag = False
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
flag = False
ProjectData.ClearProjectError()
Return flag
ProjectData.ClearProjectError()
End Try
Return flag
End Function
Public Shared Function RegIsSeted(ByVal Reserve As Boolean, ByVal SubKey As String, ByVal ValueName As String) As Boolean
Dim flag As Boolean
If Reserve Then
Try
mReg.ObjRK = mReg.MainRK.OpenSubKey(SubKey)
Dim valueNames As String() = mReg.ObjRK.GetValueNames
Dim i As Integer
For i = 0 To valueNames.Length - 1
mReg.EachItem = valueNames(i)
If ((mReg.EachItem.ToLower = ValueName.ToLower) AndAlso Operators.ConditionalCompareObjectEqual(mReg.ObjRK.GetValue(ValueName), 0, False)) Then
Return True
End If
Next i
flag = False
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
flag = False
ProjectData.ClearProjectError()
Return flag
ProjectData.ClearProjectError()
End Try
End If
Return flag
End Function
Public Shared Sub SetRegValue(ByVal SubKey As String, ByVal ValueName As String, ByVal value As Boolean)
Try
mReg.ObjRK = mReg.MainRK.CreateSubKey(SubKey)
If value Then
mReg.ObjRK.SetValue(ValueName, 1, RegistryValueKind.DWord)
Else
mReg.ObjRK.DeleteValue(ValueName, False)
End If
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
ProjectData.ClearProjectError()
End Try
End Sub
Public Shared Sub SetRegValue(ByVal Reserve As Boolean, ByVal SubKey As String, ByVal ValueName As String, ByVal value As Boolean)
Try
If Reserve Then
mReg.ObjRK = mReg.MainRK.CreateSubKey(SubKey)
If value Then
mReg.ObjRK.SetValue(ValueName, 0, RegistryValueKind.DWord)
Else
mReg.ObjRK.DeleteValue(ValueName, False)
End If
End If
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim exception As Exception = exception1
ProjectData.ClearProjectError()
End Try
End Sub
End Class
Public Class MyClsBlnREG_SZ
Public Shared Function RegIsSeted(ByVal SubKey As String, ByVal ValueName As String, ByVal strTrueValue As String) As Boolean
Dim flag As Boolean
Try
mReg.ObjRK = mReg.MainRK.OpenSubKey(SubKey)
If (Conversions.ToString(mReg.ObjRK.GetValue(ValueName)).ToLower = strTrueValue.ToLower) Then
Return True
End If
flag = False
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
flag = False
ProjectData.ClearProjectError()
Return flag
ProjectData.ClearProjectError()
End Try
Return flag
End Function
Public Shared Sub SetRegValue(ByVal SubKey As String, ByVal ValueName As String, ByVal strTrueValue As String, ByVal value As Boolean)
Try
mReg.ObjRK = mReg.MainRK.CreateSubKey(SubKey)
If value Then
mReg.ObjRK.SetValue(ValueName, strTrueValue, RegistryValueKind.String)
Else
mReg.ObjRK.DeleteValue(ValueName, False)
End If
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
ProjectData.ClearProjectError()
End Try
End Sub
End Class
Public Class MyClsREG_DWORD
Public Shared Function GetRegValue(ByVal SubKey As String, ByVal ValueName As String) As Integer
Dim num As Integer
Try
mReg.ObjRK = mReg.MainRK.OpenSubKey(SubKey)
num = Conversions.ToInteger(mReg.ObjRK.GetValue(ValueName, -1))
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
ProjectData.ClearProjectError()
End Try
Return num
End Function
Public Shared Sub SetRegValue(ByVal SubKey As String, ByVal ValueName As String, ByVal IntValue As Integer)
Try
mReg.ObjRK = mReg.MainRK.CreateSubKey(SubKey)
mReg.ObjRK.SetValue(ValueName, IntValue, RegistryValueKind.DWord)
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
ProjectData.ClearProjectError()
End Try
End Sub
End Class
Public Class MyClsREG_SZ
Public Shared Function GetRegValue(ByVal SubKey As String, ByVal ValueName As String) As String
Try
mReg.ObjRK = mReg.MainRK.OpenSubKey(SubKey)
Return Conversions.ToString(mReg.ObjRK.GetValue(ValueName, ""))
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
ProjectData.ClearProjectError()
End Try
Return ""
End Function
Public Shared Sub SetRegValue(ByVal SubKey As String, ByVal ValueName As String, ByVal strValue As String)
Try
mReg.MainRK.CreateSubKey(SubKey).SetValue(ValueName, strValue, RegistryValueKind.String)
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
ProjectData.ClearProjectError()
End Try
End Sub
Public Shared Sub refreshRegistry()
Registry.ClassesRoot.Close()
Registry.CurrentUser.Close()
Registry.LocalMachine.Close()
Registry.Users.Close()
Registry.CurrentConfig.Close()
End Sub
End Class
End Namespace |
Partager