Skip to content

Commit 21fbef8

Browse files
committed
chore: update gonut check pe type
1 parent 5c9cc4c commit 21fbef8

5 files changed

Lines changed: 23 additions & 27 deletions

File tree

external/gonut/donut.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,17 @@ package gonut
22

33
import (
44
"os"
5-
"path/filepath"
65
"strings"
76
)
87

98
// DonutShellcodeFromFile 从给定的 PE 文件生成 Donut shellcode
109
func DonutShellcodeFromFile(filePath string, arch string, params string) (data []byte, err error) {
11-
pe, err := os.ReadFile(filePath)
10+
bin, err := os.ReadFile(filePath)
1211
if err != nil {
1312
return
1413
}
15-
ext, err := GetExtension(filePath)
16-
if err != nil {
17-
return
18-
}
19-
20-
baseName := filepath.Base(filePath)
21-
if filepath.Ext(baseName) == "" && ext != "" {
22-
baseName = baseName + ext
23-
}
2414

25-
return DonutShellcodeFromPE(baseName, pe, arch, params, false, true)
15+
return DonutShellcodeFromPE(filePath, bin, arch, params, false, true)
2616
}
2717

2818
// DonutShellcodeFromPE 从给定的 PE 数据生成 Donut shellcode

external/gonut/utils.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ func UnsafeStructToBytes(ptr any) []byte {
7171
}
7272

7373
func GetExtension(filepath string) (string, error) {
74-
7574
file, err := os.Open(filepath)
7675
if err != nil {
7776
return "", fmt.Errorf("failed to open file %s: %w", filepath, err)

helper/consts/implant.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ const (
126126

127127
const (
128128
ELF = ".elf"
129-
PE = ".pe"
130129
DLL = ".dll"
131130
PEFile = ".exe"
132131
ShellcodeFile = ".bin"

helper/utils/formatutils/formatter.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"github.com/chainreactors/malice-network/helper/cryptography"
88
"github.com/chainreactors/malice-network/helper/encoders"
99
"github.com/chainreactors/malice-network/helper/proto/client/clientpb"
10-
"os"
11-
"path/filepath"
1210
"strings"
1311
)
1412

@@ -156,17 +154,12 @@ func ConvertArtifact(artifact *clientpb.Artifact, format string) (*clientpb.Arti
156154
artifact.Format = format
157155
return artifact, nil
158156
}
159-
filename := filepath.Join(encoders.UUID())
160-
if err := os.WriteFile(filename, artifact.Bin, 0644); err != nil {
161-
return nil, err
162-
}
163-
shellcode, err := SRDIArtifact(filename, artifact.Platform, artifact.Arch, artifact.Type == consts.CommandBuildPulse)
157+
158+
shellcode, err := SRDIArtifact(artifact.Bin, artifact.Platform, artifact.Arch, artifact.Type == consts.CommandBuildPulse)
164159
if err != nil {
165160
return nil, fmt.Errorf("failed to convert: %s", err)
166161
}
167-
if err := os.Remove(filename); err != nil {
168-
return nil, fmt.Errorf("failed to remove file: %s", err)
169-
}
162+
170163
convert, err := Convert(shellcode, format)
171164
if err != nil {
172165
return nil, err

helper/utils/formatutils/srdi.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package formatutils
33
import (
44
"fmt"
55
"github.com/chainreactors/logs"
6+
"github.com/chainreactors/malice-network/helper/consts"
7+
"github.com/chainreactors/malice-network/helper/encoders"
8+
"github.com/chainreactors/malice-network/helper/utils/pe"
69
"github.com/wabzsy/gonut"
710
"os"
811
"os/exec"
@@ -52,10 +55,22 @@ func ObjcopyPulse(path, platform, arch string) ([]byte, error) {
5255
return bin, nil
5356
}
5457

55-
func SRDIArtifact(path, platform, arch string, useobjcopy bool) ([]byte, error) {
58+
func SRDIArtifact(bin []byte, platform, arch string, useobjcopy bool) ([]byte, error) {
5659
if useobjcopy {
57-
return ObjcopyPulse(path, platform, arch)
60+
filename := filepath.Join(encoders.UUID())
61+
if err := os.WriteFile(filename, bin, 0644); err != nil {
62+
return nil, err
63+
}
64+
defer os.Remove(filename)
65+
return ObjcopyPulse(filename, platform, arch)
5866
} else {
59-
return gonut.DonutShellcodeFromFile(path, arch, "")
67+
switch pe.CheckPEType(bin) {
68+
case consts.DLLFile:
69+
return gonut.DonutShellcodeFromPE("bin"+consts.DLL, bin, arch, "", false, true)
70+
case consts.EXEFile:
71+
return gonut.DonutShellcodeFromPE("bin"+consts.PEFile, bin, arch, "", false, true)
72+
default:
73+
return nil, fmt.Errorf("unsupported file type")
74+
}
6075
}
6176
}

0 commit comments

Comments
 (0)