summaryrefslogtreecommitdiff
path: root/vendor/github.com/AllenDang/w32/alpc_test.go
blob: 6d1c7d4e67f070d64768f89cedd70571301c7853 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package w32

import (
	"testing"
)

var testPortName = "\\TestAlpcPort"

var basicPortAttr = ALPC_PORT_ATTRIBUTES{
	MaxMessageLength: uint64(SHORT_MESSAGE_MAX_SIZE),
	SecurityQos: SECURITY_QUALITY_OF_SERVICE{
		Length:              SECURITY_QOS_SIZE,
		ContextTrackingMode: SECURITY_DYNAMIC_TRACKING,
		EffectiveOnly:       1,
		ImpersonationLevel:  SecurityAnonymous,
	},
	Flags:          ALPC_PORFLG_ALLOW_LPC_REQUESTS,
	DupObjectTypes: ALPC_SYNC_OBJECT_TYPE,
}

func ObjectAttributes(name string) (oa OBJECT_ATTRIBUTES, e error) {

	sd, e := InitializeSecurityDescriptor(1)
	if e != nil {
		return
	}

	e = SetSecurityDescriptorDacl(sd, nil)
	if e != nil {
		return
	}

	oa, e = InitializeObjectAttributes(name, 0, 0, sd)
	return
}

func Send(
	hPort HANDLE,
	msg *AlpcShortMessage,
	flags uint32,
	pMsgAttrs *ALPC_MESSAGE_ATTRIBUTES,
	timeout *int64,
) (e error) {

	e = NtAlpcSendWaitReceivePort(hPort, flags, msg, pMsgAttrs, nil, nil, nil, timeout)
	return

}

func Recv(
	hPort HANDLE,
	pMsg *AlpcShortMessage,
	pMsgAttrs *ALPC_MESSAGE_ATTRIBUTES,
	timeout *int64,
) (bufLen uint32, e error) {

	bufLen = uint32(pMsg.TotalLength)
	e = NtAlpcSendWaitReceivePort(hPort, 0, nil, nil, pMsg, &bufLen, pMsgAttrs, timeout)
	return

}

// Convenience method to create an ALPC port with a NULL DACL. Requires an
// absolute port name ( where / is the root of the kernel object directory )
func CreatePort(name string) (hPort HANDLE, e error) {

	oa, e := ObjectAttributes(name)
	if e != nil {
		return
	}

	hPort, e = NtAlpcCreatePort(&oa, &basicPortAttr)

	return
}

func ConnectPort(serverName, clientName string, pConnMsg *AlpcShortMessage) (hPort HANDLE, e error) {

	oa, e := InitializeObjectAttributes(clientName, 0, 0, nil)
	if e != nil {
		return
	}

	hPort, e = NtAlpcConnectPort(
		serverName,
		&oa,
		&basicPortAttr,
		ALPC_PORFLG_ALLOW_LPC_REQUESTS,
		nil,
		pConnMsg,
		nil,
		nil,
		nil,
		nil,
	)

	return
}

func Accept(
	hSrv HANDLE,
	context *AlpcPortContext,
	pConnReq *AlpcShortMessage,
	accept bool,
) (hPort HANDLE, e error) {

	oa, _ := InitializeObjectAttributes("", 0, 0, nil)

	var accepted uintptr
	if accept {
		accepted++
	}

	hPort, e = NtAlpcAcceptConnectPort(
		hSrv,
		0,
		&oa,
		&basicPortAttr,
		context,
		pConnReq,
		nil,
		accepted,
	)

	return
}

func TestNtAlpcCreatePort(t *testing.T) {

	hPort, err := CreatePort(testPortName)

	if err != nil {
		t.Errorf("failed to create ALPC port %v: %v", testPortName, err)
	} else {
		t.Logf("[OK] Created ALPC port %v with handle 0x%x", testPortName, hPort)
	}
}