Skip to content

Commit 7d254a0

Browse files
authored
Merge pull request #2127 from Ahm3dRN/windows-commandline-information
Closes #2126 Implement NtQueryInformationProcess to get commandline Windows 8.1+
2 parents 4dab2b9 + fbf8dd1 commit 7d254a0

2 files changed

Lines changed: 183 additions & 3 deletions

File tree

‎process/process_windows.go‎

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package process
66
import (
77
"bufio"
88
"context"
9+
"encoding/binary"
910
"errors"
1011
"fmt"
1112
"io"
@@ -1204,14 +1205,82 @@ func (p *processReader) Read(buf []byte) (int, error) {
12041205

12051206
func getProcessCommandLine(pid int32) (string, error) {
12061207
h, err := windows.OpenProcess(processQueryInformation|windows.PROCESS_VM_READ, false, uint32(pid))
1207-
if errors.Is(err, windows.ERROR_ACCESS_DENIED) || errors.Is(err, windows.ERROR_INVALID_PARAMETER) {
1208+
if err == nil {
1209+
defer syscall.CloseHandle(syscall.Handle(h))
1210+
1211+
cmdLine, pebErr := getProcessCommandLinePEB(h)
1212+
if pebErr == nil {
1213+
return cmdLine, nil
1214+
}
1215+
1216+
// PEB read failed even though the handle opened. VM_READ may
1217+
// have been granted but ineffective against this process's
1218+
// protection driver. Fall back to the native query on the same handle.
1219+
if cmdLine, err := getProcessCommandLineNative(h, pid); err == nil {
1220+
return cmdLine, nil
1221+
}
1222+
return "", pebErr
1223+
}
1224+
1225+
if errors.Is(err, windows.ERROR_INVALID_PARAMETER) {
12081226
return "", nil
12091227
}
1210-
if err != nil {
1228+
if !errors.Is(err, windows.ERROR_ACCESS_DENIED) {
12111229
return "", err
12121230
}
1213-
defer syscall.CloseHandle(syscall.Handle(h))
12141231

1232+
// fallback in case it's a genuine PPL process, where
1233+
// PROCESS_VM_READ itself is denied at OpenProcess time. Retry with
1234+
// just PROCESS_QUERY_LIMITED_INFORMATION, which PPL still grants.
1235+
lh, lerr := windows.OpenProcess(processQueryInformation, false, uint32(pid))
1236+
if lerr != nil {
1237+
return "", nil
1238+
}
1239+
defer syscall.CloseHandle(syscall.Handle(lh))
1240+
cmdLine, _ := getProcessCommandLineNative(lh, pid)
1241+
return cmdLine, nil
1242+
}
1243+
1244+
func getProcessCommandLineNative(handle windows.Handle, pid int32) (string, error) {
1245+
var returnLength uint32
1246+
// first call is to get the actual returnLength
1247+
err := windows.NtQueryInformationProcess(
1248+
handle,
1249+
windows.ProcessCommandLineInformation,
1250+
nil,
1251+
0,
1252+
&returnLength,
1253+
)
1254+
if err != nil &&
1255+
!errors.Is(err, windows.STATUS_INFO_LENGTH_MISMATCH) &&
1256+
!errors.Is(err, windows.STATUS_BUFFER_OVERFLOW) &&
1257+
!errors.Is(err, windows.STATUS_BUFFER_TOO_SMALL) {
1258+
return "", fmt.Errorf("querying command line size for pid %d: %w", pid, err)
1259+
}
1260+
if returnLength == 0 {
1261+
return "", fmt.Errorf("process %d returned zero-length command line info", pid)
1262+
}
1263+
1264+
// second call: actually fetch it into a correctly-sized buffer
1265+
buf := make([]byte, returnLength)
1266+
err = windows.NtQueryInformationProcess(
1267+
handle,
1268+
windows.ProcessCommandLineInformation,
1269+
unsafe.Pointer(&buf[0]),
1270+
returnLength,
1271+
&returnLength,
1272+
)
1273+
if err != nil {
1274+
return "", fmt.Errorf("reading command line for pid %d: %w", pid, err)
1275+
}
1276+
cmdLine, err := parseCommandLineInformation(buf)
1277+
if err != nil {
1278+
return "", fmt.Errorf("parsing command line for pid %d: %w", pid, err)
1279+
}
1280+
return cmdLine, nil
1281+
}
1282+
1283+
func getProcessCommandLinePEB(h windows.Handle) (string, error) {
12151284
procIs32Bits := is32BitProcess(h)
12161285

12171286
if procIs32Bits {
@@ -1258,3 +1327,19 @@ func convertUTF16ToString(src []byte) string {
12581327
}
12591328
return syscall.UTF16ToString(codePoints)
12601329
}
1330+
1331+
func parseCommandLineInformation(buf []byte) (string, error) {
1332+
if len(buf) < 2 {
1333+
return "", errors.New("command line buffer too small to read length")
1334+
}
1335+
length := binary.LittleEndian.Uint16(buf[0:2])
1336+
if length == 0 {
1337+
return "", nil
1338+
}
1339+
strOffset := int(unsafe.Sizeof(windows.NTUnicodeString{}))
1340+
strEnd := strOffset + int(length)
1341+
if strEnd > len(buf) {
1342+
return "", errors.New("command line length exceeds buffer size")
1343+
}
1344+
return convertUTF16ToString(buf[strOffset:strEnd]), nil
1345+
}

‎process/process_windows_test.go‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
package process
3+
4+
import (
5+
"encoding/binary"
6+
"os"
7+
"syscall"
8+
"testing"
9+
"unicode/utf16"
10+
"unsafe"
11+
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
14+
"golang.org/x/sys/windows"
15+
)
16+
17+
func TestParseCommandLineInformation(t *testing.T) {
18+
ntUnicodeStringHeaderSize := int(unsafe.Offsetof(windows.NTUnicodeString{}.Buffer)) +
19+
int(unsafe.Sizeof((*uint16)(nil)))
20+
21+
buildCommandLineInfoBuf := func(s string) []byte {
22+
utf16Data := utf16.Encode([]rune(s))
23+
dataBytes := make([]byte, len(utf16Data)*2)
24+
for i, u := range utf16Data {
25+
binary.LittleEndian.PutUint16(dataBytes[i*2:], u)
26+
}
27+
buf := make([]byte, ntUnicodeStringHeaderSize+len(dataBytes))
28+
binary.LittleEndian.PutUint16(buf[0:2], uint16(len(dataBytes)))
29+
copy(buf[ntUnicodeStringHeaderSize:], dataBytes)
30+
return buf
31+
}
32+
tests := []struct {
33+
name string
34+
buf []byte
35+
expect string
36+
expectErr bool
37+
}{
38+
{
39+
name: "valid command line",
40+
buf: buildCommandLineInfoBuf("notepad.exe file.txt"),
41+
expect: "notepad.exe file.txt",
42+
},
43+
{
44+
name: "empty command line",
45+
buf: buildCommandLineInfoBuf(""),
46+
expect: "",
47+
},
48+
{
49+
name: "buffer too small for length field",
50+
buf: []byte{0x01},
51+
expectErr: true,
52+
},
53+
{
54+
name: "truncated buffer (length claims more than available)",
55+
buf: func() []byte {
56+
b := buildCommandLineInfoBuf("some command")
57+
return b[:len(b)-4]
58+
}(),
59+
expectErr: true,
60+
},
61+
}
62+
63+
for _, tt := range tests {
64+
t.Run(tt.name, func(t *testing.T) {
65+
got, err := parseCommandLineInformation(tt.buf)
66+
if tt.expectErr {
67+
assert.Error(t, err)
68+
return
69+
}
70+
require.NoError(t, err)
71+
assert.Equal(t, tt.expect, got)
72+
})
73+
}
74+
}
75+
76+
// TestGetProcessCommandLineNativeMatchesPEB verifies that the native
77+
// NtQueryInformationProcess path and the PEB read path return the same
78+
// command line for the current process.
79+
func TestGetProcessCommandLineNativeMatchesPEB(t *testing.T) {
80+
pid := int32(os.Getpid())
81+
h, err := windows.OpenProcess(
82+
windows.PROCESS_QUERY_INFORMATION|windows.PROCESS_VM_READ,
83+
false,
84+
uint32(pid),
85+
)
86+
require.NoError(t, err)
87+
defer syscall.CloseHandle(syscall.Handle(h))
88+
89+
pebCmdline, err := getProcessCommandLinePEB(h)
90+
require.NoError(t, err)
91+
92+
nativeCmdline, err := getProcessCommandLineNative(h, pid)
93+
require.NoError(t, err)
94+
assert.Equal(t, pebCmdline, nativeCmdline)
95+
}

0 commit comments

Comments
 (0)