{"id":104669,"date":"2021-01-06T07:00:00","date_gmt":"2021-01-06T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=104669"},"modified":"2021-01-06T21:53:57","modified_gmt":"2021-01-07T05:53:57","slug":"20210106-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20210106-00\/?p=104669","title":{"rendered":"How can I tell whether my process is running as SYSTEM?"},"content":{"rendered":"<p>A customer wanted to know how to check whether the current process is running as the SYSTEM account. They proposed this algorithm:<\/p>\n<pre>\/\/ Code in italics is wrong\r\n<i>bool IsCurrentProcessRunningAsSystem()\r\n{\r\n DWORD session_id;\r\n return ProcessIdToSessionId(GetCurrentProcessId(), &amp;session_id) &amp;&amp;\r\n        session_id == 0;\r\n}<\/i>\r\n<\/pre>\n<p>This algorithm is flawed both for the possibility of false positives as well as false negatives.<\/p>\n<p>You can see this for yourself by opening Task Manager:<\/p>\n<table style=\"background-color: white; font-family: Segoe UI, Tahoma, Arial, sans-serif; font-size: 10pt; border: solid 1px black; border-collapse: separate;\" border=\"0\" cellspacing=\"0\">\n<tbody>\n<tr style=\"background-coloir: #fcfcfc;\">\n<td style=\"border: solid 1px #e1e1e1; padding-left: 2pt; padding-right: 1em;\" nowrap=\"nowrap\">Name<\/td>\n<td style=\"border: solid 1px #e1e1e1; padding-left: 2pt; padding-right: 1em; width: 10em;\" nowrap=\"nowrap\">User name<\/td>\n<td style=\"border: solid 1px #e1e1e1; padding-left: 2pt; padding-right: 1em;\" nowrap=\"nowrap\">Session ID<\/td>\n<\/tr>\n<tr>\n<td style=\"padding-left: 2pt; padding-right: 1em;\" nowrap=\"nowrap\">LogonUI.exe<\/td>\n<td style=\"padding-left: 2pt; padding-right: 1em;\" nowrap=\"nowrap\">SYSTEM<\/td>\n<td style=\"padding-left: 2pt; padding-right: 1em;\" nowrap=\"nowrap\">3<\/td>\n<\/tr>\n<tr>\n<td style=\"padding-left: 2pt; padding-right: 1em;\" nowrap=\"nowrap\">winlogon.exe<\/td>\n<td style=\"padding-left: 2pt; padding-right: 1em;\" nowrap=\"nowrap\">SYSTEM<\/td>\n<td style=\"padding-left: 2pt; padding-right: 1em;\" nowrap=\"nowrap\">3<\/td>\n<\/tr>\n<tr>\n<td style=\"padding-left: 2pt; padding-right: 1em;\" nowrap=\"nowrap\">fontdrvhost.exe<\/td>\n<td style=\"padding-left: 2pt; padding-right: 1em;\" nowrap=\"nowrap\">UMFD-0<\/td>\n<td style=\"padding-left: 2pt; padding-right: 1em;\" nowrap=\"nowrap\">0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>We have some processes running as SYSTEM which aren&#8217;t in session zero. And we have a process in session zero that is not running as SYSTEM.<\/p>\n<p>If you want to know whether you are running as SYSTEM, check your token to see whether it represents the SYSTEM user.<\/p>\n<p>I&#8217;m going to use <a href=\"https:\/\/github.com\/Microsoft\/wil\"> wil<\/a> as my RAII library.<\/p>\n<pre>#include &lt;<a href=\"https:\/\/github.com\/microsoft\/wil\/blob\/master\/include\/wil\/token_helpers.h\">wil\/token_helpers.h<\/a>&gt;\r\n\r\nbool DoesTokenRepresentSid(HANDLE token, WELL_KNOWN_SID_TYPE type)\r\n{\r\n \/\/ maps to GetTokenInformation(token, TokenUser, ...);\r\n auto user = wil::get_token_information&lt;TOKEN_USER&gt;(token);\r\n return !!IsWellKnownSid(user-&gt;User.Sid, type);\r\n}\r\n\r\nbool IsCurrentProcessRunningAsSystem()\r\n{\r\n return DoesTokenRepresentSid(GetCurrentProcessToken(),\r\n                              WinLocalSystemSid);\r\n}\r\n\r\nbool IsCurrentThreadRunningAsSystem()\r\n{\r\n return DoesTokenRepresentSid(GetCurrentThreadEffectiveToken(),\r\n                              WinLocalSystemSid);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Check your token.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-104669","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Check your token.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104669","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=104669"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104669\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=104669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=104669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=104669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}