我剛剛開始使用 Windows RPC 編程。我正在嘗試通過ms-even6與 Windows 事件日志系統進行互動。Microsoft 在其教程中沒有介紹安全的 RPC 呼叫,我在任何地方都找不到太多關于它的資訊。到目前為止,我有以下代碼......
#include <stdlib.h>
#include <iostream>
#include <ctype.h>
#include "ms-even6_h.h"
#include <windows.h>
#pragma comment(lib, "rpcrt4.lib")
int main()
{
RPC_STATUS status;
RPC_WSTR pszUuid = NULL;
RPC_WSTR pszProtocolSequence = reinterpret_cast<RPC_WSTR>(const_cast<PWSTR>(L"ncacn_np"));
RPC_WSTR pszNetworkAddress = NULL;
RPC_WSTR pszEndpoint = reinterpret_cast<RPC_WSTR>(const_cast<PWSTR>(L"\\pipe\\eventlog"));
RPC_WSTR pszOptions = NULL;
RPC_WSTR pszStringBinding = NULL;
unsigned long ulCode;
status = RpcStringBindingCompose(pszUuid,
pszProtocolSequence,
pszNetworkAddress,
pszEndpoint,
pszOptions,
&pszStringBinding);
if (status) {
std::cerr << "[-] RpcStringBindingCompose failed [" << status << "]" << std::endl;
exit(status);
}
status = RpcBindingFromStringBinding(pszStringBinding, &client_IfHandle);
if (status) {
std::cerr << "[-] RpcBindingFromStringBinding failed [" << status << "]" << std::endl;
exit(status);
}
status = RpcBindingSetAuthInfo(client_IfHandle,
0,
RPC_C_AUTHN_LEVEL_PKT_INTEGRITY,
RPC_C_AUTHN_WINNT,
0,
0
);
if (status) {
std::cerr << "[-] RpcBindingSetAuthInfo failed [" << status << "]" << std::endl;
exit(status);
}
RpcTryExcept // This block always throw an Access Denied runtime exception
{
EvtRpcVariantList props;
status = EvtRpcGetChannelConfig(L"Application", 0, &props);
if (status)
{
std::cerr << "[-] EvtRpcGetChannelConfig failed [" << status << "]" << std::endl;
exit(status);
}
std::cout << "[ ] EvtRpcGetChannelConfig worked!" << std::endl;
}
RpcExcept(1)
{
ulCode = RpcExceptionCode();
printf("Runtime reported exception 0x%lx = %ld\n", ulCode, ulCode);
}
RpcEndExcept
status = RpcStringFree(&pszStringBinding);
if (status) {
std::cerr << "[-] RpcStringFree failed [" << status << "]" << std::endl;
exit(status);
}
status = RpcBindingFree(&client_IfHandle);
if (status) {
std::cerr << "[-] RpcBindingFree failed [" << status << "]" << std::endl;
exit(status);
}
std::cout << "[ ] Done." << std::endl;
return 0;
}
/******************************************************/
/* MIDL allocate and free */
/******************************************************/
void __RPC_FAR* __RPC_USER midl_user_allocate(size_t len)
{
return (malloc(len));
}
void __RPC_USER midl_user_free(void __RPC_FAR* ptr)
{
free(ptr);
}
我嘗試呼叫不同的 RPC 函式,它們都拋出拒絕訪問例外。我的客戶端行程以本地管理員身份運行,并且我的目標是我的本地計算機。
關于我在這里做錯了什么有什么想法嗎?
感謝所有幫助!
#更新
查看windows_protocols ms-even6后,我將協議序列更改為ncacn_ip_tcp。現在ACCESS DENIED錯誤似乎消失了,但現在我得到一個運行時報告的例外 0x6f7 = 1783 (The stub received bad data.) error。
/* file: helloc.c */
#include <stdlib.h>
#include <iostream>
#include <ctype.h>
#include "ms-even6_h.h"
#include <windows.h>
#include <thread>
#pragma comment(lib, "rpcrt4.lib")
int main()
{
RPC_STATUS status;
RPC_WSTR pszUuid = NULL;
RPC_WSTR pszProtocolSequence = reinterpret_cast<RPC_WSTR>(const_cast<PWSTR>(L"ncacn_ip_tcp"));
RPC_WSTR pszNetworkAddress = reinterpret_cast<RPC_WSTR>(const_cast<PWSTR>(L"localhost"));
RPC_WSTR pszEndpoint = NULL;
RPC_WSTR pszOptions = NULL;
RPC_WSTR pszStringBinding = NULL;
unsigned long ulCode;
status = RpcStringBindingCompose(pszUuid,
pszProtocolSequence,
pszNetworkAddress,
pszEndpoint,
pszOptions,
&pszStringBinding);
if (status) {
std::cerr << "[-] RpcStringBindingCompose failed [" << status << "]" << std::endl;
exit(status);
}
status = RpcBindingFromStringBinding(pszStringBinding, &client_IfHandle);
if (status) {
std::cerr << "[-] RpcBindingFromStringBinding failed [" << status << "]" << std::endl;
exit(status);
}
status = RpcEpResolveBinding(
client_IfHandle,
IEventService_v1_0_c_ifspec
);
if (status) {
std::cerr << "[-] RpcEpResolveBinding failed [" << status << "]" << std::endl;
exit(status);
}
status = RpcBindingSetAuthInfo(client_IfHandle,
0,
RPC_C_AUTHN_LEVEL_PKT_INTEGRITY,
RPC_C_AUTHN_WINNT,
0,
0
);
if (status) {
std::cerr << "[-] RpcBindingSetAuthInfo failed [" << status << "]" << std::endl;
exit(status);
}
RpcTryExcept
{
EvtRpcVariantList props;
status = EvtRpcGetChannelConfig(L"Application", (INT32)0, &props);
if (status)
{
std::cerr << "[-] EvtRpcGetChannelConfig failed [" << status << "]" << std::endl;
exit(status);
}
std::cout << "[ ] EvtRpcGetChannelConfig worked!" << std::endl;
}
RpcExcept(1)
{
ulCode = RpcExceptionCode();
std::cerr << "[-] Runtime reported exception 0x"
<< std::hex << ulCode
<< " = "
<< std::dec << ulCode
<< std::endl;
}
RpcEndExcept
status = RpcStringFree(&pszStringBinding);
if (status) {
std::cerr << "[-] RpcStringFree failed [" << status << "]" << std::endl;
exit(status);
}
status = RpcBindingFree(&client_IfHandle);
if (status) {
std::cerr << "[-] RpcBindingFree failed [" << status << "]" << std::endl;
exit(status);
}
std::cout << "[ ] Done." << std::endl;
return 0;
}
/******************************************************/
/* MIDL allocate and free */
/******************************************************/
void __RPC_FAR* __RPC_USER midl_user_allocate(size_t len)
{
return (malloc(len));
}
void __RPC_USER midl_user_free(void __RPC_FAR* ptr)
{
free(ptr);
}
uj5u.com熱心網友回復:
查看了windows_protocols ms-even6后,我將協議序列改為ncacn_ip_tcp。一旦完成,訪問拒絕錯誤就消失了,但我仍然無法呼叫遠程程序。應用程式總是會失敗,運行時報告例外 0x6f7 = 1783(存根收到錯誤資料。)或運行時報告例外 0x6c6 = 1734(陣列邊界無效。)。
原因?未初始化的指標!一旦我正確初始化了我的指標和 DWORD,一切正常!
這是一個作業示例。
/* file: helloc.c */
#include <stdlib.h>
#include <iostream>
#include <ctype.h>
#include <windows.h>
#include "ms-even6_h.h"
#pragma comment(lib, "rpcrt4.lib")
int main()
{
RPC_STATUS status;
RPC_WSTR pszUuid = NULL;
RPC_WSTR pszProtocolSequence = reinterpret_cast<RPC_WSTR>(const_cast<PWSTR>(L"ncacn_ip_tcp"));
RPC_WSTR pszNetworkAddress = reinterpret_cast<RPC_WSTR>(const_cast<PWSTR>(L"localhost"));
RPC_WSTR pszEndpoint = NULL;
RPC_WSTR pszOptions = NULL;
RPC_WSTR pszStringBinding = NULL;
unsigned long ulCode;
status = RpcStringBindingCompose(pszUuid,
pszProtocolSequence,
pszNetworkAddress,
pszEndpoint,
pszOptions,
&pszStringBinding);
if (status) {
std::cerr << "[-] RpcStringBindingCompose failed [" << status << "]" << std::endl;
exit(status);
}
status = RpcBindingFromStringBinding(pszStringBinding, &client_IfHandle);
if (status) {
std::cerr << "[-] RpcBindingFromStringBinding failed [" << status << "]" << std::endl;
exit(status);
}
status = RpcEpResolveBinding(
client_IfHandle,
IEventService_v1_0_c_ifspec
);
if (status) {
std::cerr << "[-] RpcEpResolveBinding failed [" << status << "]" << std::endl;
exit(status);
}
status = RpcBindingSetAuthInfo(client_IfHandle,
0,
RPC_C_AUTHN_LEVEL_PKT_INTEGRITY,
RPC_C_AUTHN_WINNT,
0,
0
);
if (status) {
std::cerr << "[-] RpcBindingSetAuthInfo failed [" << status << "]" << std::endl;
exit(status);
}
RpcTryExcept
{
PCONTEXT_HANDLE_LOG_HANDLE hLog;
RpcInfo error;
status = EvtRpcOpenLogHandle(L"Application", 1, &hLog, &error);
if (status)
{
std::cerr << "[-] EvtRpcOpenLogHandle failed [" << status << "]" << std::endl;
exit(status);
}
std::cout << "[ ] EvtRpcOpenLogHandle worked!" << std::endl;
status = EvtRpcClose(&hLog);
if (status)
{
std::cerr << "[-] EvtRpcClose failed [" << status << "]" << std::endl;
exit(status);
}
std::cout << "[ ] EvtRpcClose worked!" << std::endl;
EvtRpcVariantList props;
props.count = 0;
props.props = NULL;
status = EvtRpcGetChannelConfig(L"Application", 0, &props);
if (status)
{
std::cerr << "[-] EvtRpcGetChannelConfig failed [" << status << "]" << std::endl;
exit(status);
}
std::cout << "[ ] EvtRpcGetChannelConfig worked!" << std::endl;
for (size_t i = 0; i < props.count; i )
{
std::cout << "\tChannelConfig[" << i << "]:"
<< "\n\t\ttype: " << props.props[i].type
<< std::endl;
}
PCONTEXT_HANDLE_REMOTE_SUBSCRIPTION hSub;
PCONTEXT_HANDLE_OPERATION_CONTROL hOpCtrl;
DWORD dwQueryChannelInfoSize = 0;
EvtRpcQueryChannelInfo* queryChannelInfo = NULL;
status = EvtRpcRegisterRemoteSubscription(
L"Application",
L"*",
NULL,
0x00000002 | 0x00001000,
&hSub,
&hOpCtrl,
&dwQueryChannelInfoSize,
&queryChannelInfo,
&error);
if (status)
{
std::cerr << "[-] EvtRpcRegisterRemoteSubscription failed [" << status << "]" << std::endl;
exit(status);
}
std::cout << "[ ] EvtRpcRegisterRemoteSubscription worked!" << std::endl;
std::cout << "\tdwQueryChannelInfoSize: " << dwQueryChannelInfoSize << std::endl;
for (size_t i = 0; i < dwQueryChannelInfoSize; i )
{
std::wcout << L"\tqueryChannelInfo[" << i << "]: "
<< queryChannelInfo[i].name
<< L" ("
<< queryChannelInfo[i].status
<< L")"
<< std::endl;
}
DWORD numRequestedRecords = 1;
DWORD flags = 0;
DWORD numActualRecords = 0;
DWORD* eventDataIndices = NULL;
DWORD* eventDataSizes = NULL;
DWORD resultBufferSize = 0;
BYTE* resultBuffer = NULL;
status = EvtRpcRemoteSubscriptionNextAsync(
hSub,
numRequestedRecords,
flags,
&numActualRecords,
&eventDataIndices,
&eventDataSizes,
&resultBufferSize,
&resultBuffer
);
if (status)
{
std::cerr << "[-] EvtRpcRemoteSubscriptionNextAsync failed [" << status << "]" << std::endl;
exit(status);
}
std::cout << "[ ] EvtRpcRemoteSubscriptionNextAsync worked!" << std::endl;
}
RpcExcept(1)
{
ulCode = RpcExceptionCode();
std::cerr << "[-] Runtime reported exception 0x"
<< std::hex << ulCode
<< " = "
<< std::dec << ulCode
<< std::endl;
}
RpcEndExcept
status = RpcStringFree(&pszStringBinding);
if (status) {
std::cerr << "[-] RpcStringFree failed [" << status << "]" << std::endl;
exit(status);
}
status = RpcBindingFree(&client_IfHandle);
if (status) {
std::cerr << "[-] RpcBindingFree failed [" << status << "]" << std::endl;
exit(status);
}
std::cout << "[ ] Done." << std::endl;
return 0;
}
/******************************************************/
/* MIDL allocate and free */
/******************************************************/
void __RPC_FAR* __RPC_USER midl_user_allocate(size_t len)
{
return (malloc(len));
}
void __RPC_USER midl_user_free(void __RPC_FAR* ptr)
{
free(ptr);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/510435.html
標籤:C C视窗温纳皮RPC
下一篇:單選按鈕,兩個按鈕都被選中
