@@ -3,6 +3,7 @@ package ls
3
3
import (
4
4
// Standard
5
5
"encoding/json"
6
+ "github.com/MythicAgents/poseidon/Payload_Type/poseidon/agent_code/pkg/utils/functions"
6
7
"os"
7
8
"os/user"
8
9
"path/filepath"
@@ -57,32 +58,28 @@ func GetPermission(finfo os.FileInfo) structs.FilePermission {
57
58
return perms
58
59
}
59
60
60
- func Run (task structs.Task ) {
61
- msg := task .NewResponse ()
62
- args := structs.FileBrowserArguments {}
63
- err := json .Unmarshal ([]byte (task .Params ), & args )
64
- if err != nil {
65
- msg .SetError (err .Error ())
66
- task .Job .SendResponses <- msg
67
- return
68
- }
61
+ func ProcessPath (path string ) (* structs.FileBrowser , error ) {
69
62
var e structs.FileBrowser
70
63
e .SetAsUserOutput = true
71
- fixedPath := args .Path
64
+ e .Files = make ([]structs.FileData , 0 )
65
+ fixedPath := path
72
66
if strings .HasPrefix (fixedPath , "~/" ) {
73
67
dirname , _ := os .UserHomeDir ()
74
68
fixedPath = filepath .Join (dirname , fixedPath [2 :])
75
69
}
76
70
abspath , _ := filepath .Abs (fixedPath )
71
+ //abspath, _ = filepath.EvalSymlinks(abspath)
77
72
dirInfo , err := os .Stat (abspath )
73
+ filepath .EvalSymlinks (abspath )
78
74
if err != nil {
79
- msg .SetError (err .Error ())
80
- task .Job .SendResponses <- msg
81
- return
75
+ return & e , err
82
76
}
83
77
e .IsFile = ! dirInfo .IsDir ()
84
-
85
78
e .Permissions = GetPermission (dirInfo )
79
+ symlinkPath , _ := filepath .EvalSymlinks (abspath )
80
+ if symlinkPath != abspath {
81
+ e .Permissions .Symlink = symlinkPath
82
+ }
86
83
e .Filename = dirInfo .Name ()
87
84
e .ParentPath = filepath .Dir (abspath )
88
85
if strings .Compare (e .ParentPath , e .Filename ) == 0 {
@@ -101,13 +98,9 @@ func Run(task structs.Task) {
101
98
if dirInfo .IsDir () {
102
99
files , err := os .ReadDir (abspath )
103
100
if err != nil {
104
- msg .SetError (err .Error ())
105
101
e .Success = false
106
- msg .FileBrowser = & e
107
- task .Job .SendResponses <- msg
108
- return
102
+ return & e , err
109
103
}
110
-
111
104
fileEntries := make ([]structs.FileData , len (files ))
112
105
for i := 0 ; i < len (files ); i ++ {
113
106
fileEntries [i ].IsFile = ! files [i ].IsDir ()
@@ -123,7 +116,11 @@ func Run(task structs.Task) {
123
116
}
124
117
fileEntries [i ].Name = files [i ].Name ()
125
118
fileEntries [i ].FullName = filepath .Join (abspath , files [i ].Name ())
126
- at , err := atime .Stat (abspath )
119
+ symlinkPath , _ = filepath .EvalSymlinks (fileEntries [i ].FullName )
120
+ if symlinkPath != fileEntries [i ].FullName {
121
+ fileEntries [i ].Permissions .Symlink = symlinkPath
122
+ }
123
+ at , err = atime .Stat (fileEntries [i ].FullName )
127
124
if err != nil {
128
125
fileEntries [i ].LastAccess = 0
129
126
} else {
@@ -135,10 +132,55 @@ func Run(task structs.Task) {
135
132
fileEntries := make ([]structs.FileData , 0 )
136
133
e .Files = fileEntries
137
134
}
135
+ return & e , nil
136
+ }
137
+ func Run (task structs.Task ) {
138
+ args := structs.FileBrowserArguments {}
139
+ err := json .Unmarshal ([]byte (task .Params ), & args )
140
+ if err != nil {
141
+ msg := task .NewResponse ()
142
+ msg .SetError (err .Error ())
143
+ task .Job .SendResponses <- msg
144
+ return
145
+ }
146
+ if args .Depth == 0 {
147
+ args .Depth = 1
148
+ }
149
+ if args .Host != "" {
150
+ if strings .ToLower (args .Host ) != strings .ToLower (functions .GetHostname ()) {
151
+ if args .Host != "127.0.0.1" && args .Host != "localhost" {
152
+ msg := task .NewResponse ()
153
+ msg .SetError ("can't currently list files on remote hosts" )
154
+ task .Job .SendResponses <- msg
155
+ return
156
+ }
157
+ }
158
+ }
159
+ var paths = []string {args .Path }
160
+ for args .Depth >= 1 {
161
+ nextPaths := []string {}
162
+ for _ , path := range paths {
163
+ msg := task .NewResponse ()
164
+ fb , err := ProcessPath (path )
165
+ if err != nil {
166
+ msg .SetError (err .Error ())
167
+ }
168
+ msg .FileBrowser = fb
169
+ task .Job .SendResponses <- msg
170
+ if fb == nil {
171
+ continue
172
+ }
173
+ for _ , child := range fb .Files {
174
+ if ! child .IsFile {
175
+ nextPaths = append (nextPaths , child .FullName )
176
+ }
177
+ }
178
+ }
179
+ paths = nextPaths
180
+ args .Depth --
181
+ }
182
+ msg := task .NewResponse ()
138
183
msg .Completed = true
139
- msg .FileBrowser = & e
140
- //temp, _ := json.Marshal(msg.FileBrowser)
141
- //msg.UserOutput = string(temp)
142
184
task .Job .SendResponses <- msg
143
185
return
144
186
}
0 commit comments