本帖最后由 ceoguang 于 2010-9-3 03:28 编辑
感谢P版的关注,问题依然没解决.
在官方找到个例子,可以取系统所有内核对象的句柄,不过有些BUG,贴上代码,以供研究
#include <WinAPI.au3> ; _GetPrivilege_SEDEBUG() - by wraithdu - uses this include.
#include <array.au3> ; Needed to display array in example.
#RequireAdmin
; SystemHandleInformation = 16
;~ typedef struct _SYSTEM_HANDLE_TABLE_ENTRY_INFO {
;~ USHORT UniqueProcessId;
;~ USHORT CreatorBackTraceIndex;
;~ UCHAR ObjectTypeIndex;
;~ UCHAR HandleAttributes;
;~ USHORT HandleValue;
;~ PVOID Object;
;~ ULONG GrantedAccess;
;~ } SYSTEM_HANDLE_TABLE_ENTRY_INFO, *PSYSTEM_HANDLE_TABLE_ENTRY_INFO;
;~ typedef struct _SYSTEM_HANDLE_INFORMATION {
;~ ULONG NumberOfHandles;
;~ SYSTEM_HANDLE_TABLE_ENTRY_INFO Handles[ 1 ];
;~ } SYSTEM_HANDLE_INFORMATION, *PSYSTEM_HANDLE_INFORMATION;
;~ BOOL DuplicateHandle(
;~ HANDLE hSourceProcessHandle, // handle to process with handle to duplicate OpenProcess PROCESS_DUP_HANDLE
;~ HANDLE hSourceHandle, // handle to duplicate
;~ HANDLE hTargetProcessHandle, // handle to process to duplicate to GetCurrentProcess PROCESS_DUP_HANDLE
;~ LPHANDLE lpTargetHandle, // pointer to duplicate handle
;~ DWORD dwDesiredAccess, // access for duplicate handle 0
;~ BOOL bInheritHandle, // handle inheritance flag 0
;~ DWORD dwOptions // optional actions const $DUPLICATE_SAME_ACCESS = 0x2
;~ );
;~ NtQueryObject(
;~ IN HANDLE ObjectHandle,
;~ IN OBJECT_INFORMATION_CLASS ObjectInformationClass, ObjectTypeInformation = 2 , ObjectNameInformation=1
;~ OUT PVOID ObjectInformation,
;~ IN ULONG Length,
;~ OUT PULONG ResultLength );
;~ $tag_OBJECT_TYPE=
;~ "ushort Length;" & _
;~ "ushort MaximumLength;" & _
;~ "ptr ProcessName;" & _
;~ "byte[512]"
$tag_SYSTEM_HANDLE_INFO= _
"USHORT UniqueProcessId;" & _;
"USHORT CreatorBackTraceIndex;" & _;
"ubyte ObjectTypeIndex;" & _;
"ubyte HandleAttributes;" & _;
"USHORT HandleValue;" & _;
"ptr Object;" & _;
"ptr GrantedAccess";
$tag_OBJECT_TYPE= _ ; TYPE / NAME Doesnt matter... I just want the unicodestring.
"ushort Length;" & _
"ushort MaximumLength;" & _
"ptr Name;" & _
"byte[512]"
; ############# Needed Constants ###################
Global Const $PROCESS_VM_READ=0x10
Global Const $PROCESS_QUERY_INFORMATION = 0x400
; ############ Example code #######################
_GetPrivilege_SEDEBUG()
$temp=_Handles()
_ArrayDisplay($temp)
; ###############################################
; ############ Here be func! ####################
Func _Handles()
Local $times[10]
Local $Mem=DllStructCreate("byte[" & 40000000 & "]")
Local $ret=dllcall("ntdll.dll", "int", "ZwQuerySystemInformation","int", 16, "ptr", DllStructGetPtr($MEM), "int", DllStructGetSize($MEM), "int*",0)
Local $SysHnd=DllStructCreate($tag_SYSTEM_HANDLE_INFO, $ret[2]+4)
Local $dw=DllStructCreate("dword",$ret[2])
Local $Count=DllStructGetData($dw,1)
Local $SysHnd_ptr=$ret[2]+4
Local $SysHnd_Size=DllStructGetSize($SysHnd)
Local $buffer, $i=0, $lastthread, $m=0, $NextEntryDelta, $k, $temp, $space, $l
Local $avArray[1000000][10]
Const $PROCESS_DUP_HANDLE = 0x40
const $DUPLICATE_SAME_ACCESS = 0x2
Local $types[40]
Local $ObjType=DllStructCreate($tag_OBJECT_TYPE)
While 1
if $m=$count Then ExitLoop
$avArray[$i][0]=DllStructGetData($SysHnd, "UniqueProcessId")
$avArray[$i][1]=DllStructGetData($SysHnd, "CreatorBackTraceIndex")
if not $avArray[$i][1] Then $avArray[$i][1]=""
$avArray[$i][2]=DllStructGetData($SysHnd, "ObjectTypeIndex")
$avArray[$i][3]=DllStructGetData($SysHnd, "HandleAttributes")
if not $avArray[$i][3] Then $avArray[$i][3]=""
$avArray[$i][4]=ptr(DllStructGetData($SysHnd, "HandleValue"))
$avArray[$i][5]=DllStructGetData($SysHnd, "Object")
$avArray[$i][6]=DllStructGetData($SysHnd, "GrantedAccess")
$hProcSource=_WinAPI_OpenProcess(0x1f0fff, 0, $avarray[$i][0])
$hProcDest=_WinAPI_OpenProcess(0x1f0fff, 0, @AutoItPID)
$ret=dllcall("kernel32.dll","int","DuplicateHandle","hwnd", $hProcSource, "hwnd", $avarray[$i][4], "hwnd", $hProcDest, _
"hwnd*", 0, "int",0, "int", 0, "int", $DUPLICATE_SAME_ACCESS)
$avArray[$i][7]=$ret[4]
if not $types[$avArray[$i][2]] Then
dllcall("ntdll.dll", "int", "NtQueryObject", "hwnd", $ret[4], "int", 2, "ptr", dllstructgetptr($ObjType, 1), _
"int" ,DllStructGetSize($ObjType), "int*", 0)
$buffer=DllStructCreate("wchar[256]", DllStructGetData($ObjType, "Name"))
$avArray[$i][8]=DllStructGetData($buffer, 1)
$types[$avArray[$i][2]]=$avArray[$i][8]
Else
$avArray[$i][8]=$types[$avArray[$i][2]]
EndIf
; Try to filter out NAMED PIPES to not deadlock. Writing a driver to get names would be best. I'm researching...
if $avArray[$i][2]=28 Then
if $avArray[$i][6]=0x00120189 Then
$avArray[$i][9]=" NAMED PIPES ??? - DANGER OF DEADLOCK - SKIPPED ..."
$m+=1
$i+=1
$SysHnd=DllStructCreate($tag_SYSTEM_HANDLE_INFO, $SysHnd_ptr+$SysHnd_Size*$m)
Continueloop
EndIf
if $avArray[$i][6]=0x00100000 Then
$avArray[$i][9]=" NAMED PIPES ??? - DANGER OF DEADLOCK - SKIPPED ..."
$m+=1
$i+=1
$SysHnd=DllStructCreate($tag_SYSTEM_HANDLE_INFO, $SysHnd_ptr+$SysHnd_Size*$m)
Continueloop
EndIf
if $avArray[$i][6]=0x0012019F Then
if $avArray[$i][3]<2 Then
$avArray[$i][9]=" NAMED PIPES ??? - DANGER OF DEADLOCK - SKIPPED ..."
$m+=1
$i+=1
$SysHnd=DllStructCreate($tag_SYSTEM_HANDLE_INFO, $SysHnd_ptr+$SysHnd_Size*$m)
Continueloop
EndIf
EndIf
EndIf
;~ if $avArray[$i][0]<>1452 Then ; single out one PID
;~ $m+=1
;~ ;$i+=1
;~ $SysHnd=DllStructCreate($tag_SYSTEM_HANDLE_INFO, $SysHnd_ptr+$SysHnd_Size*$m)
;~ Continueloop
;~ EndIf
; Still checking which accesrights deadlock - Consolewrite...
ConsoleWrite($avArray[$i][6] & " " & $avArray[$i][2] & " " & $avArray[$i][0] & $avArray[$i][8] & " " & @LF)
Switch $avArray[$i][2]
Case 5
$ret1=dllcall("kernel32.dll", "int", "GetProcessId", "hwnd", $ret[4])
$avArray[$i][9]=$ret1[0]
Case Else
if not $avArray[$i][9] Then
$ObjType=DllStructCreate($tag_OBJECT_TYPE)
dllcall("ntdll.dll", "int", "NtQueryObject", "hwnd", $ret[4], "int", 1, "ptr", dllstructgetptr($ObjType, 1), _
"int" ,DllStructGetSize($ObjType), "int*", 0)
$buffer=DllStructCreate("wchar[256]", DllStructGetData($ObjType, "Name"))
$avArray[$i][9]=DllStructGetData($buffer, 1)
if not $avArray[$i][9] Then $avArray[$i][9]=""
EndIf
EndSwitch
_WinAPI_CloseHandle($hProcSource)
_WinAPI_CloseHandle($hProcDest)
$i+=1
$m+=1
$SysHnd=DllStructCreate($tag_SYSTEM_HANDLE_INFO, $SysHnd_ptr+$SysHnd_Size*$m)
ContinueLoop
WEnd
Redim $avArray[$i][10]
Return $avArray
EndFunc
; #######################
; ####################### Thanks to wraithdu!
Func _GetPrivilege_SEDEBUG()
Local $tagLUIDANDATTRIB = "int64 Luid;dword Attributes"
Local $count = 1
Local $tagTOKENPRIVILEGES = "dword PrivilegeCount;byte LUIDandATTRIB[" & $count * 12 & "]" ; count of LUID structs * sizeof LUID struct
Local $TOKEN_ADJUST_PRIVILEGES = 0x20
Local $call = DllCall("advapi32.dll", "int", "OpenProcessToken", "ptr", _WinAPI_GetCurrentProcess(), "dword", $TOKEN_ADJUST_PRIVILEGES, "ptr*", "")
Local $hToken = $call[3]
$call = DllCall("advapi32.dll", "int", "LookupPrivilegeValue", "str", Chr(0), "str", "SeDebugPrivilege", "int64*", "")
;msgbox(0,"",$call[3] & " " & _WinAPI_GetLastErrorMessage())
Local $iLuid = $call[3]
Local $TP = DllStructCreate($tagTOKENPRIVILEGES)
Local $LUID = DllStructCreate($tagLUIDANDATTRIB, DllStructGetPtr($TP, "LUIDandATTRIB"))
DllStructSetData($TP, "PrivilegeCount", $count)
DllStructSetData($LUID, "Luid", $iLuid)
DllStructSetData($LUID, "Attributes", $SE_PRIVILEGE_ENABLED)
$call = DllCall("advapi32.dll", "int", "AdjustTokenPrivileges", "ptr", $hToken, "int", 0, "ptr", DllStructGetPtr($TP), "dword", 0, "ptr", Chr(0), "ptr", Chr(0))
Return ($call[0] <> 0) ; $call[0] <> 0 is success
EndFunc ;==>_GetPrivilege_SEDEBUG
|