This file is indexed.

/usr/lib/python2.7/dist-packages/rekall/plugins/filesystems/ntfs.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
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
# Rekall Memory Forensics
# Copyright 2014 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
#

"""This file implements support for parsing NTFS filesystem in Rekall.

Simply select the ntfs profile with an ntfs image - you might need to also
specify the --file_offset (or -o) parameter.

$ rekal -v --profile ntfs -f ~/images/ntfs1-gen2.E01

[1] Default session 13:56:54> fls
 MFT   Seq           Created                  File Mod                   MFT Mod                   Access              Size    Filename
----- ----- ------------------------- ------------------------- ------------------------- ------------------------- ---------- --------
    4     4 2008-12-31 22:44:02+0000  2008-12-31 22:44:02+0000  2008-12-31 22:44:02+0000  2008-12-31 22:44:02+0000       36000 $AttrDef
    8     8 2008-12-31 22:44:02+0000  2008-12-31 22:44:02+0000  2008-12-31 22:44:02+0000  2008-12-31 22:44:02+0000           0 $BadClus
    6     6 2008-12-31 22:44:02+0000  2008-12-31 22:44:02+0000  2008-12-31 22:44:02+0000  2008-12-31 22:44:02+0000      126112 $Bitmap
    7     7 2008-12-31 22:44:02+0000  2008-12-31 22:44:02+0000  2008-12-31 22:44:02+0000  2008-12-31 22:44:02+0000        8192 $Boot
   11    11 2008-12-31 22:44:02+0000  2008-12-31 22:44:02+0000  2008-12-31 22:44:02+0000  2008-12-31 22:44:02+0000           0 $Extend
    2     2 2008-12-31 22:44:02+0000  2008-12-31 22:44:02+0000  2008-12-31 22:44:02+0000  2008-12-31 22:44:02+0000     4685824 $LogFile
    0     1 2008-12-31 22:44:02+0000  2008-12-31 22:44:02+0000  2008-12-31 22:44:02+0000  2008-12-31 22:44:02+0000       65536 $MFT
...

"""

import array
import logging
import re
import struct

from rekall import addrspace
from rekall import kb
from rekall import plugin
from rekall import obj
from rekall import testlib
from rekall import utils
from rekall.plugins import core
from rekall.plugins import guess_profile
from rekall.plugins.filesystems import lznt1
from rekall.plugins.overlays import basic


class Error(Exception):
    pass


class ParseError(Error):
    pass


class NTFSParseError(ParseError):
    pass


class NTFSDetector(guess_profile.DetectionMethod):
    name = "ntfs"

    def Offsets(self):
        return [0]

    def DetectFromHit(self, hit, _, address_space):
        ntfs_profile = self.session.LoadProfile("ntfs")
        try:
            ntfs = NTFS(address_space=address_space, session=self.session)
            self.session.SetCache("ntfs", ntfs, volatile=False)

            return ntfs_profile
        except NTFSParseError:
            return


FILE_FLAGS = dict(
    READ_ONLY=0x0001,
    HIDDEN=0x0002,
    SYSTEM=0x0004,
    ARCHIVE=0x0020,
    DEVICE=0x0040,
    NORMAL=0x0080,
    TEMPORARY=0x0100,
    SPARSE=0x0200,
    REPARSE_POINT=0x0400,
    COMPRESSED=0x0800,
    OFFLINE=0x1000,
    NOT_INDEXED=0x2000,
    ENCRYPTED=0x4000
    )

ntfs_vtypes = {
    'NTFS_BOOT_SECTOR': [512, {
        "oemname": [3, ["String", dict(length=8)]],
        "sector_size": [11, ["unsigned short"]],

        "_cluster_size": [13, ["unsigned char"]],

        # The cluster_size in bytes.
        "cluster_size": lambda x: x.m("_cluster_size") * x.sector_size,

        # The total number of clusters in the volume
        "block_count": lambda x: x.m("_volume_size") / x.cluster_size,

        "_volume_size":   [40, ["unsigned long"]],
        "_mft_cluster":   [48, ["unsigned long"]],
        "_mirror_mft_cluster":   [56, ["unsigned long"]],
        "_mft_record_size": [64, ["signed byte"]],
        "index_record_size": [68, ["unsigned char"]],
        "serial": [72, ["String", dict(length=8)]],

        # Should be 0xAA55
        "magic": [510, ["unsigned short"]],

        # The MFT can actually be fragmented so this does not have to be the
        # complete MFT.

        "MFT": [lambda x: x.m("_mft_cluster") * x.cluster_size,
                ["Array", dict(
                    target="MFT_ENTRY",
                    target_size=lambda x: x.mft_record_size)
                ]],
    }],

    "MFT_ENTRY": [None, {
        "magic": [0, ["String", dict(length=4)]],
        "fixup_offset": [4, ["unsigned short"]],
        "fixup_count":  [6, ["unsigned short"]],
        "logfile_sequence_number": [8, ["unsigned long long"]],
        "sequence_value": [16, ["unsigned short"]],
        "link_count": [18, ["unsigned short"]],
        "attribute_offset": [20, ["unsigned short"]],
        "flags": [22, ["Flags", dict(
            target="unsigned short",
            bitmap=dict(
                ALLOCATED=0,
                DIRECTORY=1)
        )]],
        "mft_entry_size": [24, ["unsigned short"]],
        "mft_entry_allocated": [28, ["unsigned short"]],
        "base_record_reference": [32, ["unsigned long long"]],
        "next_attribute_id": [40, ["unsigned short"]],
        "record_number": [44, ["unsigned long"]],

        # These are fixups.
        "fixup_magic": [lambda x: x.obj_offset + x.fixup_offset,
                        ["String", dict(length=2, term=None)]],

        "fixup_table": [lambda x: x.obj_offset + x.fixup_offset + 2,
                        ["Array", dict(
                            target="String",
                            target_args=dict(length=2, term=None),
                            count=lambda x: x.fixup_count-1)]],

        # Attributes are a list of NTFS_ATTRIBUTE objects, starting from the
        # attribute_offset member.
        "_attributes": [lambda x: x.obj_offset + x.attribute_offset,
                        ["ListArray",
                         dict(target="NTFS_ATTRIBUTE",
                              maximum_size=lambda x: x.mft_entry_size)]],
    }],

    "NTFS_ATTRIBUTE": [lambda x: x.length, {
        "type": [0, [
            "Enumeration", dict(
                target="unsigned int",

                # The actual mapping between types and type names is
                # given by the $AttrDef file. At some point we parse
                # this file and store it in the session.
                choices=lambda x: x.obj_profile.get_constant(
                    "ATTRIBUTE_NAMES")
            )]],
        "length": [4, ["unsigned int"]],
        "resident": [8, ["Enumeration", dict(
            target="unsigned char",
            choices={
                0: "RESIDENT",
                1: "NON-RESIDENT",
            }
        )]],

        # A Quick check for resident attributes.
        "is_resident": lambda x: x.resident == 0,
        "name_length": [9, ["unsigned char"]],
        "name_offset": [10, ["unsigned short"]],
        "flags": [12, ["Flags", dict(
            target="unsigned short",
            maskmap={
                "COMPRESSED" : 0x0001,
                "ENCRYPTED": 0x4000,
                "SPARSE": 0x8000,
            }
        )]],
        "attribute_id": [14, ["unsigned short"]],

        "name": [lambda x: x.obj_offset + x.name_offset,
                 ["UnicodeString", dict(
                     length=lambda x: x.name_length * 2)]],

        # The following are only valid if the attribute is resident.
        "content_size": [16, ["unsigned int"]],
        "content_offset": [20, ["unsigned short"]],

        # The following are valid if the attribute is non-resident.
        "runlist_vcn_start": [16, ["unsigned long long"]],
        "runlist_vcn_end": [24, ["unsigned long long"]],
        "runlist_offset": [32, ["unsigned short"]],
        "compression_unit_size": [34, ["unsigned short"]],
        "allocated_size": [40, ["unsigned long long"]],
        "actual_size": [48, ["unsigned long long"]],
        "initialized_size": [56, ["unsigned long long"]],
    }],

    "STANDARD_INFORMATION": [None, {
        "create_time": [0, ["WinFileTime"]],
        "file_altered_time": [8, ["WinFileTime"]],
        "mft_altered_time": [16, ["WinFileTime"]],
        "file_accessed_time": [24, ["WinFileTime"]],
        "flags": [32, ["Flags", dict(
            target="unsigned int",
            maskmap=FILE_FLAGS)]],
        "max_versions": [36, ["unsigned int"]],
        "version": [40, ["unsigned int"]],
        "class_id": [44, ["unsigned int"]],
        "owner_id": [48, ["unsigned int"]],
        "sid": [52, ["unsigned int"]],
        "quota": [56, ["unsigned long long"]],
        "usn": [64, ["unsigned int"]],
    }],

    "FILE_NAME": [None, {
        "mftReference": [0, ["BitField", dict(
            target="unsigned long long",
            start_bit=0,
            end_bit=48)]],
        "seq_num": [6, ["short int"]],
        "created": [8, ["WinFileTime"]],
        "file_modified": [16, ["WinFileTime"]],
        "mft_modified": [24, ["WinFileTime"]],
        "file_accessed": [32, ["WinFileTime"]],
        "allocated_size": [40, ["unsigned long long"]],
        "size": [48, ["unsigned long long"]],
        "flags": [56, ["Flags", dict(
            target="unsigned int",
            bitmap=FILE_FLAGS)]],
        "reparse_value": [60, ["unsigned int"]],
        "_length_of_name": [64, ["byte"]],
        "name_type": [65, ["Enumeration", dict(
            target="byte",
            choices={
                0: "POSIX",
                1: "Win32",
                2: "DOS",
                3: "DOS+Win32"
            })]],
        "name": [66, ["UnicodeString", dict(
            length=lambda x: x.m("_length_of_name") * 2)]],
    }],

    "STANDARD_INDEX_HEADER": [42, {
        "magicNumber": [0, ["Signature", dict(
            value="INDX",
        )]],

        "fixup_offset": [4, ["unsigned short"]],
        "fixup_count": [6, ["unsigned short"]],
        "logFileSeqNum": [8, ["unsigned long long"]],
        "vcnOfINDX": [16, ["unsigned long long"]],
        "node": [24, ["INDEX_NODE_HEADER"]],

        # These are fixups.
        "fixup_magic": [lambda x: x.obj_offset + x.fixup_offset,
                        ["String", dict(length=2, term=None)]],

        "fixup_table": [lambda x: x.obj_offset + x.fixup_offset + 2,
                        ["Array", dict(
                            target="String",
                            target_args=dict(length=2, term=None),
                            count=lambda x: x.fixup_count-1)]],
    }],

    "INDEX_RECORD_ENTRY": [lambda x: x.sizeOfIndexEntry.v(), {
        "mftReference": [0, ["BitField", dict(
            target="unsigned long long",
            start_bit=0,
            end_bit=48)]],
        "seq_num": [6, ["short int"]],
        "sizeOfIndexEntry": [8, ["unsigned short"]],
        "filenameOffset": [10, ["unsigned short"]],
        "flags": [12, ["unsigned int"]],
        "file": [16, ["FILE_NAME"]],
    }],

    "INDEX_ROOT": [None, {
        "type": [0, [
            "Enumeration", dict(
                target="unsigned int",

                # The actual mapping between types and type names is
                # given by the $AttrDef file. At some point we parse
                # this file and store it in the session.
                choices=lambda x: x.obj_profile.get_constant(
                    "ATTRIBUTE_NAMES")
            )]],

        "collation_rule": [4, ["unsigned int"]],
        "idxalloc_size_b": [8, ["unsigned int"]],
        "idx_size_c": [12, ["unsigned int"]],
        "node": [16, ["INDEX_NODE_HEADER"]],
    }],

    "INDEX_NODE_HEADER": [0x10, {
        "offset_to_index_entry": [0, ["unsigned int"]],
        "offset_to_end_index_entry": [4, ["unsigned int"]],
    }],

    "ATTRIBUTE_LIST_ENTRY": [lambda x: x.length, {
        "type": [0, [
            "Enumeration", dict(
                target="unsigned int",
                choices=lambda x: x.obj_profile.get_constant(
                    "ATTRIBUTE_NAMES")
            )]],
        "length": [4, ["unsigned short int"]],
        "name_length": [6, ["byte"]],
        "offset_to_name": [7, ["byte"]],
        "starting_vcn": [8, ["unsigned long long"]],
        "mftReference": [16, ["BitField", dict(
            target="unsigned long long",
            start_bit=0,
            end_bit=48)]],

        "attribute_id": [24, ["byte"]],

        # Automatically retrieve the target attribute from the MFT.
        "attribute": lambda x: x.obj_context["mft"][
            x.mftReference].get_attribute(
                x.type, x.attribute_id)
    }],
}


class INDEX_NODE_HEADER(obj.Struct):
    def Entries(self):
        result = self.obj_profile.ListArray(
            offset=self.offset_to_index_entry + self.obj_offset,
            vm=self.obj_vm,
            maximum_offset=self.offset_to_end_index_entry + self.obj_offset - 1,
            target="INDEX_RECORD_ENTRY", context=self.obj_context,
        )

        for x in result:
            if x.flags > 0:
                break
            yield x


class FixupAddressSpace(addrspace.BaseAddressSpace):
    """An address space to implement record fixup."""

    def __init__(self, fixup_magic, fixup_table, base_offset, length, **kwargs):
        super(FixupAddressSpace, self).__init__(**kwargs)
        self.as_assert(self.base is not None, "Address space must be stacked.")
        self.base_offset = base_offset
        self.fixup_table = fixup_table
        self.fixup_magic = fixup_magic

        # We read the entire region into a mutable buffer then apply the fixups.
        self.buffer = array.array("c", self.base.read(base_offset, length))
        for i, fixup_value in enumerate(fixup_table):
            fixup_offset = (i+1) * 512 - 2
            if (self.buffer[fixup_offset:fixup_offset+2].tostring() !=
                    fixup_magic.v()):
                raise NTFSParseError("Fixup error")

            self.buffer[fixup_offset:fixup_offset+2] = array.array(
                "c", fixup_value.v())

    def read(self, address, length):
        buffer_offset = address - self.base_offset
        return self.buffer[buffer_offset:buffer_offset+length].tostring()


class RunListAddressSpace(addrspace.RunBasedAddressSpace):
    """An address space which is initialized from a runlist."""

    def __init__(self, run_list, cluster_size=None, size=0, name="", **kwargs):
        super(RunListAddressSpace, self).__init__(**kwargs)
        self.PAGE_SIZE = cluster_size or self.session.cluster_size
        self.compression_unit_size = 16 * self.PAGE_SIZE
        self._end = size
        self.name = name

        # In clusters.
        file_offset = 0
        for range_start, range_length in run_list:
            if size == 0:
                self._end += range_length * self.PAGE_SIZE

            # A range_start of None represents a sparse range (i.e. should be
            # filled with 0).
            if range_start is None:
                file_offset += range_length

                # Identify a compressed range if the current range is sparse and
                # the last range's length is smaller than a compression unit.
                try:
                    run = self.runs[-1][2]
                    if run.length < self.compression_unit_size:
                        run.data["compression"] = True

                except (ValueError, IndexError):
                    pass

                continue

            # To support compression, we divide the range into complete 16
            # cluster runs, and a remainder. The remainder is possibly
            # compressed.
            compressed_subrange = range_length % 16
            uncompressed_range_length = range_length - compressed_subrange
            if uncompressed_range_length:
                self._store_run(
                    file_offset, range_start, uncompressed_range_length)

            file_offset += uncompressed_range_length
            range_start += uncompressed_range_length

            if compressed_subrange:
                self._store_run(file_offset, range_start, compressed_subrange)

            file_offset += compressed_subrange

    def _store_run(self, file_offset, range_start, length):
        """Store a new run with all items given in self.PAGE_SIZE."""
        # The runs contain a list of:
        # file_offset - the byte offset in the file where the run starts.
        #
        # range_start - the byte offset in the image where the range starts.
        #
        # length - the length of the run in bytes.
        #
        # compressed - A flag to indicate if this run is compressed. Note that
        #     we dont decide it is compressed until we see it followed by a
        #     sparse run which adds us to compression_unit_size.
        self.add_run(file_offset * self.PAGE_SIZE,
                     range_start * self.PAGE_SIZE,
                     length * self.PAGE_SIZE,
                     data=dict(compression=False))

    def _read_chunk(self, addr, length):
        addr = int(addr)
        start, end, run = self.runs.get_containing_range(addr)

        # addr is not in any range, pad to the next range.
        if start is None:
            end = self.runs.get_next_range_start(addr)
            if end is None:
                end = addr + length

            return "\x00" * min(end - addr, length)

        if run.data.get("compression"):
            block_data = lznt1.decompress_data(
                self.base.read(run.file_offset, run.length) + "\x00" * 10,
                logger=self.session.logging.getChild("ntfs"))

            available_length = (self.compression_unit_size - (addr - run.start))

            block_offset = addr - run.start

            result = block_data[
                block_offset:
                block_offset + min(length, available_length)]

            # Decompression went wrong - just zero pad.
            if len(result) < length:
                result += "\x00" * (length - len(result))

            return result

        available_length = run.length - (addr - run.start)
        block_offset = addr - run.start + run.file_offset

        if available_length > 0:
            return self.base.read(
                block_offset, min(length, available_length))

    def get_mappings(self, start=0, end=2**64):
        for run in super(RunListAddressSpace, self).get_mappings(
                start=start, end=end):
            if start > run.end:
                continue

            length = run.length
            # When the run is compressed it really contains an entire
            # compression unit.
            if run.data.get("compression"):
                length = self.compression_unit_size

            length = min(run.length, self.end() - run.start)
            if length > 0:
                yield addrspace.Run(start=run.start,
                                    end=run.start + length,
                                    address_space=run.address_space,
                                    file_offset=run.file_offset)

    def __unicode__(self):
        return utils.SmartUnicode(self.name or self.__class__.__name__)

    def end(self):
        return self._end


class MFT_ENTRY(obj.Struct):
    """An MFT Entry.

    Note that MFT entries behave as either files or directories depending on the
    attributes they have. This object wraps this behavior with convenience
    methods. Hence callers do not need to manipulate attributes directly.
    """

    def __init__(self, **kwargs):
        super(MFT_ENTRY, self).__init__(**kwargs)

        # We implement fixup by wrapping the base address space with a fixed
        # one:
        if self.obj_context.get("ApplyFixup", True):
            self.obj_vm = FixupAddressSpace(fixup_magic=self.fixup_magic,
                                            fixup_table=self.fixup_table,
                                            base_offset=self.obj_offset,
                                            length=self.mft_entry_allocated,
                                            base=self.obj_vm)
        self.logging = self.obj_session.logging.getChild("ntfs")
        # Change to DEBUG to turn on module level debugging.
        self.logging.setLevel(logging.ERROR)

    @utils.safe_property
    def mft_entry(self):
        return self.obj_context.get("index", self.record_number.v())

    @utils.safe_property
    def attributes(self):
        seen = set()

        for attribute in self._attributes:
            if attribute.type == 0xFFFFFFFF:
                break

            if attribute in seen:
                continue

            seen.add(attribute)
            yield attribute

            if attribute.type == "$ATTRIBUTE_LIST":
                for sub_attr in attribute.DecodeAttribute():
                    if sub_attr.mftReference == self.mft_entry:
                        continue

                    result = sub_attr.attribute
                    if result in seen:
                        continue

                    yield result

    def get_attribute(self, type=None, id=None):
        for attribute in self.attributes:
            if type is not None and attribute.type != type:
                continue

            if id is not None and attribute.attribute_id != id:
                continue

            return attribute

        return obj.NoneObject("Attribute not found")

    def is_directory(self):
        """Does this MFT entry behave as a directory?"""
        for attribute in self.attributes:
            if (attribute.type in ("$INDEX_ALLOCATION", "$INDEX_ROOT") and
                    attribute.name == "$I30"):
                return True
        return False

    def list_files(self):
        """List the files contained in this directory.

        Note that any file can contain other files (i.e. be a directory) if it
        has an $I30 stream. Thats is directories may also contain data and
        behave as files!

        Returns:
          An iterator over all INDEX_RECORD_ENTRY.
        """
        for attribute in self.attributes:
            if (attribute.type in ("$INDEX_ALLOCATION", "$INDEX_ROOT") and
                    attribute.name == "$I30"):
                for index_header in attribute.DecodeAttribute():
                    for x in index_header.node.Entries():
                        yield x

    def open_file(self):
        """Returns an address space which maps the content of the file's data.

        If this MFT does not contain any $DATA streams, returns a NoneObject().

        The returned address space is formed by joining all $DATA streams' run
        lists in this MFT into a contiguous mapping.
        """
        runlists = []
        data_size = 0

        # Combine the runlists from all the $DATA attributes into one
        # big runlist.
        for attribute in self.attributes:
            if attribute.type == "$DATA":
                if attribute.is_resident:
                    return attribute.data

                if data_size == 0:
                    data_size = attribute.size

                # Some sanity checking. The runlist should agree with the VCN
                # fields.
                run_length = (attribute.runlist_vcn_end -
                              attribute.runlist_vcn_start + 1)
                run_list = list(attribute.RunList())

                if sum(x[1] for x in run_list) != run_length:
                    self.logging.error(
                        "NTFS_ATTRIBUTE %s-%s: Not all runs found!",
                        self.mft_entry, attribute)

                runlists.extend(attribute.RunList())

        if runlists:
            return RunListAddressSpace(
                run_list=runlists,
                base=self.obj_session.physical_address_space,
                session=self.obj_session,
                name=self.full_path,
                size=data_size)

        return obj.NoneObject("No data")

    @utils.safe_property
    def filename(self):
        dos_name = obj.NoneObject()
        for attribute in self.attributes:
            if attribute.type == "$FILE_NAME":
                attribute = attribute.DecodeAttribute()

                # Prefer to return the win32 names.
                if "Win32" in str(attribute.name_type):
                    return attribute

                dos_name = attribute

        # If only the dos name exists, fall back to it.
        return dos_name

    @utils.safe_property
    def full_path(self):
        """Returns the full path of this MFT to the root."""
        result = []
        mft = self.obj_context["mft"]
        mft_entry = self
        depth = 0
        while depth < 10:
            filename_record = mft_entry.filename
            filename = unicode(filename_record.name)
            if filename == ".":
                break

            result.append(filename)
            mft_entry = mft[filename_record.mftReference]
            if mft_entry == None:
                break

            depth += 1

        result.reverse()
        return "/".join(result)

    @utils.safe_property
    def data_size(self):
        """Search all the $DATA attributes for the allocated size."""
        for attribute in self.attributes:
            if attribute.type == "$DATA" and attribute.size > 0:
                return attribute.size

        return 0


class NTFS_BOOT_SECTOR(obj.Struct):
    """A class to parse and access the NTFS boot sector."""

    # The mft_record_size in bytes
    mft_record_size = 0

    def __init__(self, **kwargs):
        """Parse the boot sector and calculate offsets."""
        super(NTFS_BOOT_SECTOR, self).__init__(**kwargs)
        if self._mft_record_size > 0:
            self.mft_record_size = self._mft_record_size * self.cluster_size
        else:
            self.mft_record_size = 1 << -self._mft_record_size

    def Validate(self):
        """Verify the boot sector for sanity."""

        if self.magic != 0xAA55:
            raise NTFSParseError("Magic not correct.")

        if self.cluster_size not in [
                0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80]:
            raise NTFSParseError("Invalid cluster_size")

        if self.sector_size == 0 or self.sector_size % 512:
            raise NTFSParseError("invalid sector_size")

        if self.block_count == 0:
            raise NTFSParseError("Volume size is 0")


class NTFS_ATTRIBUTE(obj.Struct):
    """The NTFS attribute."""

    # A quick lookup to decode the runlist. Key is the byte size, value is a
    # mask to use.
    MASK = {
        0: 0,
        1: 0xFF,
        2: 0xFFFF,
        3: 0xFFFFFF,
        4: 0xFFFFFFFF,
        5: 0xFFFFFFFFFF,
        6: 0xFFFFFFFFFFFF,
        7: 0xFFFFFFFFFFFFFF,
        8: 0xFFFFFFFFFFFFFFFF,
        }

    # Helps to sign extend the run offset. Key is the number of bytes in the
    # offset, value is the sign bit.
    SIGN_BIT = {
        0: 0,
        1: 1 << (1 * 8 - 1),
        2: 1 << (2 * 8 - 1),
        3: 1 << (3 * 8 - 1),
        4: 1 << (4 * 8 - 1),
        5: 1 << (5 * 8 - 1),
        6: 1 << (6 * 8 - 1),
        7: 1 << (7 * 8 - 1),
        8: 1 << (8 * 8 - 1),
        }

    def sign_extend(self, x, b):
        """Sign extend a value based on the number of bytes it should take."""
        m = self.SIGN_BIT[b]
        x = x & self.MASK[b]
        return (x ^ m) - m

    @utils.safe_property
    def data(self):
        """Returns an address space representing the data of this attribute."""
        if self.is_resident:
            return addrspace.BufferAddressSpace(
                data=self.obj_vm.read(
                    self.obj_offset + self.content_offset,
                    self.content_size),
                session=self.obj_session)
        else:
            run_list = list(self.RunList())

            # Create an address space.
            address_space = RunListAddressSpace(
                run_list=run_list,
                base=self.obj_session.physical_address_space,
                session=self.obj_session, size=self.size)

            return address_space

    def DecodeAttribute(self):
        if self.type == "$STANDARD_INFORMATION":
            return self.obj_profile.STANDARD_INFORMATION(
                offset=0, vm=self.data, context=self.obj_context)

        elif self.type == "$FILE_NAME":
            return self.obj_profile.FILE_NAME(
                offset=0, vm=self.data, context=self.obj_context)

        elif self.type == "$DATA":
            return list(self.RunList())

        elif self.type == "$INDEX_ALLOCATION":
            result = []
            for i in xrange(0, self.size, 0x1000):
                result.append(
                    self.obj_profile.STANDARD_INDEX_HEADER(
                        offset=i, vm=self.data, context=self.obj_context))

            return result

        elif self.type == "$INDEX_ROOT":
            return [self.obj_profile.INDEX_ROOT(
                offset=0, vm=self.data, context=self.obj_context)]

        elif self.type == "$ATTRIBUTE_LIST":
            result = self.obj_profile.ListArray(
                offset=0, vm=self.data,
                target="ATTRIBUTE_LIST_ENTRY",
                maximum_size=self.content_size,
                context=self.obj_context
            )

            return result

    def RunList(self):
        """Decodes the runlist for this attribute."""
        if self.is_resident:
            return

        offset = self.obj_offset + self.runlist_offset
        run_offset = 0

        while 1:
            idx = ord(self.obj_vm.read(offset, 1))
            if idx == 0:
                return

            length_size = idx & 0xF
            run_offset_size = idx >> 4
            offset += 1

            run_length = struct.unpack("<Q", self.obj_vm.read(offset, 8))[0]
            run_length &= self.MASK[length_size]
            offset += length_size

            relative_run_offset = struct.unpack(
                "<Q", self.obj_vm.read(offset, 8))[0]

            relative_run_offset = self.sign_extend(relative_run_offset,
                                                   run_offset_size)

            run_offset += relative_run_offset
            offset += run_offset_size

            # This represents a sparse run.
            if relative_run_offset == 0:
                yield None, run_length
            else:
                yield run_offset, run_length

    @utils.safe_property
    def indices(self):
        return self.type, self.attribute_id

    @utils.safe_property
    def owner_MFT(self):
        """The MFT entry containing this entry."""
        # Note that our offset is expressed in terms of the MFT already.
        return self.obj_offset / 0x400

    @utils.safe_property
    def size(self):
        """The size of this attribute's data."""
        if self.is_resident:
            return self.content_size

        # The first $DATA attribute will return the size of the entire file
        # here.
        return self.actual_size


class STANDARD_INDEX_HEADER(obj.Struct):
    """The index header must manage its own fixups."""

    def __init__(self, **kwargs):
        super(STANDARD_INDEX_HEADER, self).__init__(**kwargs)

        # We implement fixup by wrapping the base address space with a fixed
        # one:
        if self.obj_context.get("ApplyFixup", True):
            self.obj_vm = FixupAddressSpace(fixup_magic=self.fixup_magic,
                                            fixup_table=self.fixup_table,
                                            base_offset=self.obj_offset,
                                            length=self.fixup_count * 512,
                                            base=self.obj_vm)



class NTFSProfile(basic.ProfileLLP64, basic.BasicClasses):
    """A profile for the NTFS."""

    def __init__(self, **kwargs):
        super(NTFSProfile, self).__init__(**kwargs)
        self.add_overlay(ntfs_vtypes)
        self.add_classes(dict(
            NTFS_BOOT_SECTOR=NTFS_BOOT_SECTOR,
            MFT_ENTRY=MFT_ENTRY,
            NTFS_ATTRIBUTE=NTFS_ATTRIBUTE,
            INDEX_NODE_HEADER=INDEX_NODE_HEADER,
            STANDARD_INDEX_HEADER=STANDARD_INDEX_HEADER,
        ))

        # We start off with a constant mapping of attribute types. This may
        # later be updated when parsing the $AttrDef file.
        self.add_constants(dict(ATTRIBUTE_NAMES={
            16: "$STANDARD_INFORMATION",
            32: "$ATTRIBUTE_LIST",
            48: "$FILE_NAME",
            64: "$OBJECT_ID",
            80: "$SECURITY_DESCRIPTOR",
            96: "$VOLUME_NAME",
            112: "$VOLUME_INFORMATION",
            128: "$DATA",
            144: "$INDEX_ROOT",
            160: "$INDEX_ALLOCATION",
            176: "$BITMAP",
            192: "$REPARSE_POINT",
            256: "$LOGGED_UTILITY_STREAM",
        }))


class NTFS(object):
    """A class to manage the NTFS filesystem parser."""

    def __init__(self, address_space, session=None):
        self.profile = NTFSProfile(session=session)
        self.bs = self.profile.NTFS_BOOT_SECTOR(vm=address_space)

        # Check for validity of boot sector.
        self.bs.is_valid()

        session.cluster_size = self.bs.cluster_size

        # Now we search for the $DATA attribute of the $MFT file so we can
        # defragment the MFT.
        mft = self.bs.MFT[0]

        self.address_space = None

        for attribute in mft.attributes:
            if attribute.type == "$DATA":
                run_list = list(attribute.RunList())
                self.address_space = RunListAddressSpace(
                    run_list=run_list, base=address_space, session=session)
                break

        if self.address_space is None:
            raise NTFSParseError("Unable to locate the $MFT.")

        # The MFT is constructed over the RunListAddressSpace to reassemble the
        # fragmentation.
        self.mft = self.profile.Array(offset=0, vm=self.address_space,
                                      target="MFT_ENTRY",
                                      target_size=self.bs.mft_record_size,
                                     )

        # Add a reference to the mft to all sub-objects..
        self.mft.obj_context["mft"] = self.mft

    def MFTEntryByName(self, path):
        """Return the MFT entry by traversing the path.

        We support both / and \\ as path separators. Path matching is case
        insensitive.

        Raises IOError if path is not found.

        Returns:
          a tuple of (path, MFT_ENTRY) where path is the case corrected path.

        """
        components = filter(None, re.split(r"[\\/]", path))
        return_path = []

        # Always start from the root of the filesystem.
        directory = self.mft[5]
        for component in components:
            component = component.lower()

            for record in directory.list_files():
                filename = record.file.name.v()
                if filename.lower() == component.lower():
                    directory = self.mft[record.mftReference]
                    return_path.append(filename)
                    break
            else:
                raise IOError("Path %s component not found." % component)

        directory.obj_context["path"] = "/".join(return_path)

        return directory


class NTFSPlugins(plugin.PhysicalASMixin, plugin.TypedProfileCommand,
                  plugin.ProfileCommand):
    """Base class for ntfs plugins."""
    __abstract = True

    mode = "mode_ntfs"

    def __init__(self, *args, **kwargs):
        super(NTFSPlugins, self).__init__(*args, **kwargs)
        self.ntfs = self.session.GetParameter("ntfs")
        if self.ntfs == None:
            self.ntfs = NTFS(self.session.physical_address_space,
                             session=self.session)
            self.session.SetCache("ntfs", self.ntfs, volatile=False)
            self.session.ntfs = self.ntfs


class FileBaseCommandMixin(object):
    """Mixin for commands which take filenames- delegate to inode commands."""
    delegate = ""

    __args = [
        dict(name="path", default="/", positional=True,
             help="Path to print stats for."),
    ]

    def render(self, renderer):
        mft = self.ntfs.MFTEntryByName(self.plugin_args.path)
        delegate = getattr(self.session.plugins, self.delegate)(
            mfts=[mft.mft_entry])
        delegate.render(renderer)


class MFTPluginsMixin(object):
    """A mixin for plugins which work on mft entries."""

    __args = [
        dict(name="mfts", type="ArrayIntParser", default=[5],
             required=False, positional=True,
             help="MFT entries to list.")
    ]


class FStat(FileBaseCommandMixin, NTFSPlugins):
    """Print information by filename."""
    name = "fstat"
    delegate = "istat"


class IStat(MFTPluginsMixin, NTFSPlugins):
    """Print information related to an MFT entry."""
    name = "istat"

    def render_standard_info(self, renderer, mft_entry):
        for attribute in mft_entry.attributes:
            if attribute.type == "$STANDARD_INFORMATION":
                decoded_attribute = attribute.DecodeAttribute()

                renderer.format("$STANDARD_INFORMATION Attribute Values:\n")

                renderer.table_header([
                    ("Key", "key", "30"),
                    ("Value", "value", "30")], suppress_headers=True)

                renderer.table_row("Flags", decoded_attribute.flags)
                renderer.table_row("Owner ID", decoded_attribute.owner_id)
                renderer.table_row("SID", decoded_attribute.sid)
                renderer.table_row("Created", decoded_attribute.create_time)
                renderer.table_row("File Modified",
                                   decoded_attribute.file_altered_time)
                renderer.table_row("MFT Modified",
                                   decoded_attribute.mft_altered_time)

                renderer.table_row("Accessed",
                                   decoded_attribute.file_accessed_time)

    def render_block_allocation(self, renderer, mft_entry):
        for attribute in mft_entry.attributes:
            if attribute.type == "$DATA":
                if attribute.is_resident:
                    return

                renderer.format("\nClusters ({0:d}-{1:d}):\n",
                                attribute.type, attribute.attribute_id)
                renderer.table_header([
                    ("c%s" % x, "c%s" % x, "25") for x in range(4)
                ], suppress_headers=True, nowrap=True)

                blocks = attribute.DecodeAttribute()
                for i in range(0, len(blocks), 8):
                    ranges = []
                    for (start, length) in blocks[i:i+8]:
                        if start is None:
                            ranges.append("Sparse(%s)" % length)
                        else:
                            ranges.append("%s-%s(%s)" % (
                                start, start + length, length))

                    renderer.table_row(*ranges)

    def comment(self, attribute):
        if attribute.type == "$FILE_NAME":
            return attribute.DecodeAttribute().name

        if attribute.type == "$DATA" and not attribute.is_resident:
            return "VCN: %s-%s" % (attribute.runlist_vcn_start,
                                   attribute.runlist_vcn_end)

        return ""

    def render_i30(self, renderer, mft_entry):
        if mft_entry.is_directory():
            renderer.format("\n$I30 Analysis:\n")
            renderer.table_header([
                ("MFT", "mft", ">10"),
                ("Seq", "seq", ">5"),
                ("Created", "created", "25"),
                ("File Mod", "file_mod", "25"),
                ("MFT Mod", "mft_mod", "25"),
                ("Access", "accessed", "25"),
                ("Size", "size", ">10"),
                ("Filename", "filename", ""),
            ])

            for record in mft_entry.list_files():
                file_record = record.file

                renderer.table_row(
                    record.mftReference,
                    record.seq_num,
                    file_record.created,
                    file_record.file_modified,
                    file_record.mft_modified,
                    file_record.file_accessed,
                    file_record.size,
                    file_record.name)

    def render(self, renderer):
        for mft in self.plugin_args.mfts:
            mft_entry = self.ntfs.mft[mft]

            renderer.format("MFT Entry Header Values:\n")
            renderer.format("Entry: {0:d}        Sequence: {1:d}\n",
                            mft, mft_entry.sequence_value)

            renderer.format("$LogFile Sequence Number: {0:d}\n",
                            mft_entry.logfile_sequence_number)
            renderer.format("Links: {0:d}\n\n", mft_entry.link_count)

            self.render_standard_info(renderer, mft_entry)

            renderer.format("\nAttributes:\n")
            renderer.table_header([
                ("Inode", "inode", ">15"),
                ("Type", "type", "30"),
                ("Name", "name", "10"),
                ("Res", "resident", "5"),
                ("Size", "size", ">10"),
                ("Comment", "comment", "")])

            for attribute in mft_entry.attributes:
                renderer.table_row(
                    "%d-%d-%d" % (attribute.owner_MFT, attribute.type,
                                  attribute.attribute_id),
                    attribute.type,
                    attribute.name,
                    attribute.is_resident,
                    attribute.size, self.comment(attribute))

            self.render_block_allocation(renderer, mft_entry)
            self.render_i30(renderer, mft_entry)


class FLS(FileBaseCommandMixin, NTFSPlugins):
    name = "fls"
    delegate = "ils"


class ILS(MFTPluginsMixin, NTFSPlugins):
    """List files in an NTFS image."""

    name = "ils"

    def render(self, renderer):
        for mft in self.plugin_args.mfts:
            directory = self.ntfs.mft[mft]

            # List all files inside this directory.
            renderer.table_header([
                ("MFT", "mft", ">10"),
                ("Seq", "seq", ">5"),
                ("Created", "created", "25"),
                ("File Mod", "file_mod", "25"),
                ("MFT Mod", "mft_mod", "25"),
                ("Access", "accessed", "25"),
                ("Size", "size", ">10"),
                ("Filename", "filename", ""),
            ])

            for record in directory.list_files():
                file_record = record.file

                renderer.table_row(
                    record.mftReference,
                    record.seq_num,
                    file_record.created,
                    file_record.file_modified,
                    file_record.mft_modified,
                    file_record.file_accessed,
                    file_record.size,
                    file_record.name)


class IDump(NTFSPlugins):
    """Dump a part of an MFT file."""
    name = "idump"

    __args = [
        dict(name="mft", type="IntParser", default=5,
             required=True, positional=True,
             help="MFT entry to dump."),

        dict(name="type", type="IntParser", default=128,
             required=False, positional=True,
             help="Attribute type to dump."),

        dict(name="id", type="IntParser", default=None,
             required=False, positional=True,
             help="Id of attribute to dump."),
    ]

    # Dump offset within the file.
    offset = 0

    def render(self, renderer):
        mft_entry = self.ntfs.mft[self.plugin_args.mft]
        attribute = mft_entry.get_attribute(
            self.plugin_args.type, self.plugin_args.id)
        data = attribute.data

        if data:
            dump_plugin = self.session.plugins.dump(
                offset=self.offset, address_space=data)
            dump_plugin.render(renderer)
            self.offset = dump_plugin.offset


class IExport(core.DirectoryDumperMixin, IDump):
    """Extracts files from NTFS.

    For each specified MFT entry, dump the file to the specified dump
    directory. The filename is taken as the longest filename of this MFT entry.
    """

    name = "iexport"

    def render(self, renderer):
        mft_entry = self.ntfs.mft[self.plugin_args.mft]
        filename = mft_entry.full_path or ("MFT_%s" % self.plugin_args.mft)
        attribute = mft_entry.get_attribute(self.plugin_args.type,
                                            self.plugin_args.id)

        in_as = attribute.data
        if in_as:
            renderer.format(
                "Writing MFT Entry {0} as {1}\n",
                self.plugin_args.mft, filename)

            with renderer.open(directory=self.dump_dir,
                               filename=filename, mode="wb") as out_fd:
                utils.CopyAStoFD(
                    in_as, out_fd, cb=lambda x, _: renderer.RenderProgress(
                        "Wrote %s bytes" % x))


class TestIExport(testlib.HashChecker):
    PARAMETERS = dict(
        commandline="iexport %(mfts)s --dump_dir %(tempdir)s"
    )


class TestIStat(testlib.SimpleTestCase):
    PARAMETERS = dict(
        commandline="istat %(mfts)s"
    )


class TestFStat(testlib.SimpleTestCase):
    PARAMETERS = dict(
        commandline="fstat %(path)s"
    )

class TestIDump(testlib.HashChecker):
    PARAMETERS = dict(
        commandline="idump %(mft)s %(type)s %(id)s",
        type=128,
        id=1
    )