从缓冲区中读取数组元素的句柄信息

时间:2014-09-19 22:35:18

标签: firefox-addon ctypes jsctypes

我提取句柄,结构如下:

var SYSTEM_HANDLE_TABLE_ENTRY_INFO = new ctypes.StructType('SYSTEM_HANDLE_TABLE_ENTRY_INFO', [
    {'UniqueProcessId': ctypes.unsigned_long},
    {'CreatorBackTraceIndex': ctypes.unsigned_long},
    {'ObjectTypeIndex': ctypes.unsigned_long},
    {'HandleAttributes': ctypes.unsigned_long},
    {'HandleValue': ctypes.unsigned_long},
    {'Object': ctypes.unsigned_long},
    {'GrantedAccess': ctypes.unsigned_long}
]);


var SYSTEM_HANDLE_INFORMATION = new ctypes.StructType('SYSTEM_HANDLE_INFORMATION', [
    {'NumberOfHandles': ctypes.unsigned_long},
    {'Handles': ctypes.ArrayType(SYSTEM_HANDLE_TABLE_ENTRY_INFO, 5)}
]);

所以现在我成功获取SYSTEM_HANDLE_INFORMATION这不是问题,我这样做:

var proc = ctypes.cast(buffer.addressOfElement(0), SYSTEM_HANDLE_INFORMATION.ptr).contents;

这成功地填充了proc

{NumberOfHandles: 59000, Handles:array}

array中的每个元素都是一个看起来像上面SYSTEM_HANDLE_TABLE_ENTRY_INFO结构的对象。

现在我的问题是:我想访问UniqueProcessId中每个元素的array,我想这样做proc.Handles[i].UniqueProcessId,但这会崩溃。

所以我想我必须做上面的行var proc这样的事情:

var procHandleAtIndex = ctypes.cast(buffer.addressOfElement(i), SYSTEM_HANDLE_TABLE_ENTRY_INFO.ptr).contents;

现在这可以避免崩溃但是它读错了。换句话说,我通过给i=0 proc.Handles[0].UniqueProcessId != procHandleAtIndex.UniqueProcessId进行了测试。当我通过procHandleAtIndex.UniqueProcessId == proc.NumberOfHandles时,结果是i=0

请帮助我从Handles

中阅读buffer中的elemet

谢谢,我不知道任何C我是一个javascript人。

参考

这是在python ctypes中成功完成的方式: https://github.com/offensive-security/exploit-database/blob/40b9adcb14b8307ea66f8b92d0371be80bbc70e4/platforms/windows/local/34272.py#L139

"""Return all the processes handles in the system atm."""
system_handle_information = SYSTEM_HANDLE_INFORMATION_EX()
size = DWORD (sizeof (system_handle_information))
while True:
    result = ntdll.NtQuerySystemInformation(
        SystemExtendedHandleInformation,
        byref(system_handle_information),
        size,
        byref(size)
    )
    result = signed_to_unsigned(result)
    if result == STATUS_SUCCESS:
        break
    elif result == STATUS_INFO_LENGTH_MISMATCH:
        size = DWORD(size.value * 4)
        resize(system_handle_information, size.value)
    else:
        raise x_file_handles("NtQuerySystemInformation", hex(result))

pHandles = cast(
    system_handle_information.Handles,
    POINTER(SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX * \
            system_handle_information.NumberOfHandles)
)
for handle in pHandles.contents:
    yield handle.UniqueProcessId, handle.HandleValue, handle.Object

0 个答案:

没有答案