Skip to content

Commit a7f8c3b

Browse files
authored
Merge pull request #22 from forderud/localservice
Run MyServerCs under "LocalService" account
2 parents e594240 + f6a2392 commit a7f8c3b

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

MyServerCpp/Main.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@ MyserverModule _AtlModule;
1919

2020
// EXE Entry Point
2121
int wmain(int /*argc*/, wchar_t* /*argv*/[]) {
22-
ComInitialize com(COINIT_MULTITHREADED);
22+
// initialize COM early for programmatic COM security
23+
_AtlModule.InitializeCom();
24+
25+
// Disable COM security to allow any client to connect.
26+
// WARNING: Enables non-admin clients to connect to a server running with admin privileges.
27+
HRESULT hr = CoInitializeSecurity(nullptr, -1/*auto*/, nullptr, NULL/*reserved*/,
28+
RPC_C_AUTHN_LEVEL_DEFAULT, ///<
29+
RPC_C_IMP_LEVEL_IDENTIFY, ///< allow server to identify but not impersonate client
30+
nullptr, EOAC_NONE/*capabilities*/, NULL/*reserved*/);
31+
if (FAILED(hr))
32+
abort();
2333

2434
return _AtlModule.WinMain(SW_SHOWDEFAULT);
2535
}

MyServerCs/Program.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ class Program
1313
[MTAThread] // or [STAThread]
1414
static void Main(string[] args)
1515
{
16+
// allow lower privilege clients to connect
17+
int hr = ComSecurity.CoInitializeSecurity(IntPtr.Zero, -1, IntPtr.Zero, IntPtr.Zero, RpcAuthnLevel.Default, RpcImpLevel.Identify, IntPtr.Zero, EoAuthnCap.None, IntPtr.Zero);
18+
if (hr != 0) // S_OK check
19+
throw new Exception("CoInitializeSecurity failed");
20+
1621
using var consoleTrace = new ConsoleTraceListener();
1722
Trace.Listeners.Add(consoleTrace);
1823

@@ -26,7 +31,7 @@ static void Main(string[] args)
2631
Guid typeLib = TypeLib.Register(exePath);
2732
LocalServer.Register(typeof(MyInterfaces.MyServerClass).GUID, exePath, typeLib);
2833
#if ENABLE_RUN_AS
29-
AppID.Register(typeof(MyInterfaces.MyServerClass).GUID, "Interactive User");
34+
AppID.Register(typeof(MyInterfaces.MyServerClass).GUID, "nt authority\\localservice");
3035
#endif
3136
return;
3237
}

0 commit comments

Comments
 (0)