forked from yuk7/wsldl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparenttl.h
72 lines (63 loc) · 1.69 KB
/
parenttl.h
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
/*
* Copyright (c) 2020 yuk7
* Author: yuk7 <yukx00@gmail.com>
*
* Released under the MIT license
* http://opensource.org/licenses/mit-license.php
*/
#ifndef PARENTTL_H_
#define PARENTTL_H_
#include <stdbool.h>
#include <stdio.h>
#include <windows.h>
#include <tlhelp32.h>
#ifdef __cplusplus
extern "C" {
#endif
#define PROC_LIST_SIZE 20000
bool isParentCmdLine()
{
HANDLE hSnapshot;
DWORD procs[PROC_LIST_SIZE];
int procsCnt = 0;
DWORD procID = GetCurrentProcessId();
if((hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0)) != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
if(Process32First(hSnapshot,&pe32))
{
do
{
if ((strcmp(pe32.szExeFile, "cmd.exe") == 0) ||
(strcmp(pe32.szExeFile, "powershell.exe") == 0) ||
(strcmp(pe32.szExeFile, "wsl.exe") == 0) )
{
if(procsCnt < PROC_LIST_SIZE)
{
procs[procsCnt] = pe32.th32ProcessID;
procsCnt++;
}
}
if (pe32.th32ProcessID == procID)
{
for(int i = 0; i < procsCnt; i++)
{
if(procs[i] == pe32.th32ParentProcessID)
{
return true;
}
}
return false;
}
}
while(Process32Next(hSnapshot, &pe32));
}
CloseHandle(hSnapshot);
}
return false;
}
#ifdef __cplusplus
}
#endif
#endif