Esempio n. 1
0
        private void CopyFiles(NtfsFileSystem sourceNtfs, NtfsFileSystem destNtfs, string path, bool subs)
        {
            if (subs)
            {
                foreach (var dir in sourceNtfs.GetDirectories(path))
                {
                    if (!IsExcluded(dir))
                    {
                        int  hardLinksRemaining = sourceNtfs.GetHardLinkCount(dir) - 1;
                        bool newDir             = false;

                        long   sourceFileId = sourceNtfs.GetFileId(dir);
                        string refPath;
                        if (_uniqueFiles.TryGetValue(sourceFileId, out refPath))
                        {
                            // If this is another name for a known dir, recreate the hard link
                            destNtfs.CreateHardLink(refPath, dir);
                        }
                        else
                        {
                            destNtfs.CreateDirectory(dir);
                            newDir = true;

                            FileAttributes fileAttrs = sourceNtfs.GetAttributes(dir);
                            if ((fileAttrs & FileAttributes.ReparsePoint) != 0)
                            {
                                destNtfs.SetReparsePoint(dir, sourceNtfs.GetReparsePoint(dir));
                            }

                            destNtfs.SetAttributes(dir, fileAttrs);

                            destNtfs.SetSecurity(dir, sourceNtfs.GetSecurity(dir));
                        }

                        // File may have a short name
                        string shortName = sourceNtfs.GetShortName(dir);
                        if (!string.IsNullOrEmpty(shortName) && shortName != dir)
                        {
                            destNtfs.SetShortName(dir, shortName);
                            --hardLinksRemaining;
                        }


                        if (newDir)
                        {
                            if (hardLinksRemaining > 0)
                            {
                                _uniqueFiles[sourceFileId] = dir;
                            }
                            CopyFiles(sourceNtfs, destNtfs, dir, subs);
                        }

                        // Set standard information last (includes modification timestamps)
                        destNtfs.SetFileStandardInformation(dir, sourceNtfs.GetFileStandardInformation(dir));
                    }
                }
            }

            foreach (var file in sourceNtfs.GetFiles(path))
            {
                Console.WriteLine(file);

                int hardLinksRemaining = sourceNtfs.GetHardLinkCount(file) - 1;

                long sourceFileId = sourceNtfs.GetFileId(file);

                string refPath;
                if (_uniqueFiles.TryGetValue(sourceFileId, out refPath))
                {
                    // If this is another name for a known file, recreate the hard link
                    destNtfs.CreateHardLink(refPath, file);
                }
                else
                {
                    CopyFile(sourceNtfs, destNtfs, file);

                    if (hardLinksRemaining > 0)
                    {
                        _uniqueFiles[sourceFileId] = file;
                    }
                }

                // File may have a short name
                string shortName = sourceNtfs.GetShortName(file);
                if (!string.IsNullOrEmpty(shortName) && shortName != file)
                {
                    destNtfs.SetShortName(file, shortName);
                }
            }
        }
Esempio n. 2
0
        public static void GetDirListing(string VMDKpath, string directory)
        {
            if (File.Exists(VMDKpath))
            {
                try
                {
                    using (VirtualDisk vhdx = VirtualDisk.OpenDisk(VMDKpath, FileAccess.Read))
                    {
                        if (vhdx.Partitions.Count > 1)
                        {
                            Console.WriteLine("Target has more than one partition");
                            for (var i = 0; i <= vhdx.Partitions.Count; i++)
                            {
                                NtfsFileSystem vhdbNtfs = new NtfsFileSystem(vhdx.Partitions[i].Open());
                                if (vhdbNtfs.DirectoryExists("\\\\" + directory))
                                {
                                    string[] filelist = vhdbNtfs.GetFiles(vhdbNtfs.Root.FullName + directory);
                                    string[] dirlist  = vhdbNtfs.GetDirectories(vhdbNtfs.Root.FullName + directory);

                                    foreach (var file in filelist)
                                    {
                                        Console.WriteLine("[F] {0}", file);
                                    }

                                    foreach (var dir in dirlist)
                                    {
                                        Console.WriteLine("[D] {0}", dir);
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("\r\n[*] Directory does not exist");
                                }
                            }
                        }
                        else
                        {
                            NtfsFileSystem vhdbNtfs = new NtfsFileSystem(vhdx.Partitions[0].Open());
                            if (vhdbNtfs.DirectoryExists("\\\\" + directory))
                            {
                                string[] filelist = vhdbNtfs.GetFiles(vhdbNtfs.Root.FullName + directory);
                                string[] dirlist  = vhdbNtfs.GetDirectories(vhdbNtfs.Root.FullName + directory);


                                foreach (var file in filelist)
                                {
                                    Console.WriteLine("[F] {0}  {1}", file, vhdbNtfs.GetFileLength(file));
                                }

                                foreach (var dir in dirlist)
                                {
                                    Console.WriteLine("[D] {0}", dir);
                                }
                            }
                            else
                            {
                                Console.WriteLine("\r\n[*] Directory does not exist");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("\r\n [!] Virtual Disk provided is not supported");
                    Console.WriteLine("\r\n [!] An exception occured: {0}", ex);
                    Environment.Exit(1);
                }
            }
            else
            {
                Console.WriteLine("\r\n [!] The provided VMDK image does not exist or can not be accessed");
            }
        }
Esempio n. 3
0
        public static void GetDirListing(string DiskPath, string directory)
        {
            if (File.Exists(DiskPath))
            {
                try
                {
                    VolumeManager volMgr = new VolumeManager();
                    VirtualDisk   vhdx   = VirtualDisk.OpenDisk(DiskPath, FileAccess.Read);
                    volMgr.AddDisk(vhdx);
                    VolumeInfo volInfo = null;
                    if (vhdx.Partitions.Count > 1)
                    {
                        Console.WriteLine("\r\n[*] Target has more than one partition\r\n");
                        foreach (var physVol in volMgr.GetPhysicalVolumes())
                        {
                            Console.WriteLine("      Identity: " + physVol.Identity);
                            Console.WriteLine("          Type: " + physVol.VolumeType);
                            Console.WriteLine("       Disk Id: " + physVol.DiskIdentity);
                            Console.WriteLine("      Disk Sig: " + physVol.DiskSignature.ToString("X8"));
                            Console.WriteLine("       Part Id: " + physVol.PartitionIdentity);
                            Console.WriteLine("        Length: " + physVol.Length + " bytes");
                            Console.WriteLine(" Disk Geometry: " + physVol.PhysicalGeometry);
                            Console.WriteLine("  First Sector: " + physVol.PhysicalStartSector);
                            Console.WriteLine();
                            if (!string.IsNullOrEmpty(physVol.Identity))
                            {
                                volInfo = volMgr.GetVolume(physVol.Identity);
                            }

                            using (NtfsFileSystem vhdbNtfs = new NtfsFileSystem(physVol.Partition.Open()))
                            {
                                if (vhdbNtfs.DirectoryExists("\\\\" + directory))
                                {
                                    string[] filelist = vhdbNtfs.GetFiles(vhdbNtfs.Root.FullName + directory);
                                    string[] dirlist  = vhdbNtfs.GetDirectories(vhdbNtfs.Root.FullName + directory);

                                    foreach (var file in filelist)
                                    {
                                        Console.WriteLine("[F] {0}  {1}", file, vhdbNtfs.GetFileLength(file));
                                    }

                                    foreach (var dir in dirlist)
                                    {
                                        Console.WriteLine("[D] {0}", dir);
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("\r\n[*] Directory does not exist in partition {0}\r\n",
                                                      physVol.Identity);
                                }
                            }
                        }
                    }
                    else //No partitions
                    {
                        Console.WriteLine("\r\n[*] Found only one partition\r\n");
                        Console.WriteLine("LOGICAL VOLUMES");
                        foreach (var logVol in volMgr.GetLogicalVolumes())
                        {
                            Console.WriteLine("      Identity: " + logVol.Identity);
                            Console.WriteLine("        Length: " + logVol.Length + " bytes");
                            Console.WriteLine(" Disk Geometry: " + logVol.PhysicalGeometry);
                            Console.WriteLine("  First Sector: " + logVol.PhysicalStartSector);
                            Console.WriteLine();
                        }

                        using (NtfsFileSystem vhdbNtfs = new NtfsFileSystem(vhdx.Partitions[0].Open()))
                        {
                            if (vhdbNtfs.DirectoryExists("\\\\" + directory))
                            {
                                string[] filelist = vhdbNtfs.GetFiles(vhdbNtfs.Root.FullName + directory);
                                string[] dirlist  = vhdbNtfs.GetDirectories(vhdbNtfs.Root.FullName + directory);


                                foreach (var file in filelist)
                                {
                                    Console.WriteLine("[F] {0}  {1}", file, vhdbNtfs.GetFileLength(file));
                                }

                                foreach (var dir in dirlist)
                                {
                                    Console.WriteLine("[D] {0}", dir);
                                }
                            }
                            else
                            {
                                Console.WriteLine("\r\n[*] Directory does not exist");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception {0}", ex);
                }
            }
        }