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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
/*
* ** ZABBIX
* ** Copyright (C) 2000-2005 SIA Zabbix
* **
* ** This program is free software; you can redistribute it and/or modify
* ** it under the terms of the GNU General Public License as published by
* ** the Free Software Foundation; either version 2 of the License, or
* ** (at your option) any later version.
* **
* ** This program is distributed in the hope that it will be useful,
* ** but WITHOUT ANY WARRANTY; without even the implied warranty of
* ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* ** GNU General Public License for more details.
* **
* ** You should have received a copy of the GNU General Public License
* ** along with this program; if not, write to the Free Software
* ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* **/
#include "config.h"
#include "common.h"
#include "sysinfo.h"
static int GetProcessUsername(HANDLE hProcess, char *userName, int userNameLen) {
HANDLE tok = 0;
TOKEN_USER *ptu;
DWORD
nlen,
dlen;
char
name[300],
dom[300],
tubuf[300];
int iUse;
assert(userName);
//clean result;
*userName = '\0';
//open the processes token
if (!OpenProcessToken(hProcess,TOKEN_QUERY,&tok)) goto lbl_err;;
//get the SID of the token
ptu = (TOKEN_USER*)tubuf;
if (!GetTokenInformation(tok,(TOKEN_INFORMATION_CLASS)1,ptu,300,&nlen)) goto lbl_err;
//get the account/domain name of the SID
dlen = 300; nlen = 300;
if (!LookupAccountSid(0, ptu->User.Sid, name, &nlen, dom, &dlen, (PSID_NAME_USE)&iUse)) goto lbl_err;
nlen = min(userNameLen-1,(int)nlen);
strncpy(userName, name, nlen);
userName[nlen] = 0;
return 1;
lbl_err:
if (tok) CloseHandle(tok);
return 0;
}
int PROC_MEMORY(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result)
{ /* usage: <function name>[ <process name>, <user name>, <mode>, <command> ] */
#ifdef TODO
# error Realize function KERNEL_MAXFILES!!!
#endif /* todo */
return SYSINFO_RET_FAIL;
}
#define MAX_PROCESSES 4096
int PROC_NUM(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result)
{ /* usage: <function name>[ <process name>, <user name>] */
HANDLE hProcess;
HMODULE hMod;
DWORD procList[MAX_PROCESSES];
DWORD dwSize=0;
int
i = 0,
proccount = 0,
max_proc_cnt = 0,
proc_ok = 0,
user_ok = 0;
char
procName[MAX_PATH],
userName[MAX_PATH],
baseName[MAX_PATH],
uname[300];
if(num_param(param) > 2)
{
return SYSINFO_RET_FAIL;
}
if(get_param(param, 1, procName, MAX_PATH) != 0)
{
return SYSINFO_RET_FAIL;
}
if(get_param(param, 2, userName, MAX_PATH) != 0)
{
userName[0] = '\0';
}
EnumProcesses(procList,sizeof(DWORD)*MAX_PROCESSES,&dwSize);
for(i=0,proccount=0,max_proc_cnt=dwSize/sizeof(DWORD); i < max_proc_cnt; i++)
{
proc_ok = 0;
user_ok = 0;
hProcess=OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,FALSE,procList[i]);
if (hProcess!=NULL)
{
if (procName[0] != 0)
{
if (EnumProcessModules(hProcess,&hMod,sizeof(hMod),&dwSize))
{
GetModuleBaseName(hProcess,hMod,baseName,sizeof(baseName));
if (stricmp(baseName,procName) == 0)
{
proc_ok = 1;
}
}
} else {
proc_ok = 1;
}
if(userName[0] != '\0')
{
if(GetProcessUsername(hProcess, uname, 300))
{
if (stricmp(uname,userName) == 0)
user_ok = 1;
}
} else {
user_ok = 1;
}
if(user_ok && proc_ok) proccount++;
CloseHandle(hProcess);
}
}
SET_UI64_RESULT(result, proccount);
return SYSINFO_RET_OK;
}
|