To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
MSDN Subscribers: Download the VS 2010 Release Candidate
MSDN Subscribers: Download the VS 2010 Release Candidate
Sell Your Code and Make Money?
Creating your own Tetris game using VB.NET
Article :: Improving Software Economics, Part 4 of 7: Top 10 Principles of Iterative Software Management



Go Back   VBForums > Visual Basic > API

Reply Post New Thread
 
Thread Tools Search this Thread Display Modes
Old Jun 14th, 2007, 10:49 AM   #1
altf4
Lively Member
 
Join Date: May 04
Location: Home
Posts: 97
altf4 is an unknown quantity at this point (<10)
Native API: NtOpenKey & OBJECT_ATTRIBUTES ---> Need Help Please

Ok I am trying to work with Native API functions but it seems very hard to do since they mostly require unicode input strings.

I decided that one of the basic places to start would be using NtOpenKey to open a registry key.

What it requires is the handle to be opened (hKey), the key access, and OBJECT_ATTRIBUTES UDT.

Here is my code:
Code:
 Private Type ACL
        AclRevision As Byte
        Sbz1 As Byte
        AclSize As Integer
        AceCount As Integer
        Sbz2 As Integer
End Type

Private Type SECURITY_DESCRIPTOR
        Revision As Byte
        Sbz1 As Byte
        Control As Long
        Owner As Long
        Group As Long
        Sacl As ACL
        Dacl As ACL
End Type

Private Type UNICODE_STRING
       Length           As Long
       MaximumLength    As Long
       Buffer           As Long
End Type
 
'Private Type OBJECT_ATTRIBUTES
'    Length                      As Long
'    RootDirectory               As Long
'    ObjectName                  As UNICODE_STRING
'    Attributes                  As Long
'    SecurityDescriptor          As SECURITY_DESCRIPTOR
'    SecurityQualityOfService    As Long
'End Type

Private Type OBJECT_ATTRIBUTES
    Length                      As Long
    RootDirectory               As Long
    ObjectName                  As Long
    Attributes                  As Long
    SecurityDescriptor          As Long
    SecurityQualityOfService    As Long
End Type

Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Private Const LANG_NEUTRAL = &H0
Private Const SUBLANG_DEFAULT = &H1


Public Enum rcMainKey
    HKEY_CLASSES_ROOT = &H80000000
    HKEY_CURRENT_USER = &H80000001
    HKEY_LOCAL_MACHINE = &H80000002
    HKEY_USERS = &H80000003
    HKEY_PERFORMANCE_DATA = &H80000004
    HKEY_CURRENT_CONFIG = &H80000005
    HKEY_DYN_DATA = &H80000006
End Enum

Private Const KEY_ALL_ACCESS = &HF003F
 
Private Declare Sub RtlInitUnicodeString Lib "ntdll.dll" (DestinationString As UNICODE_STRING, ByVal SourceString As Long)
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long

Private Declare Function NtOpenKey Lib "ntdll.dll" (KeyHandle As Long, ByVal DesiredAccess As Long, ObjectAttributes As OBJECT_ATTRIBUTES) As Long
Private Declare Function NtClose Lib "ntdll.dll" (ByVal hObj As Long) As Long

Private Sub Command1_Click()
    Call OpenKey
End Sub

Public Sub OpenKey()

    Dim ObjAt           As OBJECT_ATTRIBUTES
    Dim hKey            As Long
    Dim hResult         As Long
    Dim UString         As UNICODE_STRING
    Dim l               As Long
    Dim hMem            As Long
    Dim KeySubPath      As String
   
    'KeySubPath = "\\Registry\\Machine\\SOFTWARE"
    KeySubPath = "\Registry\Machine\SOFTWARE"
    'Call RtlInitUnicodeString(UString, StrPtr(KeySubPath))
    
    ObjAt.Length = Len(ObjAt)
    ObjAt.RootDirectory = 0
    'ObjAt.ObjectName = UString
    'ObjAt.ObjectName = VarPtr(UString)
    ObjAt.ObjectName = StrPtr(KeySubPath)
    'ObjAt.Attributes = 0
    'ObjAt.SecurityDescriptor = 0
    'ObjAt.SecurityQualityOfService = 0
   
    hResult = NtOpenKey(hKey, KEY_ALL_ACCESS, ObjAt)
    
        MsgBox GetSysMsg(hResult)
        MsgBox hKey
   
        MsgBox GetError(hResult)
   
    If hKey = 0 Then Call NtClose(hKey)
   
End Sub

Public Function GetError(ByVal ret As Long) As String

    Dim sBuffer As String * 255
    
    FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, ret, LANG_NEUTRAL, sBuffer, 255, ByVal 0
    GetError = Trim(sBuffer)
   
End Function

Problem is, NtOpenKey keeps returning a 0x80000002 (&H80000002) which means "Ran out of memory"

I am trying to figure out how this is happening and I really need some help. The entire unicode pointer crap is killing me for the ObjAt.ObjectName because I dont know if this is the correct way to do it.

I have googled for hours for examples of how to use this in vb but I can only find code that does not work. So can someone please help me initialize this structure properly so I can further understand how to work with Native API functions in VB.

Thanks you.
altf4 is offline   Reply With Quote
Old Jun 15th, 2007, 03:50 PM   #2
minor28
Member
 
Join Date: Oct 06
Posts: 51
minor28 is an unknown quantity at this point (<10)
Re: Native API: NtOpenKey & OBJECT_ATTRIBUTES ---> Need Help Please

I wonder what you want to achieve to call functions in ntdll.

RegOpenKey or rather RegOpenKeyEx are the functions you should use. There are lots of code to execute from these functions via different ways depending on your parameters before you come to NtOpenKey. To succeed with NtOpenKey you must do these preparations.

<-------------------user mode-----------------------------------------------><-----kernel mode--->
|----advapi32.dll----|--------ntdll.dll-------------------------------------|
RegOpenKeyEx > lots of code > NtOpenKey > lots of code > interupt jumptable > ntoskrnl.exe > result

Do you really believe you can open a key with just NtOpenKey. A simple function like NtClose will work though
minor28 is offline   Reply With Quote
Old Jun 16th, 2007, 07:15 PM   #3
triggernum5
Hyperactive Member
 
Join Date: Aug 06
Posts: 367
triggernum5 is on a distinguished road (10+)
Re: Native API: NtOpenKey & OBJECT_ATTRIBUTES ---> Need Help Please

Is your goal to hide a registry key from anything not calling with the native api??

Last edited by triggernum5; Oct 2nd, 2007 at 09:49 AM.
triggernum5 is offline   Reply With Quote
Old Jun 17th, 2007, 08:41 AM   #4
schoolbusdriver
Fanatic Member
 
schoolbusdriver's Avatar
 
Join Date: Jan 06
Location: O'er yonder
Posts: 1,020
schoolbusdriver is a jewel in the rough (200+)schoolbusdriver is a jewel in the rough (200+)schoolbusdriver is a jewel in the rough (200+)
Re: Native API: NtOpenKey & OBJECT_ATTRIBUTES ---> Need Help Please

I'm curious too.

(Note that the company that used these as a method of copyright protection ended up in deep trouble. If the intention is to create hidden values, you can do it without the native APIs - although I won't post it. Also note that regedit won't export these hidden values, so they're easy to get rid of by exporting the branch to a file, deleting the branch in regedit, then importing the file.)
schoolbusdriver is offline   Reply With Quote
Old Jun 19th, 2007, 02:25 PM   #5
altf4
Lively Member
 
Join Date: May 04
Location: Home
Posts: 97
altf4 is an unknown quantity at this point (<10)
Re: Native API: NtOpenKey & OBJECT_ATTRIBUTES ---> Need Help Please

My goal here is to be able to work with native api so i can have better access to the registry. I am curious to how null charater regkeys can be implemented as well as deleted. I have observed the reghide app C source from sysinternals and it works wonderful, but in vb this is a litter harder to perform since its strings are in ANSI.

@ minor28, Hmm I guess you could use RegCreateKeyExW and take a VB6 regkey path string (with null charactor included) and convert every char to a separate byte and convert the byte array to unicode. Once you get the unicode array you would pass its pointer using VarPtr to the input of RegCreateKeyExW ?
Just a guess..

Anyways, Well I have figured out how to make this work from some other Unicode_String examples online. I changed the buffer to String and added "& chr(0)" to then end of the buffer and it appeared to work. Problem is the /0 null unicode char is not working properly.

I found a module on here to convert the string's byte array to unicode and pass it that way, yet it still does not seem the input the null char to the key.

Anyways, I guess I am out of ideas here. I mean I am very glad I atleast got these functions working, now I just have to find out other ways to do it so the unicode is properly input.
altf4 is offline   Reply With Quote
Old Jun 19th, 2007, 03:39 PM   #6
minor28
Member
 
Join Date: Oct 06
Posts: 51
minor28 is an unknown quantity at this point (<10)
Re: Native API: NtOpenKey & OBJECT_ATTRIBUTES ---> Need Help Please

If your problem is ansi -> unicode you have the API MultiByteToWideChar and back API WideCharToMultiByte or if you prefer ntdll.dll RtlAnsiStringToUnicodeString
minor28 is offline   Reply With Quote
Old Jun 19th, 2007, 04:31 PM   #7
triggernum5
Hyperactive Member
 
Join Date: Aug 06
Posts: 367
triggernum5 is on a distinguished road (10+)
Re: Native API: NtOpenKey & OBJECT_ATTRIBUTES ---> Need Help Please

I once tinkered with the same notion, but then Vista appeared and I opted to play it safe since I had no way to test it.. What does reghide.c do under Vista? Any idea?
triggernum5 is offline   Reply With Quote
Old Jun 19th, 2007, 10:49 PM   #8
altf4
Lively Member
 
Join Date: May 04
Location: Home
Posts: 97
altf4 is an unknown quantity at this point (<10)
Re: Native API: NtOpenKey & OBJECT_ATTRIBUTES ---> Need Help Please

Hello everyone.

Well I have just made a breakthrough and figured it out. Yay.

Anyways Thanks for all your input. Btw should I post the code once Im done modifing it?

Also, triggernum5, yes it should work under vista b/c I jsut tested this under vista with a null char and it can not by opened by any Win32 API functioon (IE Regedit or any other registry tool that uses win32 API. Now if it uses Native API then it will work and be displayed.)

Anyways, have a great night, now it looks like its time to move on to more Native Functions and some remote process thread crap. =]

VB6 0wnz
altf4 is offline   Reply With Quote
Old Jun 19th, 2007, 11:08 PM   #9
triggernum5
Hyperactive Member
 
Join Date: Aug 06
Posts: 367
triggernum5 is on a distinguished road (10+)
Re: Native API: NtOpenKey & OBJECT_ATTRIBUTES ---> Need Help Please

I was concerned about Vista security labeling me evil.. But thinking more, I doubt it would be possible to put mailious entries in, assuming windows uses the Win32 api to load crucial settings at boot etc.. <-- Confirmation couldn't hurt..
triggernum5 is offline   Reply With Quote
Old Jun 20th, 2007, 08:41 AM   #10
altf4
Lively Member
 
Join Date: May 04
Location: Home
Posts: 97
altf4 is an unknown quantity at this point (<10)
Re: Native API: NtOpenKey & OBJECT_ATTRIBUTES ---> Need Help Please

Interesting point, well atleast I think it uses Win32 API. I should try to create a startup value using a nullchar and see if it works. Ill prolly try that later on today.
altf4 is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > API


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

Forum Jump


All times are GMT -5. The time now is 01:33 PM.




To view more projects, click here

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.