Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Chmod, Chown, Chtimes, and uid:gid to Ls #1170

Merged
merged 7 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions client/command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2143,6 +2143,70 @@ func BindCommands(con *console.SliverConsoleClient) {
},
})

con.App.AddCommand(&grumble.Command{
Name: consts.ChmodStr,
Help: "Change permissions on a file or directory",
LongHelp: help.GetHelpFor([]string{consts.ChmodStr}),
Flags: func(f *grumble.Flags) {
f.Bool("r", "recursive", false, "recursively change permissions on files")
f.Int("t", "timeout", defaultTimeout, "command timeout in seconds")
},
Args: func(a *grumble.Args) {
a.String("path", "path to the file to remove")
a.String("mode", "file permissions in octal, e.g. 0644")
},
Run: func(ctx *grumble.Context) error {
con.Println()
filesystem.ChmodCmd(ctx, con)
con.Println()
return nil
},
HelpGroup: consts.SliverHelpGroup,
})

con.App.AddCommand(&grumble.Command{
Name: consts.ChownStr,
Help: "Change owner on a file or directory",
LongHelp: help.GetHelpFor([]string{consts.ChownStr}),
Flags: func(f *grumble.Flags) {
f.Bool("r", "recursive", false, "recursively change permissions on files")
f.Int("t", "timeout", defaultTimeout, "command timeout in seconds")
},
Args: func(a *grumble.Args) {
a.String("path", "path to the file to remove")
a.String("uid", "User, e.g. root")
a.String("gid", "Group, e.g. root")
},
Run: func(ctx *grumble.Context) error {
con.Println()
filesystem.ChownCmd(ctx, con)
con.Println()
return nil
},
HelpGroup: consts.SliverHelpGroup,
})

con.App.AddCommand(&grumble.Command{
Name: consts.ChtimesStr,
Help: "Change access and modification times on a file (timestomp)",
LongHelp: help.GetHelpFor([]string{consts.ChtimesStr}),
Flags: func(f *grumble.Flags) {
f.Int("t", "timeout", defaultTimeout, "command timeout in seconds")
},
Args: func(a *grumble.Args) {
a.String("path", "path to the file to remove")
a.String("atime", "Last accessed time in DateTime format, i.e. 2006-01-02 15:04:05")
a.String("mtime", "Last modified time in DateTime format, i.e. 2006-01-02 15:04:05")
},
Run: func(ctx *grumble.Context) error {
con.Println()
filesystem.ChtimesCmd(ctx, con)
con.Println()
return nil
},
HelpGroup: consts.SliverHelpGroup,
})

// [ Websites ] ---------------------------------------------

websitesCmd := &grumble.Command{
Expand Down
84 changes: 84 additions & 0 deletions client/command/filesystem/chmod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package filesystem

/*
Copyright (C) 2023 b0yd

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import (
"context"

"github.com/bishopfox/sliver/client/console"
"github.com/bishopfox/sliver/protobuf/clientpb"
"github.com/bishopfox/sliver/protobuf/sliverpb"
"google.golang.org/protobuf/proto"

"github.com/desertbit/grumble"
)

// ChmodCmd - Change the permissions of a file on the remote file system
func ChmodCmd(ctx *grumble.Context, con *console.SliverConsoleClient) {
session, beacon := con.ActiveTarget.GetInteractive()
if session == nil && beacon == nil {
return
}

filePath := ctx.Args.String("path")

if filePath == "" {
con.PrintErrorf("Missing parameter: file or directory name\n")
return
}

fileMode := ctx.Args.String("mode")

if fileMode == "" {
con.PrintErrorf("Missing parameter: file permissions (mode)\n")
return
}

chmod, err := con.Rpc.Chmod(context.Background(), &sliverpb.ChmodReq{
Request: con.ActiveTarget.Request(ctx),
Path: filePath,
FileMode: fileMode,
Recursive: ctx.Flags.Bool("recursive"),
})
if err != nil {
con.PrintErrorf("%s\n", err)
return
}
if chmod.Response != nil && chmod.Response.Async {
con.AddBeaconCallback(chmod.Response.TaskID, func(task *clientpb.BeaconTask) {
err = proto.Unmarshal(task.Response, chmod)
if err != nil {
con.PrintErrorf("Failed to decode response %s\n", err)
return
}
PrintChmod(chmod, con)
})
con.PrintAsyncResponse(chmod.Response)
} else {
PrintChmod(chmod, con)
}
}

// PrintChmod - Print the chmod response
func PrintChmod(chmod *sliverpb.Chmod, con *console.SliverConsoleClient) {
if chmod.Response != nil && chmod.Response.Err != "" {
con.PrintErrorf("%s\n", chmod.Response.Err)
return
}
con.PrintInfof("%s\n", chmod.Path)
}
92 changes: 92 additions & 0 deletions client/command/filesystem/chown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package filesystem

/*
Copyright (C) 2023 b0yd

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import (
"context"

"github.com/bishopfox/sliver/client/console"
"github.com/bishopfox/sliver/protobuf/clientpb"
"github.com/bishopfox/sliver/protobuf/sliverpb"
"google.golang.org/protobuf/proto"

"github.com/desertbit/grumble"
)

// ChownCmd - Change the owner of a file on the remote file system
func ChownCmd(ctx *grumble.Context, con *console.SliverConsoleClient) {
session, beacon := con.ActiveTarget.GetInteractive()
if session == nil && beacon == nil {
return
}

filePath := ctx.Args.String("path")

if filePath == "" {
con.PrintErrorf("Missing parameter: file or directory name\n")
return
}

uid := ctx.Args.String("uid")

if uid == "" {
con.PrintErrorf("Missing parameter: user id\n")
return
}

gid := ctx.Args.String("gid")

if gid == "" {
con.PrintErrorf("Missing parameter: group id\n")
return
}

chown, err := con.Rpc.Chown(context.Background(), &sliverpb.ChownReq{
Request: con.ActiveTarget.Request(ctx),
Path: filePath,
Uid: uid,
Gid: gid,
Recursive: ctx.Flags.Bool("recursive"),
})
if err != nil {
con.PrintErrorf("%s\n", err)
return
}
if chown.Response != nil && chown.Response.Async {
con.AddBeaconCallback(chown.Response.TaskID, func(task *clientpb.BeaconTask) {
err = proto.Unmarshal(task.Response, chown)
if err != nil {
con.PrintErrorf("Failed to decode response %s\n", err)
return
}
PrintChown(chown, con)
})
con.PrintAsyncResponse(chown.Response)
} else {
PrintChown(chown, con)
}
}

// PrintChown - Print the chown response
func PrintChown(chown *sliverpb.Chown, con *console.SliverConsoleClient) {
if chown.Response != nil && chown.Response.Err != "" {
con.PrintErrorf("%s\n", chown.Response.Err)
return
}
con.PrintInfof("%s\n", chown.Path)
}
107 changes: 107 additions & 0 deletions client/command/filesystem/chtimes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package filesystem

/*
Copyright (C) 2023 b0yd

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import (
"context"
"time"

"github.com/bishopfox/sliver/client/console"
"github.com/bishopfox/sliver/protobuf/clientpb"
"github.com/bishopfox/sliver/protobuf/sliverpb"
"google.golang.org/protobuf/proto"

"github.com/desertbit/grumble"
)

// ChtimesCmd - Change the access and modified time of a file on the remote file system
func ChtimesCmd(ctx *grumble.Context, con *console.SliverConsoleClient) {
session, beacon := con.ActiveTarget.GetInteractive()
if session == nil && beacon == nil {
return
}
// DateTime layout (https://pkg.go.dev/time)
layout := "2006-01-02 15:04:05"
filePath := ctx.Args.String("path")

if filePath == "" {
con.PrintErrorf("Missing parameter: file or directory name\n")
return
}

atime := ctx.Args.String("atime")

if atime == "" {
con.PrintErrorf("Missing parameter: Last accessed time id\n")
return
}

t_a, err := time.Parse(layout, atime)
if err != nil {
con.PrintErrorf("%s\n", err)
return
}
unixAtime := t_a.Unix()

mtime := ctx.Args.String("mtime")

if mtime == "" {
con.PrintErrorf("Missing parameter: Last modified time id\n")
return
}

t_b, err := time.Parse(layout, mtime)
if err != nil {
con.PrintErrorf("%s\n", err)
return
}
unixMtime := t_b.Unix()

chtimes, err := con.Rpc.Chtimes(context.Background(), &sliverpb.ChtimesReq{
Request: con.ActiveTarget.Request(ctx),
Path: filePath,
ATime: unixAtime,
MTime: unixMtime,
})
if err != nil {
con.PrintErrorf("%s\n", err)
return
}
if chtimes.Response != nil && chtimes.Response.Async {
con.AddBeaconCallback(chtimes.Response.TaskID, func(task *clientpb.BeaconTask) {
err = proto.Unmarshal(task.Response, chtimes)
if err != nil {
con.PrintErrorf("Failed to decode response %s\n", err)
return
}
PrintChtimes(chtimes, con)
})
con.PrintAsyncResponse(chtimes.Response)
} else {
PrintChtimes(chtimes, con)
}
}

// PrintChtimes - Print the Chtimes response
func PrintChtimes(chtimes *sliverpb.Chtimes, con *console.SliverConsoleClient) {
if chtimes.Response != nil && chtimes.Response.Err != "" {
con.PrintErrorf("%s\n", chtimes.Response.Err)
return
}
con.PrintInfof("%s\n", chtimes.Path)
}
15 changes: 12 additions & 3 deletions client/command/filesystem/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,22 @@ func PrintLs(ls *sliverpb.Ls, flags grumble.FlagMap, con *console.SliverConsoleC
implantLocation := time.FixedZone(ls.Timezone, int(ls.TimezoneOffset))
modTime = modTime.In(implantLocation)

owner := ""
if fileInfo.Uid != "" {
owner = fileInfo.Uid
}
if fileInfo.Gid != "" {
owner = owner + ":" + fileInfo.Gid + "\t"
}

if fileInfo.IsDir {
fmt.Fprintf(table, "%s\t%s\t<dir>\t%s\n", fileInfo.Mode, fileInfo.Name, modTime.Format(time.RubyDate))
fmt.Fprintf(table, "%s\t%s%s\t<dir>\t%s\n", fileInfo.Mode, owner, fileInfo.Name, modTime.Format(time.RubyDate))
} else if fileInfo.Link != "" {
fmt.Fprintf(table, "%s\t%s -> %s\t%s\t%s\n", fileInfo.Mode, fileInfo.Name, fileInfo.Link, util.ByteCountBinary(fileInfo.Size), modTime.Format(time.RubyDate))
fmt.Fprintf(table, "%s\t%s%s -> %s\t%s\t%s\n", fileInfo.Mode, owner, fileInfo.Name, fileInfo.Link, util.ByteCountBinary(fileInfo.Size), modTime.Format(time.RubyDate))
} else {
fmt.Fprintf(table, "%s\t%s\t%s\t%s\n", fileInfo.Mode, fileInfo.Name, util.ByteCountBinary(fileInfo.Size), modTime.Format(time.RubyDate))
fmt.Fprintf(table, "%s\t%s%s\t%s\t%s\n", fileInfo.Mode, owner, fileInfo.Name, util.ByteCountBinary(fileInfo.Size), modTime.Format(time.RubyDate))
}

}
table.Flush()
con.Printf("%s\n", outputBuf.String())
Expand Down
Loading