2014-06-25 32 views
1

短版本:權限錯誤當編輯的Windows審覈策略編程通過WINAPI

我試圖寫一個C++程序,使進程創建日誌記錄在Windows 8。我知道這是可能使用auditpol.exe做,但我想以編程方式進行。我的研究表明,唯一的方法就是通過Windows API命令AuditSetSystemPolicy,所以我編寫了一個調用此函數的C++程序(參見下文)。但是,該程序未能引用權限問題(錯誤代碼1314)。我以管理員身份運行Visual Studio,並且嘗試在以管理員身份運行的命令提示符下執行程序,但我仍然收到錯誤消息。

長的版本:

下面的程序需要一個GUID string describing the Process Creation Subcategory我要開始審計,並將其轉換成一個GUID結構。然後它從GUID和ULONG構造一個AUDIT_POLICY_INFORMATION結構體,描述我想要做的改變(啓用成功和失敗的日誌記錄)。最後,我把結構放入一個數組並調用AuditSetSystemPolicy函數。

// Subcategory GUID for Process Creation 
string guidstr ("{0CCE922B-69AE-11D9-BED3-505054503030}"); 

// Construct a GUID object 
GUID guid; 
HRESULT hr = CLSIDFromString(s2ws (guidstr).c_str(), (LPCLSID)&guid); 

// Check if the GUID converted correctly 
if (hr == S_OK) 
{ 
    cout << "GUID successfully converted: " << endl; 

    // Print english version of the SubCateogory GUID according to the API 
    PSTR *output = new PSTR(""); 
    bool categ_name = AuditLookupSubCategoryName(&guid, output); 
    cout << *output << endl; 
} 
else 
{ 
    cout << "GUID failed conversion" << endl; 
} 

// Create an AUDIT_POLICY_INFORMATION structure describing the desired change 
// The AuditCategoryGuid field will be ignored according to documentation 
AUDIT_POLICY_INFORMATION audit; 
audit.AuditCategoryGuid = (GUID)guid; 
audit.AuditSubCategoryGuid = (GUID)guid; 

// Turn on auditing for success and failure 
audit.AuditingInformation = 0x00000003; 

// Create an array of AUDIT_POLICY_INFORMATION change requests 
AUDIT_POLICY_INFORMATION arr[1]; 
arr[0] = audit; 

bool policyChanged = TRUE; 
policyChanged = AuditSetSystemPolicy(arr, 1); 
DWORD last_error = GetLastError(); 

// Check if the policy change succeeded or not 
if (policyChanged == TRUE) 
{ 
    cout << "Successfully set policy" << endl; 
} 
else 
{ 
    cout << "Failed to set policy. Error:" << endl;  
    cout << last_error << endl; 
} 

我使用Visual Studio Professional 2013運行此代碼,通過選擇「以管理員身份運行」啓動Visual Studio Professional 2013。這導致下面的輸出:

GUID successfully converted: 
Process Creation 
Failed to set policy. Error: 
1314 

代碼1314的意思是:「A required privilege is not held by the client.」根據AuditSetSystemPolicy文檔:「要成功調用此函數,調用者必須具有SeSecurityPrivilege權限或者審覈保障對象上AUDIT_SET_SYSTEM_POLICY訪問。 「我遵循instructions on TechNet並驗證管理員有正確的審計和安全管理。爲了更好的衡量,我還給了我的用戶這些權利,並重新啓動了計算機,以確保所做的更改得以實施。我仍然收到錯誤。

我也嘗試使用auditpol.exe手動打開進程創建註銷,運行上述代碼,然後使用auditpol.exe來驗證日誌記錄仍然關閉。我也拉起事件查看器並手動驗證沒有記錄正在發生。

回答

2

我使用了這段代碼,並且必須糾正一些小問題,但總的來說,您需要在您的過程對象上設置「SeSecuritiyPrivilege」。以管理員身份運行或系統就會給你權限設置該標誌,但你必須嘗試設置審計級別之前,你的代碼中設置:

#include <atlsecurity.h> 
... 
ATL::CAccessToken processToken; 
processToken.GetEffectiveToken(TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES); 
processToken.EnablePrivilege("SeSecuritiyPrivilege"); 

你這樣做之前嘗試設置審計級別。

+0

非常感謝您的幫助。代碼中的設置權限是我嚴重缺失的部分。 – bubblesdawn