Skip to content

Commit c0f7e78

Browse files
authored
Better eva - feat(evaluate): support check setuid files in path (#67)
* perf(capabilities): red color for add caps * perf(eva): a nice head 2 of title * feat(evaluate): support check setuid files in path * perf(eva): move call function from cli/parse to evaluate/evaluate Co-authored-by: neargle <[email protected]>
1 parent cace22d commit c0f7e78

File tree

8 files changed

+183
-47
lines changed

8 files changed

+183
-47
lines changed

conf/evaluate_conf.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ var LinuxCommandChecklist = []string{
6161
"ruby",
6262
}
6363

64+
var DefaultPathEnv = []string{
65+
"/usr/local/sbin",
66+
"/usr/local/bin",
67+
"/usr/sbin",
68+
"/usr/bin",
69+
"/sbin",
70+
"/bin",
71+
"/usr/games",
72+
"/usr/local/games",
73+
"/snap/bin",
74+
}
75+
6476
// match ENV to find useful service
6577
var SensitiveEnvRegex = "(?i)\\bssh_|k8s|kubernetes|docker|gopath"
6678

pkg/cli/banner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var BannerVersion = fmt.Sprintf("%s %s", "CDK Version(GitCommit):", GitCommit)
3434
var BannerHeader = fmt.Sprintf(`%s
3535
%s
3636
Zero-dependency cloudnative k8s/docker/serverless penetration toolkit by cdxy & neargle
37-
Find tutorial, configuration and use-case in https://github.com/cdk-team/CDK/wiki
37+
Find tutorial, configuration and use-case in https://github.com/cdk-team/CDK/
3838
`, util.GreenBold.Sprint(BannerTitle), BannerVersion)
3939

4040
var BannerContainerTpl = BannerHeader + `

pkg/cli/parse.go

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package cli
1919
import (
2020
"fmt"
2121

22-
"github.com/cdk-team/CDK/conf"
2322
"github.com/cdk-team/CDK/pkg/evaluate"
2423
"github.com/cdk-team/CDK/pkg/plugin"
2524
"github.com/cdk-team/CDK/pkg/tool/dockerd_api"
@@ -72,49 +71,11 @@ func ParseCDKMain() bool {
7271
// fix #37 https://github.com/cdk-team/CDK/issues/37
7372
if ok.(bool) || fok.(bool) {
7473

75-
fmt.Printf("\n[Information Gathering - System Info]\n")
76-
evaluate.BasicSysInfo()
77-
78-
fmt.Printf("\n[Information Gathering - Services]\n")
79-
evaluate.SearchSensitiveEnv()
80-
evaluate.SearchSensitiveService()
81-
82-
fmt.Printf("\n[Information Gathering - Commands and Capabilities]\n")
83-
evaluate.SearchAvailableCommands()
84-
evaluate.GetProcCapabilities()
85-
86-
fmt.Printf("\n[Information Gathering - Mounts]\n")
87-
evaluate.MountEscape()
88-
89-
fmt.Printf("\n[Information Gathering - Net Namespace]\n")
90-
evaluate.CheckNetNamespace()
91-
92-
fmt.Printf("\n[Information Gathering - Sysctl Variables]\n")
93-
evaluate.CheckRouteLocalNetworkValue()
94-
95-
fmt.Printf("\n[Discovery - K8s API Server]\n")
96-
evaluate.CheckK8sAnonymousLogin()
97-
98-
fmt.Printf("\n[Discovery - K8s Service Account]\n")
99-
evaluate.CheckPrivilegedK8sServiceAccount(conf.K8sSATokenDefaultPath)
100-
101-
fmt.Printf("\n[Discovery - Cloud Provider Metadata API]\n")
102-
evaluate.CheckCloudMetadataAPI()
103-
104-
fmt.Printf("\n[Information Gathering - DNS-Based Service Discovery]\n")
105-
evaluate.DNSBasedServiceDiscovery()
74+
fmt.Printf(BannerHeader)
75+
evaluate.CallBasics()
10676

10777
if Args["--full"].(bool) {
108-
109-
fmt.Printf("\n[Information Gathering - Sensitive Files]\n")
110-
evaluate.SearchLocalFilePath()
111-
112-
fmt.Printf("\n[Information Gathering - ASLR]\n")
113-
evaluate.ASLR()
114-
115-
fmt.Printf("\n[Information Gathering - Cgroups]\n")
116-
evaluate.DumpCgroup()
117-
78+
evaluate.CallAddedFunc()
11879
}
11980
return true
12081
}

pkg/evaluate/available_linux_capabilities.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ func GetProcCapabilities() bool {
5959
caps, err := capability.CapHexParser(capStr)
6060

6161
fmt.Printf("\tCap decode: 0x%s = %s\n", capStr, capability.CapListToString(caps))
62-
fmt.Printf("\tAdd capability list: %s\n", capability.CapListToString(getAddCaps(caps)))
62+
63+
addCaps := getAddCaps(caps)
64+
if len(addCaps) > 0 {
65+
util.RedBold.Printf("\tAdded capability list: %s\n", capability.CapListToString(addCaps))
66+
}
6367

6468
if err != nil {
6569
log.Printf("[-] capability.CapHexParser: %v\n", err)

pkg/evaluate/evaluate.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Copyright 2022 The Authors of https://github.com/CDK-TEAM/CDK .
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package evaluate
18+
19+
import (
20+
"github.com/cdk-team/CDK/pkg/util"
21+
"github.com/cdk-team/CDK/conf"
22+
)
23+
24+
// CallBasics is a function to call basic functions
25+
func CallBasics() {
26+
util.PrintH2("Information Gathering - System Info")
27+
BasicSysInfo()
28+
FindSidFiles()
29+
30+
util.PrintH2("Information Gathering - Services")
31+
SearchSensitiveEnv()
32+
SearchSensitiveService()
33+
34+
util.PrintH2("Information Gathering - Commands and Capabilities")
35+
SearchAvailableCommands()
36+
GetProcCapabilities()
37+
38+
util.PrintH2("Information Gathering - Mounts")
39+
MountEscape()
40+
41+
util.PrintH2("Information Gathering - Net Namespace")
42+
CheckNetNamespace()
43+
44+
util.PrintH2("Information Gathering - Sysctl Variables")
45+
CheckRouteLocalNetworkValue()
46+
47+
util.PrintH2("Discovery - K8s API Server")
48+
CheckK8sAnonymousLogin()
49+
50+
util.PrintH2("Discovery - K8s Service Account")
51+
CheckPrivilegedK8sServiceAccount(conf.K8sSATokenDefaultPath)
52+
53+
util.PrintH2("Discovery - Cloud Provider Metadata API")
54+
CheckCloudMetadataAPI()
55+
56+
util.PrintH2("Information Gathering - DNS-Based Service Discovery")
57+
DNSBasedServiceDiscovery()
58+
}
59+
60+
func CallAddedFunc() {
61+
util.PrintH2("Information Gathering - Sensitive Files")
62+
SearchLocalFilePath()
63+
64+
util.PrintH2("Information Gathering - ASLR")
65+
ASLR()
66+
67+
util.PrintH2("Information Gathering - Cgroups")
68+
DumpCgroup()
69+
}

pkg/evaluate/evaluate_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ func TestDumpCgroup(t *testing.T) {
2626
fmt.Printf("\n[Information Gathering - Cgroups]\n")
2727
DumpCgroup()
2828
}
29+
30+
func TestFindSidFiles(t *testing.T) {
31+
fmt.Printf("\n[Information Gathering - SIDs]\n")
32+
FindSidFiles()
33+
}

pkg/evaluate/system_info.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/*
32
Copyright 2022 The Authors of https://github.com/CDK-TEAM/CDK .
43
@@ -18,11 +17,14 @@ limitations under the License.
1817
package evaluate
1918

2019
import (
21-
"github.com/shirou/gopsutil/v3/host"
20+
"io/ioutil"
2221
"log"
2322
"os"
2423
"os/user"
25-
"io/ioutil"
24+
25+
"github.com/cdk-team/CDK/conf"
26+
"github.com/cdk-team/CDK/pkg/util"
27+
"github.com/shirou/gopsutil/v3/host"
2628
)
2729

2830
func BasicSysInfo() {
@@ -57,6 +59,40 @@ func BasicSysInfo() {
5759

5860
}
5961

62+
// FindSidFiles such as run `find /bin/. -perm -4000 -type f `
63+
func FindSidFiles() {
64+
65+
var setuidfiles []string
66+
67+
for _, dir := range conf.DefaultPathEnv {
68+
files, err := ioutil.ReadDir(dir)
69+
if err != nil {
70+
continue
71+
}
72+
73+
for _, file := range files {
74+
// check setuid bit
75+
if file.Mode() & os.ModeSetuid != 0 {
76+
setuidfiles = append(setuidfiles, dir + "/" + file.Name())
77+
}
78+
79+
// check capabilites, like getcap -r /bin
80+
// TODO: check capabilites
81+
}
82+
}
83+
84+
if len(setuidfiles) > 0 {
85+
util.PrintItemKey("Setuid files found:", false)
86+
for _, file := range setuidfiles {
87+
util.PrintItemValue(file, true)
88+
}
89+
}
90+
}
91+
92+
// CommandAllow check command allow to run
93+
func CommandAllow() {
94+
}
95+
6096
func ASLR() {
6197
// ASLR off: /proc/sys/kernel/randomize_va_space = 0
6298
var ASLRSetting = "/proc/sys/kernel/randomize_va_space"
@@ -75,3 +111,4 @@ func ASLR() {
75111
}
76112

77113
}
114+

pkg/util/output.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright 2022 The Authors of https://github.com/CDK-TEAM/CDK .
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"fmt"
21+
"log"
22+
)
23+
24+
const Colorful = true
25+
26+
// fmt.Printf(util.GreenBold.Sprint("\n[Information Gathering - System Info]\n"))
27+
func PrintH2(title string) {
28+
fmt.Printf(BlueBold.Sprint("\n[ ") + GreenBold.Sprint(title) + BlueBold.Sprint(" ]\n"))
29+
}
30+
31+
func PrintItemKey(key string, color bool) {
32+
key = key + "\n"
33+
if color {
34+
log.Printf(YellowBold.Sprint(key))
35+
} else {
36+
log.Printf(key)
37+
}
38+
}
39+
40+
func PrintItemValue(value string, color bool) {
41+
value = "\t" + value + "\n"
42+
if color {
43+
fmt.Printf(RedBold.Sprint(value))
44+
} else {
45+
fmt.Printf(value)
46+
}
47+
}
48+

0 commit comments

Comments
 (0)