/usr/lib/python2.7/dist-packages/rekall/plugins/common/profile_index.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 | # Rekall Memory Forensics
# Copyright 2014 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
#
"""This module implements profile indexing.
Rekall relies on accurate profiles for reliable analysis of memory artifacts. We
depend on selecting the correct profile from the profile repository, but
sometimes it's hard to determine the exact profile to use. The profile
repository has index files that are used to lookup the correct profile quickly,
based on a limited set of symbols and offsets that are known, or can be easily
detected, about the image.
"""
__author__ = (
"Michael Cohen <scudette@google.com>",
"Adam Sindelar <adamsh@google.com>",
"Jordi Sanchez <nop@google.com>"
)
import hashlib
from rekall import obj
from rekall import utils
class IndexProfileLoader(obj.ProfileSectionLoader):
name = "$INDEX"
def LoadIntoProfile(self, session, profile, index):
profile.LoadIndex(index)
return profile
class Index(obj.Profile):
"""A profile which contains an index to locate other profiles."""
index = None
base_offset = 0
PERFECT_MATCH = 1.0
GOOD_MATCH = 0.75
def LoadIndex(self, index):
self.index = index
def copy(self):
result = super(Index, self).copy()
result.index = self.index.copy()
return result
def _TestSymbols(self, address_space, offset, possible_values):
"""Match any of the possible_values at offset.
Return True if there is a match.
"""
for value in possible_values:
value = value.decode("hex")
data = address_space.read(offset, len(value))
if value == data:
return data
def _TestProfile(self, address_space, image_base, profile, symbols,
minimal_match=1):
"""Match _all_ the symbols against this data."""
count_matched = 0
count_unmatched = 0
for offset, possible_values in symbols:
# The possible_values can be a single string which means there is
# only one option. If it is a list, then any of the symbols may
# match at this offset to be considered a match.
if isinstance(possible_values, basestring):
possible_values = [possible_values]
# If the offset is not mapped in we can not compare it. Skip it.
offset_to_check = image_base + offset
if address_space.vtop(offset_to_check) == None:
continue
match = self._TestSymbols(
address_space=address_space,
offset=offset_to_check,
possible_values=possible_values)
if match:
self.session.logging.debug(
"%s matched offset %#x+%#x=%#x (%r)",
profile, offset, image_base, offset+image_base, match)
count_matched += 1
else:
# FIXME: We get here if the comparison point does not match -
# does it make sense to allow some points to not match? Should
# we consider these a failure to match?
count_unmatched += 1
# Require at least this many comparison points to be matched.
if count_matched < minimal_match:
return 0
if count_matched > 0:
self.session.logging.debug(
"%s matches %d/%d comparison points",
profile, count_matched, count_matched + count_unmatched)
return float(count_matched) / (count_matched + count_unmatched)
return 0
def IndexHits(self, image_base, address_space=None, minimal_match=1):
if address_space == None:
address_space = self.session.GetParameter("default_address_space")
for profile, symbols in self.index.iteritems():
match = self._TestProfile(
address_space=address_space,
image_base=image_base,
profile=profile,
minimal_match=minimal_match,
symbols=symbols)
yield match, profile
def LookupIndex(self, image_base, address_space=None, minimal_match=1):
partial_matches = []
for match, profile in self.IndexHits(image_base, address_space,
minimal_match=minimal_match):
if match == self.PERFECT_MATCH:
# Yield perfect matches right away.
yield (profile, self.PERFECT_MATCH)
elif match > 0:
# Imperfect matches will be saved and returned in order of
# accuracy.
partial_matches.append((match, profile))
partial_matches.sort(reverse=True)
for match, profile in partial_matches:
yield (profile, match)
class SymbolOffsetIndex(Index):
"""A specialized index that works on symbols-offsets."""
def __init__(self, *args, **kwargs):
super(SymbolOffsetIndex, self).__init__(*args, **kwargs)
if not self.index:
self.index = {}
@utils.safe_property
def hashes(self):
return self.index.get("$HASHES", {})
@utils.safe_property
def traits(self):
return self.index.get("$TRAITS", {})
@utils.safe_property
def profiles(self):
return self.index.get("$PROFILES", {})
@utils.safe_property
def duplicates(self):
return [p for p in self.index.get("$PROFILES") if p not in self.hashes]
def LookupProfile(self, symbols):
"""Returns which profiles in the index match a dict of symbols.
Returns:
A list of tuples of (profile, num_matched_traits).
"""
profiles = []
try:
relative_symbols = self.RelativizeSymbols(symbols.copy())
except ValueError as e:
self.session.logging.debug(str(e))
for profile, traits in self.traits.iteritems():
matched_traits = 0
for trait in traits:
# A trait is a list of symbol-offset tuples.
match = all([relative_symbols.get(symbol) == offset
for (symbol, offset) in trait])
if match:
matched_traits += 1
if matched_traits > 0:
profiles.append((profile, matched_traits))
return profiles
def LookupHash(self, profile_hash):
"""Returns the profile with hash profile_hash."""
return self.hashes.get(profile_hash)
@classmethod
def FilterSymbols(cls, symbols):
"""Filters a dict of symbols, discarding irrelevant ones."""
return symbols
@classmethod
def CalculateRawProfileHash(cls, profile):
"""Calculates a hash of a list of symbols."""
# Skip superfluous symbols.
symbols = profile["$CONSTANTS"]
ordered_symbol_list = sorted(
["(%s, %d)" % (k, v)
for (k, v) in cls.FilterSymbols(symbols).iteritems()])
hasher = hashlib.sha256()
hasher.update("|".join(ordered_symbol_list))
return hasher.hexdigest()
@classmethod
def CalculateRawSymbolsHash(cls, profile):
"""Calculates a hash of a list of symbols."""
# Skip superfluous symbols.
symbols = profile["$CONSTANTS"]
ordered_symbol_list = sorted(symbols.keys())
hasher = hashlib.sha256()
hasher.update("|".join(ordered_symbol_list))
return hasher.hexdigest()
def ProfileMetadata(self, profile_name):
return self.profiles.get(profile_name)
@classmethod
def ProfileMatchesTrait(cls, profile, trait):
"""Whether a profile matches another profile's trait.
A trait is a list of tuples (symbol, offset) that uniquely identify
a profile.
"""
return all([profile.get_constant(t[0]) == t[1] for t in trait])
@classmethod
def RawProfileMatchesTrait(cls, profile, trait):
"""Whether a raw profile (JSON) matches another profile's trait.
A trait is a list of tuples (symbol, offset) that uniquely identify
a profile.
"""
return all([profile.get(t[0]) == t[1] for t in trait])
@classmethod
def BuildIndex(cls, hashes=None, traits=None, duplicates=None, spec=None,
iomanager=None):
"""Builds a SymbolOffset index from traits, profiles, hashes and a spec.
Args:
hashes: A dictionary of hash:profile_id. Hashes must be obtained via
the SymbolOffsetIndex.CalculateRawProfileHash() method.
traits: A dictionary of profile_id:traits. Traits are the result
of calling the SymbolOffsetIndex.FindTraits() method.
profiles: A dictionary of profile_id metadata. Profile metadata
is obtained via SymbolOffsetIndex.GetProfileMetadata().
duplicates: A list of newly found profile ids that are duplicate.
"""
spec = spec or {}
metadata = dict(Type="Index",
ProfileClass=spec.get("implementation", cls.__name__),
BaseSymbol=spec.get("base_symbol"))
hashes = hashes or {}
traits = traits or {}
# Assert all profiles that have hashes have traits as well
if not all([profile in hashes.values() for profile in traits]):
raise ValueError("Not all profiles with traits have hashes")
# Assert all profiles that have traits have hashes as well
if not all([profile in traits for profile in hashes.values()]):
raise ValueError("Not all profiles with hashes have traits")
profiles = dict([(profile_id,
cls.GetProfileMetadata(
iomanager=iomanager, profile_id=profile_id))
for profile_id in traits])
duplicates = duplicates or []
for duplicate_profile in duplicates:
profiles[duplicate_profile] = cls.GetProfileMetadata(
iomanager=iomanager, profile_id=duplicate_profile)
index = {
"$METADATA": metadata,
"$INDEX": {
"$TRAITS": traits or {},
"$PROFILES": profiles or {},
"$HASHES": hashes or {},
}
}
return index
@classmethod
def GetProfileMetadata(cls, iomanager=None, profile_id=None):
profile_metadata = dict()
file_mtime = iomanager.Metadata(profile_id)["LastModified"]
profile_metadata["LastModified"] = file_mtime
return profile_metadata
def __len__(self):
return len(self.traits)
def __iter__(self):
"""Yields tuples of profile_id, traits.
Each trait is a list of tuples of (symbol, offset) that make this
profile unique within the repository.
"""
for profile, traits in self.index.get("$TRAITS").iteritems():
yield profile, traits
def RelativizeSymbols(self, symbols, base_symbol=None):
"""Modifies a dict of symbols so its offsets relative to base_symbol.
If no base_symbol is provided and the index itself doesn't define one
then returns the symbols as is.
Args:
symbols: A dictionary of symbol:value
base_symbol: The name of the symbol to base others' values on.
"""
if not base_symbol:
base_symbol = self.metadata("BaseSymbol")
if not base_symbol:
return symbols
base_value = symbols.get(base_symbol)
if not base_value:
raise ValueError("Symbol %s not found in profile", base_symbol)
new_symbols = symbols.copy()
for symbol, value in new_symbols.iteritems():
new_symbols[symbol] = value - base_value
return new_symbols
class LinuxSymbolOffsetIndex(SymbolOffsetIndex):
"""Specialized symbol-offset index for linux."""
@classmethod
def FilterSymbols(cls, symbols):
"""Filters a dict of symbols, discarding irrelevant ones."""
return dict([(k, v) for (k, v) in symbols.iteritems()
if not "." in k and k != "__irf_end"])
@classmethod
def BuildIndex(cls, hashes=None, traits=None, duplicates=None, spec=None,
iomanager=None):
index = super(LinuxSymbolOffsetIndex, cls).BuildIndex(
hashes=hashes, traits=traits, spec=spec, duplicates=duplicates,
iomanager=iomanager)
# By default, we'll calculate KASLR from linux_proc_banner which is
# present on all kernels.
spec = spec or {}
index["$METADATA"]["BaseSymbol"] = spec.get("base_symbol",
"linux_proc_banner")
return index
|