Skip to content

Commit b81511f

Browse files
isaac-fletchermeta-codesync[bot]
authored andcommitted
Implement Windows superuser check (#568)
Summary: Pull Request resolved: #568 Previously, TTPForge would only log a warning when a TTP required superuser privileges on Windows but wouldn't actually enforce the requirement. This change implements proper administrator privilege checking on Windows using `golang.org/x/sys/windows` to check if the process token is elevated. The implementation uses Go build tags to provide platform-specific implementations of superuser checking. Unix-based systems continue to use `os.Geteuid() == 0` while Windows now uses token elevation checking. Reviewed By: RoboticPrism Differential Revision: D85156306
1 parent d41f160 commit b81511f

File tree

6 files changed

+70
-12
lines changed

6 files changed

+70
-12
lines changed

example-ttps/requirements/os-and-superuser.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ steps:
2020
- name: demo
2121
print_str: |
2222
If you see this string, you are executing this TTP
23-
with superuser privileges within a compabile OS/Architecture environment.
23+
with superuser privileges within a compatible OS/Architecture environment.

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/facebookincubator/ttpforge
22

3-
go 1.23.0
3+
go 1.24.0
44

55
toolchain go1.24.1
66

@@ -17,6 +17,7 @@ require (
1717
github.com/tidwall/gjson v1.17.1
1818
go.uber.org/zap v1.27.0
1919
golang.org/x/crypto v0.39.0
20+
golang.org/x/sys v0.37.0
2021
gopkg.in/yaml.v3 v3.0.1
2122
)
2223

@@ -43,7 +44,6 @@ require (
4344
github.com/yusufpapurcu/wmi v1.2.4 // indirect
4445
go.uber.org/multierr v1.11.0 // indirect
4546
golang.org/x/sync v0.15.0 // indirect
46-
golang.org/x/sys v0.33.0 // indirect
4747
golang.org/x/text v0.26.0 // indirect
4848
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
4949
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
113113
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
114114
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
115115
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
116-
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
117-
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
116+
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
117+
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
118118
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
119119
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
120120
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=

pkg/blocks/requirements.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ THE SOFTWARE.
2020
package blocks
2121

2222
import (
23-
"errors"
2423
"fmt"
25-
"os"
2624
"runtime"
2725

2826
"github.com/facebookincubator/ttpforge/pkg/checks"
@@ -93,13 +91,12 @@ func (rc *RequirementsConfig) Verify(ctx checks.VerificationContext) error {
9391

9492
// check superuser requirement
9593
if rc.ExpectSuperuser {
94+
if !isSuperuser() {
95+
return fmt.Errorf("must be running with elevated privileges to run this TTP")
96+
}
9697
if runtime.GOOS == "windows" {
97-
logging.L().Warnf("not enforcing superuser requirement because it is not supported on windows yet")
98+
logging.L().Debug("[+] Running as administrator")
9899
} else {
99-
if os.Geteuid() != 0 {
100-
err := errors.New("must be root (UID 0) to run this TTP")
101-
return err
102-
}
103100
logging.L().Debug("[+] Running as root")
104101
}
105102
}

pkg/blocks/requirements_unix.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//go:build unix
2+
3+
/*
4+
Copyright © 2023-present, Meta Platforms, Inc. and affiliates
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
20+
*/
21+
22+
package blocks
23+
24+
import "os"
25+
26+
// isSuperuser checks if the current process is running with superuser privileges
27+
// on Unix-like systems (Linux, macOS, BSD, etc.) by checking if the effective user ID is 0 (root).
28+
func isSuperuser() bool {
29+
return os.Geteuid() == 0
30+
}

pkg/blocks/requirements_windows.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//go:build windows
2+
3+
/*
4+
Copyright © 2023-present, Meta Platforms, Inc. and affiliates
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
20+
*/
21+
22+
package blocks
23+
24+
import "golang.org/x/sys/windows"
25+
26+
// isSuperuser checks if the current process is running with administrator privileges
27+
// on Windows by checking if the process token is elevated.
28+
func isSuperuser() bool {
29+
token := windows.GetCurrentProcessToken()
30+
return token.IsElevated()
31+
}

0 commit comments

Comments
 (0)