1d9075d4185092376a459e2bd5f2c232c6c1e9a9
[ossec-hids.git] / src / rootcheck / win-process.c
1 /* @(#) $Id$ */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All right reserved.
5  *
6  * This program is a free software; you can redistribute it
7  * and/or modify it under the terms of the GNU General Public
8  * License (version 2) as published by the FSF - Free Software
9  * Foundation
10  */
11
12 #ifdef WIN32
13 #include "shared.h"
14 #include "rootcheck.h"
15
16 #include <tlhelp32.h>
17 #include <psapi.h>
18
19
20 /* Using: http://support.microsoft.com/kb/q131065/ as ref for debug priv */
21
22
23 /* Set Debug privilege */
24 int os_win32_setdebugpriv(HANDLE h, int en)
25 {
26     TOKEN_PRIVILEGES tp;
27     TOKEN_PRIVILEGES tpPrevious;
28     LUID luid;
29     DWORD cbPrevious = sizeof(TOKEN_PRIVILEGES);
30
31     if(!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid)) 
32     {
33         return(0);
34     }
35
36     tp.PrivilegeCount = 1;
37     tp.Privileges[0].Luid = luid;
38     tp.Privileges[0].Attributes = 0;
39
40     AdjustTokenPrivileges(h, FALSE, &tp, sizeof(TOKEN_PRIVILEGES),
41                           &tpPrevious,&cbPrevious);
42
43     if(GetLastError() != ERROR_SUCCESS)
44     {
45         return(0);
46     }
47
48     tpPrevious.PrivilegeCount = 1;
49     tpPrevious.Privileges[0].Luid = luid;
50
51
52     /* If en is set to true, we enable the privilege */
53     if(en) 
54     {
55         tpPrevious.Privileges[0].Attributes |= (SE_PRIVILEGE_ENABLED);
56     }
57     else 
58     {
59         tpPrevious.Privileges[0].Attributes ^= (SE_PRIVILEGE_ENABLED &
60                 tpPrevious.Privileges[0].Attributes);
61     }
62
63     AdjustTokenPrivileges(h, FALSE, &tpPrevious, cbPrevious, NULL, NULL);
64     if(GetLastError() != ERROR_SUCCESS)
65     {
66         return(0);
67     }
68
69     return(1);
70 }
71
72
73
74 /* os_get_process_list: Get list of win32 processes */
75 void *os_get_process_list()
76 {
77     OSList *p_list = NULL;
78     
79     HANDLE hsnap;
80     HANDLE hpriv;
81     PROCESSENTRY32 p_entry;
82     p_entry.dwSize = sizeof(PROCESSENTRY32);
83
84
85     /* Getting token for enable debug priv */
86     if(!OpenThreadToken(GetCurrentThread(), 
87                         TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, FALSE, &hpriv))
88     {
89         if(GetLastError() == ERROR_NO_TOKEN)
90         {
91             if(!ImpersonateSelf(SecurityImpersonation))
92             {
93                 merror("%s: ERROR: os_get_win32_process_list -> "
94                        "ImpersonateSelf",ARGV0);
95                 return(NULL);
96             }
97
98             if(!OpenThreadToken(GetCurrentThread(),
99                                 TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, 
100                                 FALSE, &hpriv))
101             {
102                 merror("%s: ERROR: os_get_win32_process_list -> "
103                        "OpenThread",ARGV0);
104                 return(NULL) ;
105             }
106         }
107         else
108         {
109             merror("%s: ERROR: os_get_win32_process_list -> OpenThread",ARGV0);
110             return(NULL);
111         }
112     }
113     
114
115     /* Enabling debug privilege */
116     if(!os_win32_setdebugpriv(hpriv, 1))
117     {
118         merror("%s: ERROR: os_win32_setdebugpriv",ARGV0);
119         CloseHandle(hpriv);
120
121         return(NULL);
122     }
123
124
125     /* Snapshot of every process */
126     hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
127     if(hsnap == INVALID_HANDLE_VALUE)
128     {
129         merror("%s: ERROR: CreateToolhelp32Snapshot",ARGV0);
130         return(NULL);
131     }
132
133
134     /* Getting first and second processes -- system entries */
135     if(!Process32First(hsnap, &p_entry) && !Process32Next(hsnap, &p_entry ))
136     {
137         merror("%s: ERROR: Process32First", ARGV0);
138         CloseHandle(hsnap);
139         return(NULL);
140     }
141
142
143     /* Creating process list */
144     p_list = OSList_Create();
145     if(!p_list)
146     {
147         CloseHandle(hsnap);
148         merror(LIST_ERROR, ARGV0);
149         return(0);
150     }
151                                             
152
153     /* Getting each process name and path */
154     while(Process32Next( hsnap, &p_entry))
155     {
156         char *p_name;
157         char *p_path;
158         Proc_Info *p_info;
159
160         /* Setting process name */
161         os_strdup(p_entry.szExeFile, p_name);
162         
163         
164         /* Getting additional information from modules */
165         HANDLE hmod = INVALID_HANDLE_VALUE;
166         MODULEENTRY32 m_entry;
167         m_entry.dwSize = sizeof(MODULEENTRY32);
168         
169         /* Snapshot of the process */
170         hmod = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, 
171                                         p_entry.th32ProcessID);
172         if(hmod == INVALID_HANDLE_VALUE)
173         {
174             os_strdup(p_name, p_path);
175         }
176
177         /* Getting executable path (first entry in the module list */
178         else if(!Module32First(hmod, &m_entry))
179         {
180             CloseHandle(hmod);
181             os_strdup(p_name, p_path);
182         }
183
184         else
185         {
186             os_strdup(m_entry.szExePath, p_path);
187             CloseHandle(hmod);
188         }
189
190         os_calloc(1, sizeof(Proc_Info), p_info);
191         p_info->p_name = p_name;
192         p_info->p_path = p_path;
193         OSList_AddData(p_list, p_info);
194     }
195
196     /* Removing debug privileges */
197     os_win32_setdebugpriv(hpriv, 0);
198
199     CloseHandle(hsnap);
200     return((void *)p_list);
201 }
202
203 #endif /* WIN32 */
204
205 /* EOF */