This file is indexed.

/usr/lib/python2.7/dist-packages/rekall/plugins/tools/profile_tool.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
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
#!/usr/bin/env python2

# Rekall Memory Forensics
# Copyright 2013 Google Inc. All Rights Reserved.
#
# Author: Michael Cohen scudette@google.com
#
# 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
#

"""
Converts Volatility profile files into the Rekall format.

The Volatility profiles are derived by dumping debugging symbols using various
means into a zip file:

- On Linux the output of dwarfdump is stored and parsed on each execution. The
  constants are just copied from the System map.

- On OSX the symbols are produced using the dsymutil tool while the vtypes are
  python files.

- On Windows the vtypes are python files which must be executed.

Rekall profiles are more structured. All profiles contain a metadata file within
the zip archive called "metadata" which simply contains key value pairs encoded
using json. For example:

{
 # This must point at the implementation of this profile (i.e. the class which
 # should be created). Valid values include Linux32, Linux64, WinXPSP1x86
 # etc. You can use the 'info' plugin to see which classes already exist.

 "ProfileClass": "Linux64"

 # This is the name of a member inside this zip file which contains the
 # constant list.

 "Constants":  "System.map.json"

 # This points at a json file within this zip file which contains the vtype
 # definitions for this profile.

 "VTypes": "vtypes.json"
}

We chose to use json to store the vtype data structures because loading json
files in python is extremely quick and leads to much faster start up times than
having to parse the vtypes in other formats (We do not allow loading of vtypes
inside python files because this may lead to arbitrary code execution since the
vtype file needs to be evaluated.).

Often users already have profiles created for Volatility which they want to use
in Rekall. Rather than fall back to the slow and inefficient parsing of these
profiles, Rekall allows users to convert the old profile into a new, efficient
profile representation. This is what this module does with the convert command.

For example, suppose you have an existing profile created for use in Volatility,
you can just convert it to the rekall format:

./tools/profile_converter.py convert Ubuntu-3.0.0-32-generic-pae.zip \
   Ubuntu-3.0.0-32-generic-pae.rekall.json

$ ls -l Ubuntu-3.0.0-32-generic-pae.*
-rw-r----- 1 scudette g 643711 Dec 12 02:12 Ubuntu-3.0.0-32-generic-pae.rekall.json
-rw-r----- 1 scudette g 726480 Dec 12 00:30 Ubuntu-3.0.0-32-generic-pae.zip

Now simply specify the rekall profile using the --profile command line arg.
"""

__author__ = (
    "Michael Cohen <scudette@google.com>",
    "Jordi Sanchez <nop@google.com>"
)

import gzip
import itertools
import json
import os
import re
import StringIO

from rekall import io_manager
from rekall import obj
from rekall import plugin
from rekall import registry
from rekall import testlib
from rekall import utils

from rekall.plugins.common import profile_index
from rekall.plugins.overlays.linux import dwarfdump
from rekall.plugins.overlays.linux import dwarfparser
from rekall.plugins.windows import common


class ProfileConverter(object):
    """Base class for converters."""

    __metaclass__ = registry.MetaclassRegistry
    __abstract = True

    def __init__(self, input, profile_class=None, session=None):
        self.input = input
        self.session = session
        self.profile_class = profile_class

    def SelectFile(self, regex):
        """Reads the content of the first file which matches regex."""
        for f in self.input.ListFiles():
            if re.search(regex, f, re.I):
                return self.input.Open(f).read()

    def BuildProfile(self, system_map, vtypes, config=None):
        _ = config
        # Sorting the json keys usually achieves much smaller file size due to
        # better compression. Its worth doing it once on conversion.
        result = {
            "$METADATA": dict(ProfileClass=self.profile_class,
                              Type="Profile", Version=1),
            "$CONSTANTS": system_map,
            "$STRUCTS": vtypes
            }

        return result

    def Convert(self):
        raise RuntimeError("Unknown profile format.")


class LinuxConverter(ProfileConverter):
    """Convert an existing Linux profile zip file.

    Since building the linux profile often happens on the target system, where
    Rekall is not normall running, we just convert the result of running Make in
    the tools/linux/ directory. See tools/linux/README for details.

    In short:

    - Run make in tools/linux/ directory. This will build module_dwarf.ko with
      debugging symbols.

    - If you have zip installed, the above step will create the required zip
      file. Otherwise Create a zip file manually with module_dwarf.ko and
      /boot/System.map-`uname -r` (Sometimes when running make not as the root
      user, its not possible to read the System.map file).

    Finally use this tool to convert the profile to a Rekall compatible profile.
    """
    BASE_PROFILE_CLASS = "Linux"

    def ParseSystemMap(self, system_map):
        """Parse the system map and return a list of offset, symbol_name."""
        sys_map = {}
        # get the system map
        for line in system_map.splitlines():
            (address, _, symbol) = line.strip().split()
            try:
                sys_map[symbol] = long(address, 16) & 0xFFFFFFFFFFFF
            except ValueError:
                pass

        return sys_map

    def ParseConfigFile(self, config_file):
        """Parse the kernel .config file returning it as a dictionary."""
        config = {}
        for line in config_file.splitlines():
            if line.startswith("#"):
                continue
            try:
                (config_param, value) = line.strip().split("=")
                # Remove leading and trailing spaces from the config_param.
                config_param = config_param.lstrip(" \t").rstrip(" \t")
                # Massage the value a bit so plugins trying to use them get more
                # useful values. This deals with config options like
                # CONFIG_DEFAULT_HOSTNAME="(none)" having a value of
                # str("(none)") instead of str("\"(none)\"").
                value = value.rstrip(" \t").lstrip(" \t")
                value = value.rstrip('"\'').lstrip('"\'')
                config[config_param] = value
            except ValueError:
                pass

        return config

    def BuildProfile(self, system_map, vtypes, config=None):
        """Write all the components needed for the output profile."""
        # Try to guess the bit size of the system if not provided.
        if self.profile_class is None:
            self.profile_class = self.BASE_PROFILE_CLASS

        enums = vtypes.pop("$ENUMS", {})
        reverse_enums = vtypes.pop("$REVENUMS", {})

        result = super(LinuxConverter, self).BuildProfile(system_map, vtypes)
        result["$CONFIG"] = config or dict()
        result["$ENUMS"] = enums
        result["$REVENUMS"] = reverse_enums

        self.profile_class = self.BASE_PROFILE_CLASS
        largest_offset = max(system_map.values())
        if "CONFIG_CPU_MIPS32" in result["$CONFIG"]:
            result["$METADATA"]["arch"] = "MIPS"

        elif "arm_syscall" in result["$CONSTANTS"]:
            result["$METADATA"]["arch"] = "ARM"

        elif largest_offset > 2**32:
            result["$METADATA"]["arch"] = "AMD64"
        else:
            result["$METADATA"]["arch"] = "I386"
        return result

    def Convert(self):
        # Load the config file if it exists
        config = self.SelectFile("(^|/)config")
        if config:
            config = self.ParseConfigFile(config)

        # Check for a linux profile. It should have a System.map in it.
        system_map = self.SelectFile("(^|/)System.map")
        if system_map:
            # Parse the system map file.
            system_map = self.ParseSystemMap(system_map)

            ko_file = self.SelectFile(r"module.*\.ko$")
            if ko_file:
                self.session.logging.info(
                    "Converting Linux profile with ko module.")
                parser = dwarfparser.DWARFParser(StringIO.StringIO(ko_file),
                                                 session=self.session)

                profile_file = self.BuildProfile(system_map, parser.VType(),
                                                 config=config)
                return profile_file

            dwarf_file = self.SelectFile(r"\.dwarf$")
            if dwarf_file:
                self.session.logging.info(
                    "Converting Linux profile with dwarf dump output")
                parser = dwarfdump.DWARFParser()
                for line in dwarf_file.splitlines():
                    parser.feed_line(line)

                # The dwarfdump module returns python code so we must exec it.
                l = {}
                exec(parser.print_output(), {}, l)

                profile_file = self.BuildProfile(system_map, l["linux_types"],
                                                 config=config)
                return profile_file

        raise RuntimeError("Unknown profile format.")


class OSXConverter(LinuxConverter):
    """Automatic conversion from Volatility OSX style profiles.

    You can generate one of those using the instructions here:
    http://code.google.com/p/volatility/wiki/MacMemoryForensics#Building_a_Profile
    """
    BASE_PROFILE_CLASS = "Darwin"

    DLSYM_REGEX = re.compile("([^ ]+) '([^ ]+)'$")

    def ParseSystemMap(self, system_map):
        sys_map = {}
        for line in system_map.splitlines():
            if self.profile_class is None and "Symbol table for" in line:
                last_part = line.split()[-1]
                if last_part == "(x86_64)":
                    self.profile_class = "Darwin64"
                elif last_part == "(i386)":
                    self.profile_class = "Darwin32"
                else:
                    raise RuntimeError(
                        "Unknown Darwin Architecture %s" % last_part)

            # We only care about few things like functions and global symbols.
            if "N_FUN" in line or "EXT" in line or "N_STSYM" in line:
                m = self.DLSYM_REGEX.search(line)
                if m:
                    try:
                        sys_map[m.group(2)] = long(m.group(1), 16)
                    except ValueError:
                        pass

        return sys_map

    def Convert(self):
        # Check for an OSX profile.
        system_map = self.SelectFile("dsymutil$")
        if system_map:
            # Parse the system map file.
            system_map = self.ParseSystemMap(system_map)

            vtype_file = self.SelectFile(r"\.vtypes$")
            if vtype_file:
                self.session.logging.info(
                    "Converting Darwin profile with vtypes dump output")

                # The dwarfdump module returns python code so we must exec it.
                l = {}
                exec(vtype_file, {}, l)

                profile_file = self.BuildProfile(system_map, l["mac_types"])
                return profile_file

        raise RuntimeError("Unknown profile format.")


class ConvertProfile(plugin.TypedProfileCommand, plugin.Command):
    """Convert a profile from another program to the Rekall format.

    The Rekall profile format is optimized for loading at runtime. This plugin
    produces a Rekall profile from a variety of sources, including:

    - Linux debug compiled kernel module (see tool/linux/README)
    - OSX Dwarfdump outputs.
    """

    __name = "convert_profile"

    __args = [
        dict(name="profile_class",
             help="The name of the profile implementation to specify. "
             "If not specified, we autodetect."),

        dict(name="converter",
             help="The name of the converter to use. "
             "If not specified autoguess."),

        dict(name="source", positional=True, required=True,
             help="Filename of profile to read."),

        dict(name="out_file", positional=True, required=True,
             help="Path for output file."),
    ]

    def ConvertProfile(self, input):
        """Converts the input profile to a new standard profile in output."""
        # First detect what kind of profile the input profile is.
        for converter in (LinuxConverter, OSXConverter):
            try:
                profile = converter(input, session=self.session).Convert()
                return profile
            except RuntimeError:
                pass

        raise RuntimeError(
            "No suitable converter found - profile not recognized.")

    def render(self, renderer):
        if self.plugin_args.converter:
            cls = ProfileConverter.classes.get(self.plugin_args.converter)
            if not cls:
                raise IOError(
                    "Unknown converter %s" % self.plugin_args.converter)

            return cls(self.plugin_args.source,
                       profile_class=self.plugin_args.profile_class).Convert()

        try:
            input = io_manager.Factory(
                self.plugin_args.source, session=self.session, mode="r")
        except IOError:
            self.session.logging.critical(
                "Input profile file %s could not be opened.",
                self.plugin_args.source)
            return

        with input:
            profile = self.ConvertProfile(input)
            if profile:
                with renderer.open(
                    filename=self.plugin_args.out_file, mode="wb") as output:
                    output.write(utils.PPrint(profile))
                    self.session.logging.info("Converted %s to %s",
                                              input, output.name)


class TestConvertProfile(testlib.DisabledTest):
    PARAMETERS = dict(commandline="convert_profile")


class TestBuildIndex(testlib.DisabledTest):
    PARAMETERS = dict(commandline="build_index")


class BuildIndex(plugin.Command):
    """Generate a profile index file based on an index specification.

    The index specification is currently a yaml file with the following
    structure:

    ```
    base_symbol: (string) # OPTIONAL Compute ALL offsets as relative to this
        symbol. This includes MaxOffset and MinOffset.
    symbols: (array of dicts) # A list of symbols to index.
      -
        name: (string) # Symbol name
        data: (string) # Data that should be at the symbol's offset
        shift: (int) # OPTIONAL Adjust symbol offset by this number
    ```

    ## Example:

    ```
    path: win32k.sys
    symbols:
      -
        # The name of the symbol we test for.
        name: "??_C@_1BO@KLKIFHLC@?$AAG?$AAU?$AAI?$AAF?$AAo?$AAn?$AAt?$AA?4?$AAH?$AAe?$AAi?$AAg?$AAh?$AAt?$AA?$AA@"

        # The data we expect to find at that offset.
        data: "47005500490046006f006e0074002e00480065006900670068007400"

      -
        name: "wcschr"
        shift: -1
        data: "90"
    ```

    The result is an index profile. This has an $INDEX section which is a dict,
    with keys being the profile name, and values being a list of (offset, match)
    tuples. For example:

    ```
    {
     "$INDEX": {
      "tcpip.sys/AMD64/6.0.6001.18000/0C1A1EC1D61E4508A33F5212FC1B37202": [[1184600, "495053656344656c657465496e626f756e644f7574626f756e64536150616972"]],
      "tcpip.sys/AMD64/6.0.6001.18493/29A4DBCAF840463298F40190DD1492D02": [[1190376, "495053656344656c657465496e626f756e644f7574626f756e64536150616972"]],
      "tcpip.sys/AMD64/6.0.6002.18272/7E79532FC7E349C690F5FBD16E3562172": [[1194296, "495053656344656c657465496e626f756e644f7574626f756e64536150616972"]],
    ...

     "$METADATA": {
      "ProfileClass": "Index",
      "Type": "Profile"
      "MaxOffset": 546567
      "MinOffset": 0
      }
     }
    ```
    """

    __name = "build_index"

    @classmethod
    def args(cls, parser):
        super(BuildIndex, cls).args(parser)
        parser.add_argument(
            "spec", default=None,
            help="An Index specification file.")

        parser.add_argument(
            "--root", default="./",
            help="Repository root path.")

    def __init__(self, spec=None, root="./", manager=None, **kwargs):
        super(BuildIndex, self).__init__(**kwargs)
        self.spec = spec
        if manager is None:
            manager = io_manager.DirectoryIOManager(
                root, session=self.session)
        self.io_manager = manager

    @staticmethod
    def _decide_base(data, base_symbol):
        if base_symbol == None:
            return 0

        return data["$CONSTANTS"].get(base_symbol, None)

    def ValidateDataIndex(self, index):
        """Check the index for collisions.

        An index collision occurs when all the comparison points in one GUID are
        also contained in another GUID. If these points match it is impossible
        to distinguish between the two indexes. We need to issue a warning so
        the user can add additional comparison points to resolve the ambiguity.
        """
        errors = 0

        # The following algorithm is very slow O(n^2) but there aren't that many
        # profiles in the index.
        for profile, data in index.iteritems():
            for profile2, data2 in index.iteritems():
                overlap = []

                # Don't report collisions with the same profile.
                if profile == profile2:
                    continue

                for condition in data:
                    if condition in data2:
                        overlap.append(condition)

                if overlap == data:
                    # Some profiles are just rebuilt (so they have a new GUID)
                    # but they are otherwise identical. We can never distinguish
                    # between them so it does not matter.
                    if self._AreProfilesEquivalent(profile, profile2):
                        continue

                    errors += 1
                    self.session.logging.error(
                        "Profile %s and %s are ambiguous, please add more "
                        "comparison points.", profile, profile2)

                    self.session.logging.error(
                        "Run the following command:\nzdiff %s.gz %s.gz",
                        profile, profile2)

        if errors:
            self.session.logging.error("Index with errors: %s", errors)

    def _AreProfilesEquivalent(self, profile, profile2):
        # Check if the two profiles are equivalent:
        profile_obj = self.io_manager.GetData(profile)
        profile2_obj = self.io_manager.GetData(profile2)

        for section in ["$CONSTANTS", "$FUNCTIONS"]:
            if profile_obj.get(section) != profile2_obj.get(section):
                return False

        self.session.logging.info("Profile %s and %s are equivalent",
                                  profile, profile2)
        return True

    def BuildDataIndex(self, spec):
        """Builds a data index from the specification.

        A data index is an index which collates known data at known offsets
        in memory. We then apply the index to a memory location to discover
        the most likely match there.
        """
        index = {}
        metadata = dict(Type="Profile",
                        ProfileClass="Index")

        result = {"$METADATA": metadata,
                  "$INDEX": index}

        highest_offset = 0
        lowest_offset = 2 ** 64
        base_sym = spec.get("base_symbol", None)

        for relative_path, data in self._GetAllProfiles(spec["path"]):
            for sym_spec in spec["symbols"]:
                shift = sym_spec.get("shift", 0)

                if "$CONSTANTS" not in data:
                    continue

                offset = data["$CONSTANTS"].get(sym_spec["name"])
                if offset is None:
                    # Maybe its a function.
                    offset = data["$FUNCTIONS"].get(sym_spec["name"])
                    if offset is None:
                        continue

                # Offsets (as well as min/max offset) are computed
                # relative to base.
                base = self._decide_base(
                    data=data,
                    base_symbol=base_sym)

                # If we got a base symbol but it's not in the constants
                # then that means this profile is incompatible with this
                # index and should be skipped.
                if base == None:
                    continue

                # We don't record the offset as reported by the profile
                # but as the reader is actually going to use it.
                offset = offset + shift - base

                values = []
                # If a symbol's expected value is prefixed with
                # 'str:' then that means it was given to us as
                # human-readable and we need to encode it. Otherwise it
                # should already be hex-encoded.
                for value in sym_spec["data"]:
                    if value.startswith("lstr:"):
                        value = value[5:].encode("utf-16le").encode("hex")

                    elif value.startswith("str:"):
                        value = value[4:].encode("hex")

                    else:
                        try:
                            value.decode("hex")
                        except TypeError:
                            raise ValueError(
                                "String %r must be encoded in hex, "
                                "or prefixed by str: or lstr:" % value)

                    values.append(value)

                index.setdefault(relative_path, []).append((offset, values))

                # Compute the lowest and highest offsets so the reader
                # can optimize reading the image.
                lowest_offset = min(lowest_offset, offset)
                highest_offset = max(
                    highest_offset, offset + len(sym_spec["data"]))

        metadata["BaseSymbol"] = base_sym
        metadata["MaxOffset"] = highest_offset
        metadata["MinOffset"] = lowest_offset

        # Make sure to issue warnings if the index is not good enough.
        self.ValidateDataIndex(index)

        return result

    def BuildStructIndex(self, spec):
        """Builds a Struct index from specification.

        A Struct index is a collection of struct offsets for certain members
        over all available versions.
        """
        index = {}
        metadata = dict(Type="Profile",
                        ProfileClass=spec.get("implementation", "Index"))

        result = {"$METADATA": metadata,
                  "$INDEX": index}

        for relative_path, data in self._GetAllProfiles(spec["path"]):
            try:
                structs = data["$STRUCTS"]
            except KeyError:
                continue

            metadata = index[relative_path] = data["$METADATA"]
            offsets = metadata["offsets"] = {}
            for struct, fields in spec["members"].items():
                for field in fields:
                    try:
                        offsets["%s.%s" % (struct, field)] = (
                            structs[struct][1][field][0])
                    except KeyError:
                        continue

        return result

    def _SymbolIsUnique(self, profile_id, symbol, profiles):
        """Does symbol uniquely identify profile_id within profiles.

        Args:
          profile_id: The unique identifier of symbol's profile.
          symbol: The symbol to test.
          profiles: A dictionary of profile_id:symbol_dict entries where
            symbol_dict is a dictionary of symbol:offset entries.

            Every profile in profiles must be unique. That is, two entries must
            not share the exact same set of symbol:offset pairs.

        """

        offset = profiles[profile_id].get(symbol)

        # If the symbol doesn't exist it can't be unique
        if offset is None:
            return False

        unique = True

        for other_id, other_symbols in profiles.iteritems():
            # Skip comparing this profile against itself.
            if profile_id == other_id:
                continue

            # Find duplicates
            if offset == other_symbols.get(symbol):
                unique = False
                break

        return unique

    def _FindNewProfiles(self, index, target):
        """Finds new profiles in the repository that were not in the index."""

        new_profiles = 0

        # Walk all files to find new profiles
        for profile_id in self.io_manager.ListFiles():
            if not profile_id.startswith(target):
                continue

            # Skip known duplicates.
            # Skip profiles that haven't changed.
            file_mtime = self.io_manager.Metadata(profile_id)["LastModified"]

            try:
                profile_mtime = index.ProfileMetadata(
                    profile_id)["LastModified"]

                # If the current file is not fresher than the old file, we
                # just copy the metadata from the old profile. Allow 1
                # second grace for float round ups.
                if profile_mtime+1 >= file_mtime:
                    continue
            except (KeyError, TypeError):
                # Profile doesn't exist in the index yet.
                # See if it was a duplicate.
                pass

            try:
                data = self.io_manager.GetData(profile_id)
                if "$CONSTANTS" not in data:
                    self.session.logging.debug(
                        "File %s doesn't look like a profile, skipping...",
                        profile_id)
                    continue
                data["$CONSTANTS"] = index.RelativizeSymbols(
                    data["$CONSTANTS"], "linux_proc_banner")
                # Free up some memory
                del data["$CONFIG"]
                del data["$STRUCTS"]
            except ValueError as e:
                self.session.logging.error("ERROR loading %s: %s",
                                           profile_id, e)
                continue

            new_profiles += 1
            self.session.report_progress(
                "[STEP 1/6] Found %d new profiles: %s",
                new_profiles, profile_id)
            yield profile_id, data


    def _FindProfilesWithSymbolOffset(self, symbol_name, symbol_offset,
                                      profiles=None):
        """Returns a set of profile_ids that have symbol_name: symbol_offset."""
        matching_profiles = set()
        for profile_id, symbols in profiles.iteritems():
            if symbols.get(symbol_name) == symbol_offset:
                matching_profiles.add(profile_id)
        return matching_profiles

    def _FindTraits(self, profile_id=None, profiles=None, num_traits=1,
                    trait_length=1, first_try_symbols=None):
        """Finds traits of profile against other_profiles.

        Args:
            profile_id: The id of the profile to find traits for within profiles
            profiles: A dict of profile:symbols tuples where symbols is a dict
              of symbol:value.
            num_traits: How many traits to find.
            trait_length: How many symbols to consider per trait.
            first_try_symbols: A list of symbols to try first.
        """
        found_traits = []
        profile_symbols = profiles.get(profile_id)

        # The set we're looking for.
        exit_set = set([profile_id])

        # Store a pool of symbols
        symbol_pool = profile_symbols.keys()
        if first_try_symbols:
            # Reorder these symbols so they are tried first
            for symbol in reversed(first_try_symbols):
                try:
                    symbol_pool.remove(symbol)
                except ValueError:
                    pass
                symbol_pool.insert(0, symbol)

        for trait_symbols in itertools.combinations(symbol_pool, trait_length):

            symbol = trait_symbols[0]
            offset = profile_symbols.get(symbol)
            intersection_set = self._FindProfilesWithSymbolOffset(
                symbol, offset, profiles=profiles)

            for next_symbol in trait_symbols[1:]:
                next_offset = profile_symbols.get(next_symbol)
                next_set = self._FindProfilesWithSymbolOffset(
                    next_symbol, next_offset,
                    profiles=profiles)

                # For a trait to be unique, the resulting set of performing
                # the intersection of the sets of profiles containing the
                # symbol-offset tuples must be the original profile_id.
                intersection_set &= next_set

                # If the comparison set is empty, we're done
                if intersection_set == exit_set:
                    break

            if intersection_set == exit_set:
                # Found a trait
                trait = [(s, profile_symbols.get(s)) for s in trait_symbols]
                found_traits.append(trait)
                if len(found_traits) == num_traits:
                    break
        return found_traits

    def BuildSymbolsIndex(self, spec):
        """Builds an index to identify profiles based on their symbols-offsets.

        The index stores traits for each profile. A trait is a combination of
        1 or more symbol-offset pairs that uniquely identify it within the
        current profile repository.

        The code handles:
          - Incremental updates of the index. Adding a new profile to the index
          doesn't trigger recomputing the entire index.
          - Detection of duplicates. If a profile is to be added that's already
          in the index, it will be detected and skipped.
          - Clash detection. If a new profile has some symbol-offsets that were
          traits of other profiles, the profile whose traits are not unique
          anymore will be found and its index rebuilt.
        """

        directory_to_index = spec.get("path", "Linux")
        index_path = os.path.join(directory_to_index, "index")

        # Load the current index from the index directory.
        #index = self.session.LoadProfile(index_path, use_cache=False)
        index = obj.Profile.LoadProfileFromData(
            self.io_manager.GetData(index_path), name=index_path,
            session=self.session)

        # A list of duplicate profiles to update the index
        new_duplicate_profiles = []


        # If we don't yet have an index, we start with a blank one.
        if not index:
            dummy_index = profile_index.LinuxSymbolOffsetIndex.BuildIndex(
                iomanager=self.io_manager)
            index = obj.Profile.LoadProfileFromData(
                data=dummy_index, session=self.session)

        if not isinstance(index, profile_index.SymbolOffsetIndex):
            raise ValueError(
                "The index should be a SymbolOffsetIndex but found %s instead" %
                (index.___class__.__name__))
        self.session.logging.debug("Index is a %s", index.__class__.__name__)

        # STEP 1. Find new profiles. New profiles are profiles not in the
        # index or profiles that have been updated.
        self.session.report_progress("[STEP 1/6] Finding new profiles...",
                                     force=True)
        new_profile_candidates = list(self._FindNewProfiles(index,
                                                            spec["path"]))

        # STEP 2. Determine how many of the new profiles are duplicates.
        # New profiles can be duplicates because they already exist in the index
        # with another name or because they clash with some other new profile.
        self.session.report_progress("[STEP 2/6] Finding duplicate profiles...",
                                     force=True)
        new_hashes_dict = dict()
        new_profiles = dict()
        for i, (profile_id, data) in enumerate(sorted(new_profile_candidates)):
            self.session.report_progress(
                "[STEP 2/6][%d/%d] Finding if %s is duplicate.",
                i, len(new_profile_candidates), profile_id)
            profile_hash = index.CalculateRawProfileHash(data)
            existing_profile = index.LookupHash(profile_hash)

            if existing_profile == profile_id:
                # This is a profile already in the index that's been updated.
                # But if the profile still has the same hash, we have to do
                # nothing as the index is still good.
                # This wil be the case when touch()ing profiles or probably
                # copying them over.
                continue

            # If it's identical to a profile we already have indexed, this is a
            # duplicate.
            #
            # TODO: We should remove the profile and make it a Symlink.
            if existing_profile:
                self.session.logging.info(
                    ("New profile %s is equivalent to %s, which is already "
                     "in the index."),
                    profile_id, existing_profile)
                new_duplicate_profiles.append(profile_id)
                continue

            # Otherwise it may clash with another new profile. This can easily
            # happen when we add more than one profile at a time, with minor
            # version increases.
            #
            # Example: Ubuntu Trusty 3.13.0-54-generic vs 3.13.0-55-generic.
            if profile_hash in new_hashes_dict:
                # This is a duplicate. Discard.
                # TODO: Remove the profile and make it a Symlink.
                self.session.logging.info(
                    "New profile %s is equivalent to another new profile %s.",
                    profile_id,
                    new_hashes_dict.get(profile_hash))
                new_duplicate_profiles.append(profile_id)
                continue

            # If it was not a duplicate,
            symbols = data.get("$CONSTANTS")
            symbols = index.FilterSymbols(symbols)
            new_profiles[profile_id] = symbols
            new_hashes_dict[profile_hash] = profile_id

        # Inform of how many profiles we skipped indexing.
        if len(new_profile_candidates) > len(new_profiles):
            self.session.logging.info(
                "Skipped indexing %d profiles, since they were duplicates.",
                len(new_profile_candidates) - len(new_profiles))


        # STEP 3. Find if any of the new profiles forces us to recompute
        # traits for profiles already in the repository. This can happen if
        # the trait that's in the index now appears in one of the
        # new profiles.
        #
        # Since we calculate more than one trait per profile the index may
        # still work for other traits. But we want healthy indexes, so we
        # recalculate all the traits.

        num_clashing_profiles = 0
        self.session.report_progress(
            "[STEP 3/6] Finding index clashes with new profiles", force=True)

        for i, (profile_id, traits_dict) in enumerate(sorted(index)):
            self.session.report_progress(
                "[STEP 3/6][%d/%d] Finding index clashes with new profiles",
                i, len(index))
            profile_needs_rebuild = False

            for trait in traits_dict:
                for new_profile_id, symbols in new_profiles.iteritems():
                    if index.RawProfileMatchesTrait(symbols, trait):
                        self.session.logging.warn(
                          "New profile %s clashes with %s, will recalculate.",
                          new_profile_id, profile_id)
                        profile_needs_rebuild = True
                        break

                # Leave the loop early if a trait is not unique anymore.
                if profile_needs_rebuild:
                    break

            if profile_needs_rebuild:
                num_clashing_profiles += 1
                data = self.io_manager.GetData(profile_id)
                data["$CONSTANTS"] = index.RelativizeSymbols(
                    data["$CONSTANTS"])
                new_profiles[profile_id] = data["$CONSTANTS"]

        if not new_profiles:
            self.session.logging.info("No new profiles found. Exitting.")
            return profile_index.LinuxSymbolOffsetIndex.BuildIndex(
                hashes=index.hashes,
                traits=index.traits,
                spec=spec,
                duplicates=index.duplicates + new_duplicate_profiles,
                iomanager=self.io_manager)

        self.session.logging.info(
            ("Will regenerate an index for %d profiles. %d are new and %d "
             "were in the index but now have clashes"),
            len(new_profiles),
            len(new_profiles) - num_clashing_profiles,
            num_clashing_profiles)

        # STEP 4. Find unique symbols for all new profiles. We need to open
        # all the profiles in the repo
        # additionally to the new ones which we opened earlier.

        self.session.report_progress(
            "[STEP 4/6] Opening all profiles in the repository.", force=True)
        # Start by opening all profiles in the index.
        index_profiles = dict()
        for i, (profile_id, _) in enumerate(index):
            self.session.report_progress(
                "[STEP 4/6][%d/%d] Opening %s...",
                i, len(index), profile_id)
            profile = self.io_manager.GetData(profile_id)
            profile["$CONSTANTS"] = index.RelativizeSymbols(
                profile["$CONSTANTS"])
            # Free up some memory
            del profile["$STRUCTS"]
            del profile["$CONFIG"]
            symbols = profile.get("$CONSTANTS")
            symbols = index.FilterSymbols(symbols)
            index_profiles[profile_id] = symbols

        all_profiles = index_profiles.copy()
        # Any profile that was in the index but has been updated on disk will
        # be overriden here, which is what we want.
        all_profiles.update(dict(new_profiles))

        self.session.report_progress(
            "[STEP 4/6] Finding single-symbol traits.", force=True)
        # A list of profiles we haven't found traits for.
        retry_profiles = []
        # Maximum number of traits to find.
        min_traits = spec.get("min_traits", 5)
        self.session.report_progress(
            "[STEP 4/6] Finding single-symbol traits. Opening all, done.",
            force=True)

        # A dictionary of traits per profile_id
        traits_dict = dict()
        for i, (profile_id, symbols) in enumerate(
            sorted(new_profiles.iteritems())):

            self.session.report_progress(
                "[STEP 4/6][%d/%d] Finding %d traits for %s",
                i, len(new_profiles), min_traits, profile_id)

            traits = self._FindTraits(profile_id,
                                      profiles=all_profiles,
                                      num_traits=min_traits,
                                      trait_length=1)
            traits_dict[profile_id] = traits

            if not traits_dict.get(profile_id):
                self.session.logging.warning(
                    "Profile %s has no single-symbol trait.", profile_id)
                retry_profiles.append(profile_id)
            elif len(traits_dict.get(profile_id)) < min_traits:
                self.session.logging.info(
                    "[STEP 4/6][%d/%d] Found %d/%d traits for %s. Queueing...",
                    i, len(new_profiles), len(traits), min_traits,
                    profile_id)
                retry_profiles.append(profile_id)
            else:
                self.session.logging.info(
                    "[STEP 4/6][%d/%d] Found %d/%d traits for %s",
                    i, len(new_profiles), len(traits), min_traits,
                    profile_id)


        self.session.report_progress(
            "[STEP 5/6] Finding unique 2-symbol traits...", force=True)

        # STEP 5. Process the remaining profiles to find unique pairs.
        for i, profile_id in enumerate(retry_profiles):
            self.session.report_progress(
                "[STEP 5/6][%d/%d] Finding unique 2-symbol pairs for %s",
                i, len(retry_profiles), profile_id, force=True)

            # We have to find only the remaining number of traits to reach
            # min_traits.
            num_traits_to_find = (min_traits -
                                  len(traits_dict.get(profile_id, [])))

            first_try_symbols = None
            if len(traits_dict.get(profile_id, [])) == 1:
                first_try_symbols = [trait[0] for trait
                                      in traits_dict.get(profile_id)]

            traits = self._FindTraits(profile_id,
                                      profiles=all_profiles,
                                      num_traits=num_traits_to_find,
                                      trait_length=2,
                                      first_try_symbols=first_try_symbols)
            traits_dict[profile_id] = traits

            if traits_dict.get(profile_id) is None:
                self.session.logging.error(
                    "Profile %s has no 2-symbol trait.", profile_id)
            else:
                self.session.logging.info(
                    "[STEP 5/6][%d/%d] Found %d/%d 2-symbol traits for %s",
                    i, len(retry_profiles),
                    len(traits_dict.get(profile_id, [])),
                    min_traits,
                    profile_id)

        # LAST STEP: Build the index augmenting the previous index.
        self.session.report_progress(
            "[STEP 6/6] Building index...", force=True)
        new_index_hashes = index.hashes.copy()
        new_index_hashes.update(new_hashes_dict)

        new_index_traits = index.traits.copy()
        new_index_traits.update(traits_dict)

        # Update the profile metadata with the new and updated profiles.
        new_index_profile_metadata = index.profiles.copy()
        for profile_id in new_profiles:
            file_mtime = self.io_manager.Metadata(profile_id)["LastModified"]
            metadata_dict = new_index_profile_metadata.get(profile_id, {})
            metadata_dict["LastModified"] = file_mtime

        return profile_index.LinuxSymbolOffsetIndex.BuildIndex(
            hashes=new_index_hashes,
            traits=new_index_traits,
            duplicates=index.duplicates + new_duplicate_profiles,
            spec=spec,
            iomanager=self.io_manager)

    def _GetProfile(self, name):
        path = "%s.gz" % name
        file_data = gzip.open(path).read()
        return json.loads(file_data)

    def _GetAllProfiles(self, path):
        """Iterate over all paths and get the profiles."""
        for profile_name in self.io_manager.ListFiles():
            if profile_name.startswith(path):
                self.session.report_progress("Processing %s", profile_name)
                data = self.io_manager.GetData(profile_name)

                yield profile_name, data

    def build_index(self, spec):
        if spec.get("type") == "struct":
            return self.BuildStructIndex(spec)
        elif spec.get("type") == "symbol_offset":
            return self.BuildSymbolsIndex(spec)
        else:
            return self.BuildDataIndex(spec)

    def render(self, renderer):
        spec = self.io_manager.GetData(self.spec)
        renderer.write(utils.PPrint(self.build_index(spec)))


class BuildProfileLocally(plugin.Command):
    """Download and builds a profile locally in one step.

    We store the profile in the first repository in the profile_path which must
    be writable. Usually this is a caching repository so the profile goes in the
    local cache.
    """

    name = "build_local_profile"

    @classmethod
    def args(cls, parser):
        super(BuildProfileLocally, cls).args(parser)
        parser.add_argument(
            "module_name",
            help="The name of the module (without the .pdb extensilon).",
            required=True)

        parser.add_argument(
            "guid",
            help="The guid of the module.",
            required=False)

        parser.add_argument("--dumpfile",
                            help="If specified also dump the json file here.")

    def __init__(self, module_name=None, guid=None, dumpfile=None, **kwargs):
        super(BuildProfileLocally, self).__init__(**kwargs)
        self.module_name = module_name
        self.guid = guid
        self.dumpfile = dumpfile

    def _fetch_and_parse(self, module_name, guid):
        """Fetch the profile from the symbol server.

        Raises:
          IOError if the profile is not found on the symbol server or can not be
          retrieved.

        Returns:
           the profile data.
        """
        with utils.TempDirectory() as dump_dir:
            pdb_filename = "%s.pdb" % module_name
            fetch_pdb_plugin = self.session.plugins.fetch_pdb(
                pdb_filename=pdb_filename,
                guid=guid, dump_dir=dump_dir)

            # Store the PDB file somewhere.
            pdb_pathname = os.path.join(dump_dir, pdb_filename)
            with open(pdb_pathname, "wb") as outfd:
                outfd.write(fetch_pdb_plugin.FetchPDBFile())

            parse_pdb = self.session.plugins.parse_pdb(
                pdb_filename=pdb_pathname,
                dump_dir=dump_dir)

            return parse_pdb.parse_pdb()

    def fetch_and_parse(self, module_name=None, guid=None, renderer=None):
        if module_name is None:
            module_name = self.module_name

        if guid is None:
            guid = self.guid

        # Allow the user to specify the required profile by name.
        m = re.match("([^/]+)/GUID/([^/]+)$", module_name)
        if m:
            module_name = m.group(1)
            guid = m.group(2)

        if not guid or not module_name:
            raise TypeError("GUID not specified.")

        profile_name = "{0}/GUID/{1}".format(module_name.lower(), guid)

        # Get the first repository to write to.
        repository = self.session.repository_managers[0][1]
        if module_name != "nt":
            data = self._fetch_and_parse(module_name, guid)

            if self.dumpfile:
                with renderer.open(filename=self.dumpfile, mode="wb") as fd:
                    fd.write(utils.PPrint(data))

            return repository.StoreData(profile_name, data)

        for module_name in common.KERNEL_NAMES:
            if module_name.endswith(".pdb"):
                module_name, _ = os.path.splitext(module_name)
            try:
                data = self._fetch_and_parse(module_name, guid)
                self.session.logging.warning(
                    "Profile %s fetched and built. Please "
                    "consider reporting this profile to the "
                    "Rekall team so we may add it to the public "
                    "profile repository.", profile_name)

                return repository.StoreData(profile_name, data)
            except IOError, e:
                self.session.logging.error("Error: %s", e)

        raise IOError("Profile not found")

    def render(self, renderer):
        self.fetch_and_parse(self.module_name, self.guid, renderer=renderer)


class TestBuildProfileLocally(testlib.HashChecker):
    PARAMETERS = dict(
        commandline=("build_local_profile --module_name=%(pdb_name)s "
                     "--guid=%(guid)s --dumpfile %(tempdir)s/output"),
        pdb_name="ntkrnlpa",
        guid="BD8F451F3E754ED8A34B50560CEB08E31"
    )