Example #1
0
        public uint Def_CreateFile(string filename, System.IO.FileAccess access, System.IO.FileShare share, System.IO.FileMode mode, System.IO.FileOptions options, IntPtr info)
        {
            //return 0;
            RegistryFileBlock rb = new RegistryFileBlock(filename);

            if (rb.ValueKind > 0)
            {
                if (mode == FileMode.CreateNew || mode == FileMode.OpenOrCreate)
                {
                    if (rb.level == 2)
                    {
                        GetRegistoryEntry(rb).CreateSubKey(rb.KeyPointer.Substring(rb.KeyPointer.LastIndexOf("\\") + 1));
                        return(0);
                    }
                    int check;

                    if (rb.level == 3 && filename.EndsWith(regtypes[rb.ValueKind], StringComparison.CurrentCultureIgnoreCase))
                    {
                        switch (rb.ValueKind)
                        {
                        case 1:
                            GetRegistoryEntry(rb).SetValue(rb.ValueName, 0, RegValueType[rb.ValueKind]);
                            break;

                        case 2:
                            GetRegistoryEntry(rb).SetValue(rb.ValueName, "", RegValueType[rb.ValueKind]);
                            break;

                        case 3:
                            GetRegistoryEntry(rb).SetValue(rb.ValueName, 0, RegValueType[rb.ValueKind]);
                            break;

                        case 4:
                            GetRegistoryEntry(rb).SetValue(rb.ValueName, "", RegValueType[rb.ValueKind]);
                            break;

                        default:
                            return(0xC000000F);
                        }
                        return(0);
                    }
                }
            }
            else
            {
                if (filename == "\\")
                {
                    return(0);
                }
                if (filename.EndsWith("dekstop.ini", StringComparison.CurrentCultureIgnoreCase) || filename.EndsWith("autorun.inf", StringComparison.CurrentCultureIgnoreCase))
                {
                    return(0);
                }
                // else
                // Console.WriteLine("Invalid New File {0}",filename);
                return(0xC000000F);
            }
            return(0xC000000F);
        }
Example #2
0
        public uint Def_GetFileInformation(string fileName, ref BY_HANDLE_FILE_INFORMATION information, IntPtr info)
        {
            try
            {
                RegistryFileBlock FileBlock = new RegistryFileBlock(fileName);


                if (FileBlock.level == 1)
                {
                    information.FileAttributes = (uint)FileAttributes.Directory;
                    information.LastAccessTime = HelperFunction.DateTimeToFileTime(DateTime.Now.ToFileTime());
                    information.LastWriteTime  = HelperFunction.DateTimeToFileTime(DateTime.Now.ToFileTime());
                    information.CreationTime   = HelperFunction.DateTimeToFileTime(DateTime.Now.ToFileTime());


                    return(0);
                }
                RegistryKey key = GetRegistoryEntry(FileBlock);
                if (key == null)
                {
                    return(0xC0000467);
                }
                if (FileBlock.level == 3)
                {
                    //  fileName = fileName.Remove(fileName.LastIndexOf("."));
                    string value;
                    if (key.GetValue(FileBlock.ValueName) == null)//.ToString();
                    {
                        value = "";
                    }
                    else
                    {
                        value = key.GetValue(FileBlock.ValueName).ToString();
                    }
                    information.FileAttributes = (uint)FileAttributes.Readonly;
                    information.FileSizeLow    = (uint)value.Length;
                    information.LastAccessTime = HelperFunction.DateTimeToFileTime(DateTime.Now.ToFileTime());
                    information.LastWriteTime  = HelperFunction.DateTimeToFileTime(DateTime.Now.ToFileTime());
                    information.CreationTime   = HelperFunction.DateTimeToFileTime(DateTime.Now.ToFileTime());

                    return(0);
                }
                information.FileAttributes = (uint)FileAttributes.Directory;
                information.LastAccessTime = HelperFunction.DateTimeToFileTime(DateTime.Now.ToFileTime());
                information.LastWriteTime  = HelperFunction.DateTimeToFileTime(DateTime.Now.ToFileTime());
                information.CreationTime   = HelperFunction.DateTimeToFileTime(DateTime.Now.ToFileTime());
                return(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error : {0}", ex.Message);
                return(0xC0000467);
            }
        }
Example #3
0
        public uint Def_OpenDirectory(string filename, IntPtr info)
        {
            RegistryFileBlock rb = new RegistryFileBlock(filename);

            if (rb.ValueKind >= 0)
            {
                // Console.WriteLine("----------{0}---------", filename);
                //Console.WriteLine("KeyPointer={0},\n RootDIr={1},\n ValueKind={2},\n ValueNmae={3},\n level={4},\n", rb.KeyPointer, rb.RootDir, rb.ValueKind, rb.ValueName, rb.level);
                return(0);
            }
            else
            {
                if (filename == "\\")
                {
                    return(0);
                }
                return(0xC000000F);
            }
        }
Example #4
0
        private RegistryKey GetRegistoryEntry(RegistryFileBlock block)
        {
            try
            {
                switch (block.level)
                {
                //case 1:
                // if(block.RootDir=""
                case 2:
                    if (block.KeyPointer == "")
                    {
                        if (RootDirectory.ContainsKey(block.RootDir))
                        {
                            return(RootDirectory[block.RootDir]);
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        return(RootDirectory[block.RootDir].OpenSubKey(block.KeyPointer, true));
                    }
                    break;

                case 3:
                    return(RootDirectory[block.RootDir].OpenSubKey(block.KeyPointer, true));

                    break;

                default:
                    return(null);

                    break;
                }
            }
            catch (Exception ex)
            {
                // Console.WriteLine("Error:-" + ex.Message + ":" + name);
                return(null);
            }
        }
Example #5
0
        public uint Def_FindFiles(string fileName, ref FillFindData NativeFillFunction, IntPtr info)
        {
            try
            {
                RegistryFileBlock FileBlock = new RegistryFileBlock(fileName);
                if (FileBlock.level == 1)
                {
                    foreach (string name in RootDirectory.Keys)
                    {
                        WIN32_FIND_DATA finfo = new WIN32_FIND_DATA();
                        finfo.cFileName        = name;
                        finfo.dwFileAttributes = (uint)FileAttributes.Directory;
                        finfo.ftLastAccessTime = HelperFunction.DateTimeToFileTime(DateTime.Now.ToFileTime());
                        finfo.ftLastWriteTime  = HelperFunction.DateTimeToFileTime(DateTime.Now.ToFileTime());
                        finfo.ftCreationTime   = HelperFunction.DateTimeToFileTime(DateTime.Now.ToFileTime());
                        NativeFillFunction(ref finfo, info);
                    }
                    return(0);
                }
                else
                {
                    RegistryKey key = GetRegistoryEntry(FileBlock);
                    if (key == null)
                    {
                        Console.WriteLine("Find File Error {0}", fileName);
                        return(0xC0000467);
                    }
                    foreach (string name in key.GetSubKeyNames())
                    {
                        WIN32_FIND_DATA finfo = new WIN32_FIND_DATA();
                        finfo.cFileName        = name;
                        finfo.dwFileAttributes = (uint)FileAttributes.Directory;
                        finfo.ftLastAccessTime = HelperFunction.DateTimeToFileTime(DateTime.Now.ToFileTime());
                        finfo.ftLastWriteTime  = HelperFunction.DateTimeToFileTime(DateTime.Now.ToFileTime());
                        finfo.ftCreationTime   = HelperFunction.DateTimeToFileTime(DateTime.Now.ToFileTime());
                        NativeFillFunction(ref finfo, info);
                    }
                    foreach (string name in key.GetValueNames())
                    {
                        WIN32_FIND_DATA finfo = new WIN32_FIND_DATA();
                        switch (key.GetValueKind(name))
                        {
                        case RegistryValueKind.Binary:
                            finfo.cFileName = name + ".regbin";
                            break;

                        case RegistryValueKind.DWord:
                            finfo.cFileName = name + ".regdword";
                            break;

                        case RegistryValueKind.MultiString:
                            finfo.cFileName = name + ".regmltstr";
                            break;

                        case RegistryValueKind.QWord:
                            finfo.cFileName = name + ".regqwrod";
                            break;

                        case RegistryValueKind.String:
                            finfo.cFileName = name + ".regstr";
                            break;

                        case RegistryValueKind.ExpandString:
                            finfo.cFileName = name + ".regexpstr";
                            break;

                        case RegistryValueKind.Unknown:
                            finfo.cFileName = name + ".regunk";
                            break;

                        default:
                            break;
                        }


                        // finfo.cFileName = name;
                        finfo.dwFileAttributes = (uint)FileAttributes.Readonly;
                        finfo.ftLastAccessTime = HelperFunction.DateTimeToFileTime(DateTime.Now.ToFileTime());
                        finfo.ftLastWriteTime  = HelperFunction.DateTimeToFileTime(DateTime.Now.ToFileTime());
                        finfo.ftCreationTime   = HelperFunction.DateTimeToFileTime(DateTime.Now.ToFileTime());
                        finfo.nFileSizeLow     = (uint)key.GetValue(name).ToString().Length;
                        NativeFillFunction(ref finfo, info);
                    }
                    return(0);
                }
            }
            catch (Exception e) {
                Console.WriteLine(e.Message);
                return(0xC0000467);
            }
        }
Example #6
0
        public uint Def_WriteFile(string fileName, byte[] buffer, ref uint writtenBytes, long offset, IntPtr info)
        {
            try
            {
                //   Console.WriteLine("Write {0}", fileName);
                RegistryFileBlock FileBlock = new RegistryFileBlock(fileName);

                if (FileBlock.level == 3)
                {
                    string      extension = fileName.Substring(fileName.LastIndexOf("."));
                    RegistryKey key       = GetRegistoryEntry(FileBlock);
                    // fileName = fileName.Remove(fileName.LastIndexOf("."));
                    if (key == null)
                    {
                        Console.WriteLine("Key Error {0}", fileName.Replace(".value", ""));
                        return(0xC000046);
                    }
                    string value;
                    if (key.GetValue(FileBlock.ValueName) == null)//.ToString();
                    {
                        value = "";
                    }
                    else
                    {
                        value = key.GetValue(FileBlock.ValueName).ToString();
                    }
                    byte[] file = Encoding.ASCII.GetBytes(value);
                    using (MemoryStream memory = new MemoryStream())
                    {
                        memory.Seek(offset, SeekOrigin.Begin);
                        //memory.Write(file,offset, file.Length);
                        memory.Write(buffer, (int)offset, buffer.Length);

                        byte[] finalValue = memory.ToArray();

                        string val = System.Text.Encoding.UTF8.GetString(finalValue);
                        switch (extension.Replace(".reg", ""))
                        {
                        case "str":
                            Console.WriteLine("Value Wroting STR {0}", val);
                            key.SetValue(FileBlock.ValueName, val, RegistryValueKind.String);
                            break;

                        case "qword":
                            long lval;
                            if (long.TryParse(val, out lval))
                            {
                                key.SetValue(FileBlock.ValueName, lval, RegistryValueKind.QWord);
                            }
                            else
                            {
                                Console.WriteLine("Very Bad Format[Qword]  {0}", val);
                                return(0xC0000467);
                            }
                            break;

                        case "mtlstr":
                            key.SetValue(FileBlock.ValueName, val, RegistryValueKind.MultiString);
                            break;

                        case "dword":
                            //System.Text.Encoding.UTF8.GetString(finalValue);// BitConverter.ToString(finalValue, 0);
                            int x;
                            if (int.TryParse(val, out x))
                            {
                                Console.WriteLine("Format Accepted  {0} \n {1} \n {2} {3}", val, FileBlock.ValueName, FileBlock.KeyPointer, key.Name);
                                key.SetValue(FileBlock.ValueName, x, RegistryValueKind.DWord);
                                return(0);
                            }
                            else
                            {
                                Console.WriteLine("Very Bad Forma[Dword]t  {0}", val);
                                return(0xC0000467);
                            }
                            break;

                        default:
                            //       Console.WriteLine("Not Implemented format " + extension.Replace("reg", ""));
                            return(0xC0000467);

                            break;
                        }
                    }
                    return(0);
                }
                else
                {
                    //Console.WriteLine("Trying to reader folder" + fileName);
                }


                return(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Write File Error {0}  Line # {1} ", ex.Message, (new StackTrace(ex, true)).GetFrame(0).GetFileLineNumber());
                return(0xC0000467);
            }
        }
Example #7
0
        public uint Def_ReadFile(string fileName, IntPtr buffer, uint BufferSize, ref uint NumberByteReadSuccess, long Offset, IntPtr info)
        {
            int fileLengnth = 0;

            try
            {
                //  Console.WriteLine("Read {0}", fileName);
                RegistryFileBlock FileBlock = new RegistryFileBlock(fileName);

                if (FileBlock.level == 3)
                {
                    RegistryKey key = GetRegistoryEntry(FileBlock);
                    fileName = fileName.Remove(fileName.LastIndexOf("."));
                    if (key == null)
                    {
                        // Console.WriteLine("Key Error {0}", fileName.Replace(".value", ""));
                        return(0xC000046);
                    }
                    string value;
                    if (key.GetValue(FileBlock.ValueName) == null)//.ToString();
                    {
                        value = "";
                    }
                    else
                    {
                        value = key.GetValue(FileBlock.ValueName).ToString();
                    }

                    byte[] file = Encoding.ASCII.GetBytes(value);
                    fileLengnth = file.Length;
                    if (BufferSize > file.Length - Offset)
                    {
                        NumberByteReadSuccess = (uint)(fileLengnth - Offset);
                        System.Runtime.InteropServices.Marshal.Copy(file, (int)Offset, buffer, (int)NumberByteReadSuccess);
                    }
                    else
                    {
                        NumberByteReadSuccess = BufferSize;
                        System.Runtime.InteropServices.Marshal.Copy(file, (int)Offset, buffer, (int)BufferSize);
                    }
                    return(0);
                }

                else if (FileBlock.level > 0)
                {
                    RegistryKey x = GetRegistoryEntry(FileBlock);
                    if (x != null)
                    {
                        string val  = "Description of the folder\n---------------------------- \n \n1 Total Sub Key=" + x.GetSubKeyNames().Length + " \nTotal Value at this node " + x.GetValueNames().Length + "\n";
                        byte[] file = Encoding.ASCII.GetBytes(val);
                        // Create a memory stream from those bytes.
                        fileLengnth = file.Length;
                        if (BufferSize > file.Length - Offset)
                        {
                            NumberByteReadSuccess = (uint)(fileLengnth - Offset);
                            System.Runtime.InteropServices.Marshal.Copy(file, (int)Offset, buffer, (int)NumberByteReadSuccess);
                        }
                        else
                        {
                            NumberByteReadSuccess = BufferSize;
                            System.Runtime.InteropServices.Marshal.Copy(file, (int)Offset, buffer, (int)BufferSize);
                        }
                    }
                    else
                    {
                        return(0xC000000F);
                    }
                    return(0);
                }


                return(0);
            }
            catch (Exception ex)
            {
                //Get a StackTrace object for the exception
                StackTrace st = new StackTrace(0, true);

                //Get the first stack frame
                StackFrame frame = st.GetFrame(0);

                //Get the file name
                string fileNa = frame.GetFileName();

                //Get the method name
                string methodName = frame.GetMethod().Name;

                //Get the line number from the stack frame
                int line = frame.GetFileLineNumber();

                //Get the column number
                int col = frame.GetFileColumnNumber();

                Console.WriteLine("Read Error {0} with| BufferSize {1}| \n| FileLegnth {2} | \n |Offset {3}|", ex.Message, BufferSize, fileLengnth, Offset);


                return(0xC0000467);
            }
        }