-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathpowerShellProcessParser.ts
More file actions
58 lines (52 loc) · 2.43 KB
/
powerShellProcessParser.ts
File metadata and controls
58 lines (52 loc) · 2.43 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// due to wmic has been deprecated create this file to replace wmicProcessParser.ts
'use strict';
import { IAttachItem, ProcessListCommand } from './types';
export namespace PowerShellProcessParser {
export const powerShellCommand: ProcessListCommand = {
command: 'powershell',
args: [
'-Command',
'$processes = if (Get-Command Get-CimInstance -ErrorAction SilentlyContinue) { Get-CimInstance Win32_Process } else { Get-WmiObject Win32_Process }; \
$processes | % { @{ name = $_.Name; commandLine = $_.CommandLine; processId = $_.ProcessId } } | ConvertTo-Json',
], // Get-WmiObject For the legacy compatibility
};
//for unit test with Get-WmiObject
export const powerShellWithoutCimCommand: ProcessListCommand = {
command: 'powershell',
args: [
'-Command',
'$processes = if (Get-Command NotExistCommand-That-Will-Never-Exist -ErrorAction SilentlyContinue) { Get-CimInstance Win32_Process } else { Get-WmiObject Win32_Process }; \
$processes | % { @{ name = $_.Name; commandLine = $_.CommandLine; processId = $_.ProcessId } } | ConvertTo-Json',
],
};
export function parseProcesses(processes: string): IAttachItem[] {
const processesArray = JSON.parse(processes);
const processEntries: IAttachItem[] = [];
for (const process of processesArray) {
if (!process.processId) {
continue;
}
const entry: IAttachItem = {
label: process.name || '',
processName: process.name || '',
description: String(process.processId),
id: String(process.processId),
detail: '',
commandLine: '',
};
if (process.commandLine) {
const dosDevicePrefix = '\\??\\'; // DOS device prefix, see https://reverseengineering.stackexchange.com/a/15178
let commandLine = process.commandLine;
if (commandLine.startsWith(dosDevicePrefix)) {
commandLine = commandLine.slice(dosDevicePrefix.length);
}
entry.detail = commandLine;
entry.commandLine = commandLine;
}
processEntries.push(entry);
}
return processEntries;
}
}