/usr/lib/python2.7/dist-packages/rekall/plugins/windows/malware/svcscan.py is in python-rekall-core 1.6.0+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 | # Rekall Memory Forensics
# Copyright (c) 2010, 2011, 2012 Michael Ligh <michael.ligh@mnin.org>
# Copyright 2013 Google Inc. All Rights Reserved.
#
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# pylint: disable=protected-access
from rekall import plugin
from rekall import obj
from rekall import utils
from rekall.plugins.windows import common
from rekall.plugins.windows import vadinfo
SERVICE_TYPE_FLAGS = {
'SERVICE_KERNEL_DRIVER': 0,
'SERVICE_FILE_SYSTEM_DRIVER': 1,
'SERVICE_WIN32_OWN_PROCESS': 4,
'SERVICE_WIN32_SHARE_PROCESS': 5,
'SERVICE_INTERACTIVE_PROCESS': 8
}
SERVICE_STATE_ENUM = {
1: 'SERVICE_STOPPED',
2: 'SERVICE_START_PENDING',
3: 'SERVICE_STOP_PENDING',
4: 'SERVICE_RUNNING',
5: 'SERVICE_CONTINUE_PENDING',
6: 'SERVICE_PAUSE_PENDING',
7: 'SERVICE_PAUSED'
}
svcscan_base_x86 = {
'_SERVICE_HEADER': [None, {
'Tag': [0x0, ['array', 4, ['unsigned char']]],
'ServiceRecord': [0xC, ['pointer', ['_SERVICE_RECORD']]],
}],
'_SERVICE_RECORD': [None, {
'NextService': [0x0, ['_SERVICE_HEADER']],
'ServiceName': [0x8, ['pointer', ['UnicodeString', dict(length=512)]]],
'DisplayName': [0xc, ['pointer', ['UnicodeString', dict(length=512)]]],
'Order': [0x10, ['unsigned int']],
'Tag': [0x18, ['array', 4, ['unsigned char']]],
'DriverName': [0x24, ['pointer', ['UnicodeString', dict(
length=256)]]],
'ServiceProcess': [0x24, ['pointer', ['_SERVICE_PROCESS']]],
'Type': [0x28, ['Flags', {'bitmap': SERVICE_TYPE_FLAGS}]],
'State': [0x2c, ['Enumeration', dict(target='long',
choices=SERVICE_STATE_ENUM)]],
}],
'_SERVICE_PROCESS': [None, {
'BinaryPath': [0x8, ['pointer', ['UnicodeString', dict(
encoding='utf16', length=256)]]],
'ProcessId': [0xc, ['unsigned int']],
}],
}
svcscan_base_x64 = {
'_SERVICE_HEADER': [None, {
'Tag': [0x0, ['Array', dict(
count=4,
target='unsigned char'
)]],
'ServiceRecord': [0x10, ['Pointer', dict(
target='_SERVICE_RECORD'
)]],
}],
'_SERVICE_RECORD': [None, {
'NextService': [0x0, ['Pointer', dict(
target="_SERVICE_RECORD"
)]],
'ServiceName': [0x8, ['pointer', ['UnicodeString', dict(
encoding='utf16', length=512)]]],
'DisplayName': [0x10, ['Pointer', dict(
target='UnicodeString',
target_args=dict(length=512)
)]],
'Order': [0x18, ['unsigned int']],
'Tag' : [0x20, ['Array', dict(
count=4,
target='unsigned char'
)]],
'DriverName': [0x30, ['Pointer', dict(
target='UnicodeString',
target_args=dict(
length=256
)
)]],
'ServiceProcess': [0x30, ['Pointer', dict(
target='_SERVICE_PROCESS'
)]],
'Type': [0x38, ['Flags', {'bitmap': SERVICE_TYPE_FLAGS}]],
'State': [0x3C, ['Enumeration', dict(
target='long', choices=SERVICE_STATE_ENUM)]],
}],
'_SERVICE_PROCESS': [None, {
'BinaryPath': [0x10, ['Pointer', dict(
target='UnicodeString',
target_args=dict(
length=256
)
)]],
'ProcessId': [0x18, ['unsigned int']],
}],
}
class _SERVICE_RECORD_LEGACY(obj.Struct):
"Service records for XP/2003 x86 and x64"
@utils.safe_property
def Binary(self):
"Return the binary path for a service"
# No path in memory for services that aren't running
# (if needed, query the registry key)
if str(self.State) != 'SERVICE_RUNNING':
return obj.NoneObject("No path, service isn't running")
# Depending on whether the service is for a process
# or kernel driver, the binary path is stored differently
if 'PROCESS' in str(self.Type):
return self.ServiceProcess.BinaryPath.dereference()
else:
return self.DriverName.dereference()
@utils.safe_property
def Pid(self):
"Return the process ID for a service"
if str(self.State) == 'SERVICE_RUNNING':
if 'PROCESS' in str(self.Type):
return self.ServiceProcess.ProcessId
return obj.NoneObject("Cannot get process ID")
def is_valid(self):
"Check some fields for validity"
return (super(_SERVICE_RECORD_LEGACY, self).is_valid() and
self.Order > 0 and self.Order < 0xFFFF)
class _SERVICE_RECORD_RECENT(_SERVICE_RECORD_LEGACY):
"Service records for 2008, Vista, 7 x86 and x64"
class _SERVICE_HEADER(obj.Struct):
"Service headers for 2008, Vista, 7 x86 and x64"
def is_valid(self):
"Check some fields for validity"
return (super(_SERVICE_HEADER, self).is_valid() and
self.ServiceRecord.is_valid())
_SERVICE_RECORD_VISTA_X86 = {
'_SERVICE_RECORD': [None, {
'NextService': [0x0, ['pointer', ['_SERVICE_RECORD']]],
'ServiceName': [0x4, ['pointer', ['UnicodeString', dict(length=512)]]],
'DisplayName': [0x8, ['pointer', ['UnicodeString', dict(length=512)]]],
'Order': [0xC, ['unsigned int']],
'ServiceProcess': [0x1C, ['pointer', ['_SERVICE_PROCESS']]],
'DriverName': [0x1C, ['pointer', ['UnicodeString', dict(
length=256)]]],
'Type' : [0x20, ['Flags', {'bitmap': SERVICE_TYPE_FLAGS}]],
'State': [0x24, ['Enumeration', dict(
target='unsigned int', choices=SERVICE_STATE_ENUM)]],
}],
}
_SERVICE_RECORD_VISTA_X64 = {
'_SERVICE_RECORD': [None, {
'NextService': [0x00, ['Pointer', dict(
target='_SERVICE_RECORD'
)]],
'ServiceName': [0x08, ['pointer', ['UnicodeString', dict(
length=512
)]]],
'DisplayName': [0x10, ['pointer', ['UnicodeString', dict(
length=512
)]]],
'Order': [0x18, ['unsigned int']],
'ServiceProcess': [0x28, ['pointer', ['_SERVICE_PROCESS']]],
'DriverName': [0x28, ['Pointer', dict(
target='UnicodeString',
target_args=dict(
length=256,
)
)]],
'Type' : [0x30, ['Flags', {'bitmap': SERVICE_TYPE_FLAGS}]],
'State': [0x34, ['Enumeration', dict(
target='unsigned int',
choices=SERVICE_STATE_ENUM
)]],
}],
}
_SERVICE_RECORD_WIN81_X64 = {
"_SERVICE_RECORD": [None, {
"Tag": [0, ["String", dict(length=4)]], # Signature sErv
'NextService': [0x8, ['Pointer', dict(
target='_SERVICE_RECORD'
)]],
'ServiceName': [0x10, ['pointer', ['UnicodeString', dict(
length=512
)]]],
'DisplayName': [0x18, ['pointer', ['UnicodeString', dict(
length=512
)]]],
'Order': [0x20, ['unsigned int']],
'ServiceProcess': [0x38, ['pointer', ['_SERVICE_PROCESS']]],
'DriverName': [0x38, ['Pointer', dict(
target='UnicodeString',
target_args=dict(
length=256,
)
)]],
'Type' : [0x40, ['Flags', {'bitmap': SERVICE_TYPE_FLAGS}]],
'State': [0x44, ['Enumeration', dict(
target='unsigned int',
choices=SERVICE_STATE_ENUM
)]],
}],
'_SERVICE_PROCESS': [None, {
'Tag': [0, ["String", dict(length=4)]], # Sc16
'BinaryPath': [0x18, ['Pointer', dict(
target='UnicodeString',
target_args=dict(
length=256
)
)]],
'ProcessId': [0x20, ['unsigned int']],
}],
}
class ServiceModification(obj.ProfileModification):
"""A modification for the service control manager."""
@classmethod
def modify(cls, profile):
if profile.metadata("arch") == "I386":
profile.add_overlay(svcscan_base_x86)
else:
# 32bit Vista profiles
profile.add_overlay(svcscan_base_x64)
# Windows XP, 2003
version = profile.metadata("version")
if version < 6.0:
profile.add_classes({
'_SERVICE_RECORD': _SERVICE_RECORD_LEGACY,
'_SERVICE_HEADER': _SERVICE_HEADER,
})
profile.add_constants(dict(ServiceTag="sErv"))
# Vista 2008 and windows 7
elif 6.0 <= version <= 6.2:
profile.add_classes({
'_SERVICE_RECORD': _SERVICE_RECORD_RECENT,
'_SERVICE_HEADER': _SERVICE_HEADER,
})
profile.add_constants(dict(ServiceTag="serH"))
if profile.metadata("arch") == "I386":
profile.add_overlay(_SERVICE_RECORD_VISTA_X86)
else:
profile.add_overlay(_SERVICE_RECORD_VISTA_X64)
# Windows 8.1 and Windows 10
elif 6.2 <= version:
profile.add_classes({
'_SERVICE_RECORD': _SERVICE_RECORD_RECENT,
'_SERVICE_HEADER': _SERVICE_HEADER,
})
profile.add_constants(dict(ServiceTag="serH"))
if profile.metadata("arch") == "I386":
profile.add_overlay(_SERVICE_RECORD_VISTA_X86)
else:
profile.add_overlay(_SERVICE_RECORD_WIN81_X64)
else:
raise RuntimeError(
"Unsupported windows version. Please file a bug.")
class SvcRecordScanner(vadinfo.VadScanner):
"""A scanner for the service tags."""
def __init__(self, **kwargs):
super(SvcRecordScanner, self).__init__(**kwargs)
self.checks = [
('StringCheck', dict(
needle=self.profile.get_constant("ServiceTag"))),
]
self.tag_offset = self.profile.get_obj_offset('_SERVICE_RECORD', 'Tag')
def scan(self, **kwargs):
for hit in super(SvcRecordScanner, self).scan(**kwargs):
svc_record = self.profile._SERVICE_RECORD(
vm=self.address_space, offset=hit - self.tag_offset)
if svc_record.is_valid():
yield svc_record
class SvcHeaderScanner(vadinfo.VadScanner):
"""A scanner for the service tags."""
def __init__(self, **kwargs):
super(SvcHeaderScanner, self).__init__(**kwargs)
self.checks = [
('StringCheck', dict(
needle=self.profile.get_constant("ServiceTag"))),
]
# On systems more recent than XP/2003, the serH marker doesn't
# find *all* services, but the ones it does find have linked
# lists to the others. We use this variable to track which
# ones we've seen so as to not yield duplicates.
self.records = set()
def scan(self, **kwargs):
for hit in super(SvcHeaderScanner, self).scan(**kwargs):
svc_header = self.profile._SERVICE_HEADER(
vm=self.address_space, offset=hit)
if svc_header.is_valid():
for record in svc_header.ServiceRecord.walk_list("NextService"):
if record.is_valid() and record not in self.records:
self.records.add(record)
yield record
class SvcScan(plugin.KernelASMixin, common.AbstractWindowsCommandPlugin):
"Scan for Windows services"
__name = "svcscan"
def __init__(self, scan_in_kernel_address_space=False, **kwargs):
"""Scan for callbacks.
Args:
scan_in_kernel_address_space: If False we will use the physical
address space for scanning, while if true we scan in the kernel
address space.
"""
super(SvcScan, self).__init__(**kwargs)
self.scan_in_kernel_address_space = scan_in_kernel_address_space
# Update the profile.
self.profile = ServiceModification(self.profile)
def calculate(self):
# Get the version we're analyzing
version = self.profile.metadatas('major', 'minor')
pslist = self.session.plugins.pslist(proc_regex="services.exe")
for task in pslist.filter_processes():
# Process AS must be valid
process_space = task.get_process_address_space()
if process_space == None:
continue
# XP/2003 use the _SERVICE_RECORD object.
if version <= "5.2":
scanner = SvcRecordScanner(
task=task, process_profile=self.profile,
session=self.session)
else:
# Windows Vista, 2008, and 7 use the _SERVICE_HEADER
scanner = SvcHeaderScanner(
task=task, process_profile=self.profile,
session=self.session)
# Find all instances of the record tag
for record in scanner.scan():
yield record
def render(self, renderer):
renderer.table_header([
("Offset", "offset", "[addrpad]"),
("Order", "order", "5"),
("PID", "pid", "4"),
("Service Name", "service", "30"),
("Display Name", "display_name", "40"),
("Service Type", "type", "30"),
("Service State", "state", "15"),
("Binary Path", "binary_path", "")])
for rec in self.calculate():
renderer.table_row(
rec,
rec.Order,
rec.Pid,
rec.ServiceName.deref(),
rec.DisplayName.deref(),
rec.Type,
rec.State,
rec.Binary)
|