How do you limit System.Diagnostics.Process HANDLE access rights in C# .NET?

Viewed 86

When using System.Diagnostics.Process in C#/.NET, the Process HANDLE you receive is PROCESS_ALL_ACCESS. This can allow an external application to steal the HANDLE and have full access rights to the target process and abusing the trust relationship.

As seen here, .NET just defaults to full access: https://referencesource.microsoft.com/#system/services/monitoring/system/diagnosticts/Process.cs,167

How can the permissions be reduced on the Process HANDLE? Is creating a new Process class using unmanaged code using calls to openprocess() how it should be done? Or reflection?

Thanks!

1 Answers

the Process HANDLE you receive is PROCESS_ALL_ACCESS. This can allow an external application to steal the HANDLE and have full access rights to the target process and abusing the trust relationship.

The problem you are describing has nothing to do with .NET let alone Process but rather Windows and whether or not your process is running elevated a concept introduced in Windows Vista. With elevated process, non-admin users and non-elevated processes may not interfere with elevated processes. Just because you request PROCESS_ALL_ACCESS does not necessarily imply you will obtain such rights. Alternatively you can use a different account (see below), a pre-Vista technique and still valid today.

How do you limit System.Diagnostics.Process HANDLE access rights in C# .NET?

All Windows processes whether they are created by .NET or not can be enumerated by other applications and a HANDLE returned for further inspection or possible manipulation.

How can the permissions be reduced on the Process HANDLE?

If you want to protect your process somewhat you can run it elevated. This then limits manipulation to admins and other elevated processes. Other non-elevated processes or regular users may see the process but that is all.

Different account

Generally speaking, one particularly effective counter measure for non-elevated processes is to run them in a different user account to the process trying to manipulate it thus limiting the audience to admins-only.

Related