Esempio n. 1
0
        private void LoadFile(string file)
        {
            List <int> valid = new List <int>();

            byte[] magicbuffer = new byte[0x10];
            int    read        = 0;

            FileStream fs    = null;
            string     fname = Path.GetFileName(file);

            try
            {
                try
                {
                    fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read);
                }
                catch (IOException ex)
                {
                    if (!ex.Message.Contains("used by another process"))
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    if (MessageBox.Show("The file could not be write-locked, would you like to make a temporary copy and read that instead?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != System.Windows.Forms.DialogResult.Yes)
                    {
                        return;
                    }

                    byte[]        hash = NayukiSHA256.Calculate(Encoding.ASCII.GetBytes(file));
                    StringBuilder sb   = new StringBuilder(hash.Length * 2);
                    foreach (byte b in hash)
                    {
                        sb.Append(b.ToString("X2"));
                    }

                    string newf = Root.LocalAppData + sb.ToString() + ".temp";
                    File.Copy(file, newf);
                    file = newf;

                    fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read);
                }

                if (fs.Length < 1)
                {
                    return;
                }
                if ((read = fs.Read(magicbuffer, 0, magicbuffer.Length)) < 1)
                {
                    return;
                }

                for (int i = 0; i < Root.ModuleAttribs.Count; i++)
                {
                    if (fs.Length < Root.ModuleAttribs[i].MinFileSize)
                    {
                        continue;
                    }

                    if (Root.ModuleAttribs[i].Magic != null && Root.ModuleAttribs[i].Magic.Length > 0 && Root.ModuleAttribs[i].Magic.Length <= magicbuffer.Length)
                    {
                        int j = 0;
                        for (; j < Root.ModuleAttribs[i].Magic.Length; j++)
                        {
                            if (Root.ModuleAttribs[i].Magic[j] != magicbuffer[j])
                            {
                                break;
                            }
                        }

                        if (j < Root.ModuleAttribs[i].Magic.Length)
                        {
                            continue;
                        }
                    }

                    fs.Seek(0, SeekOrigin.Begin);
                    if (Root.Modules[i].CanRead(fs))
                    {
                        valid.Add(i);
                    }
                }

                this.Invoke((Action) delegate
                {
                    if (valid.Count < 1)
                    {
                        MessageBox.Show("No modules that were able to read the specified file could be found.", "Unsupported Format", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (valid.Count > 1)
                    {
                        MessageBox.Show("More than one applicable module.", "NYI", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        fs.Seek(0, SeekOrigin.Begin);
                        RootInfoNode root = new RootInfoNode(fname, file, Root.Modules[valid[0]])
                        {
                            ImageKey = "file", SelectedImageKey = "file"
                        };
                        Root.Modules[valid[0]].Read(root, fs);

                        tree.BeginUpdate();
                        Bridge.AddRootNode(root);
                        tree.EndUpdate();
                    }
                });
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                    fs.Dispose();
                }
            }
        }
Esempio n. 2
0
        private void Startup_Load(object sender, EventArgs e)
        {
            BackgroundWorker bg = new BackgroundWorker();

            bg.DoWork += delegate
            {
                this.Invoke((Action) delegate { lbl.Text = "Checking for necessary files and folders."; });
                if (!Directory.Exists("modules"))
                {
                    Directory.CreateDirectory("modules");
                }
                if (!File.Exists("modules\\trusted"))
                {
                    File.Create("modules\\trusted").Close();
                }

                string lad = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\FileOptics\";
                if (!Directory.Exists(lad))
                {
                    Directory.CreateDirectory(lad);
                }
                Root.LocalAppData = lad;

                string[] tempfs = Directory.GetFiles(Root.LocalAppData, "*.temp", SearchOption.TopDirectoryOnly);
                if (tempfs.Length > 0)
                {
                    this.Invoke((Action) delegate { lbl.Text = "Cleaning temporary files."; });
                    foreach (string tempf in tempfs)
                    {
                        File.Delete(tempf);
                    }
                }

                this.Invoke((Action) delegate { lbl.Text = "Reading trusted module checksum list."; });
                using (FileStream s = File.Open("modules\\trusted", FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    byte[] buffer = new byte[32];
                    int    read;
                    while ((read = s.Read(buffer, 0, buffer.Length)) == buffer.Length)
                    {
                        Root.TrustedModules.Add(buffer);
                    }
                }

                this.Invoke((Action) delegate { lbl.Text = "Finding modules."; });
                string[] files = Directory.GetFiles("modules", "*.dll", SearchOption.TopDirectoryOnly);
                foreach (string file in files)
                {
                    this.Invoke((Action) delegate { lbl.Text = String.Format("Reading '{0}'.", Path.GetFileName(file)); });
                    if (!File.Exists(file))
                    {
                        continue;
                    }

                    NayukiSHA256 sha256 = new NayukiSHA256();
                    sha256.AppendFile(file, true);

                    int i = 0;
                    foreach (byte[] trusted in Root.TrustedModules)
                    {
                        for (i = 0; i < 32; i++)
                        {
                            if (trusted[i] != sha256.Hash[i])
                            {
                                break;
                            }
                        }
                        if (i >= 32)
                        {
                            break;
                        }
                    }
                    if (i < 32)
                    {
                        bool scon = false;
                        this.Invoke((Action) delegate
                        {
                            if (MessageBox.Show(String.Format("The module '{0}' has not yet been trusted. Would you like to add it to the trusted module list? (Note: Modules are like programs, they can be just as dangerous as running an exe. Please be responsible with trusting modules of unknown origin)\r\nChoosing 'No' will result in the module not being loaded.", Path.GetFileName(file)), "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != System.Windows.Forms.DialogResult.Yes)
                            {
                                scon = true;
                            }
                        });
                        if (scon)
                        {
                            continue;
                        }

                        //File.WriteAllBytes("modules\\trusted", sha256.Hash);
                        using (FileStream trusted = File.Open("modules\\trusted", FileMode.Append, FileAccess.Write, FileShare.Read))
                            trusted.Write(sha256.Hash, 0, sha256.Hash.Length);
                    }

                    Assembly asm = Assembly.LoadFrom(file);
                    foreach (Type t in asm.GetTypes())
                    {
                        if (t.GetInterface("IModule") == null)
                        {
                            continue;
                        }
                        ModuleAttrib[] mods = (ModuleAttrib[])t.GetCustomAttributes(typeof(ModuleAttrib), false);
                        if (mods.Length != 1)
                        {
                            continue;
                        }

                        mods[0].Name += String.Format(" ({0})", t.Namespace);
                        Root.ModuleAttribs.Add(mods[0]);
                        Root.Modules.Add((IModule)Activator.CreateInstance(t));
                    }
                }
                this.Invoke((Action) delegate { lbl.Text = "Complete!"; Canceled = false; this.Close(); });
            };
            bg.RunWorkerAsync();
        }
Esempio n. 3
0
        public Main()
        {
            InitializeComponent();

            imglTree.Images.Add("null", Properties.Resources.tree_null);
            imglTree.Images.Add("file", Properties.Resources.tree_file);
            imglTree.Images.Add("image", Properties.Resources.tree_img);
            imglTree.Images.Add("info", Properties.Resources.tree_info);
            imglTree.Images.Add("error", Properties.Resources.tree_error);
            imglTree.Images.Add("binary", Properties.Resources.tree_binary);
            imglTree.Images.Add("block", Properties.Resources.tree_block_blue);          //Regular data block
            imglTree.Images.Add("block-orange", Properties.Resources.tree_block_orange); //Data block that could not be accounted for
            imglTree.Images.Add("block-red", Properties.Resources.tree_block_red);       //Data block that has been marked for deletion
            imglTree.Images.Add("block-purple", Properties.Resources.tree_block_purple); //Most significant data block(s), ie. image data
            imglTree.Images.Add("block-trueblue", Properties.Resources.tree_block_trueblue);
            imglTree.Images.Add("int", Properties.Resources.tree_int);
            imglTree.Images.Add("byte", Properties.Resources.tree_byte);
            imglTree.Images.Add("str", Properties.Resources.tree_str);
            imglTree.Images.Add("unknown", Properties.Resources.tree_unknown);

            imgInfo.Resize += delegate(object sender, EventArgs e)
            {
                if (imgInfo.Image == null)
                {
                    return;
                }

                if (imgInfo.Width > imgInfo.Image.Width &&
                    imgInfo.Height > imgInfo.Image.Height)
                {
                    imgInfo.SizeMode = PictureBoxSizeMode.CenterImage;
                }
                else
                {
                    imgInfo.SizeMode = PictureBoxSizeMode.Zoom;
                }
            };

            //tree.KeyDown += delegate
            //{
            //    RootInfoNode rin = new RootInfoNode("Generic root", "FileOptics.exe", null);
            //    Bridge.AddRootNode(rin);
            //    Bridge.AppendNode(new InfoNode("PE", InfoType.None, null, DataType.Useless, 0x00, 0x53) { HighlightColor = Color.Red }, rin);
            //};

            foreach (Control c in splitContainer2.Panel1.Controls)
            {
                if (c.Name.StartsWith("pInfo"))
                {
                    infopanels.Add((Panel)c);
                    c.Visible = false;
                    c.Dock    = DockStyle.Fill;
                }
            }

            this.Shown += delegate
            {
                this.Invoke((Action) delegate
                {
                    using (Startup start = new Startup())
                    {
                        start.ShowDialog();
                        if (start.Canceled)
                        {
                            this.Close();
                        }
                    }
                });
            };

            this.AllowDrop = true;
            this.DragOver += delegate(object sender, DragEventArgs e)
            { e.Effect = (e.Data.GetDataPresent(DataFormats.FileDrop) ? DragDropEffects.All : DragDropEffects.None); };
            this.DragDrop += delegate(object sender, DragEventArgs e)
            {
                if (!e.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    return;
                }

                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

                BackgroundWorker bg = new BackgroundWorker();

                //this.BeginInvoke((Action)delegate
                bg.DoWork += delegate
                {
                    if (files.Length != 1)
                    {
                        MessageBox.Show("Please only drop one file at a time!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    List <int> valid       = new List <int>();
                    byte[]     magicbuffer = new byte[0x10];
                    int        read        = 0;

                    FileStream fs    = null;
                    string     fname = Path.GetFileName(files[0]);
                    try
                    {
                        fs = File.Open(files[0], FileMode.Open, FileAccess.Read, FileShare.Read);
                    }
                    catch (IOException ex)
                    {
                        if (!ex.Message.Contains("used by another process"))
                        {
                            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        if (MessageBox.Show("The file could not be write-locked, would you like to make a temporary copy and read that instead?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != System.Windows.Forms.DialogResult.Yes)
                        {
                            return;
                        }

                        byte[]        hash = NayukiSHA256.Calculate(Encoding.ASCII.GetBytes(files[0]));
                        StringBuilder sb   = new StringBuilder(hash.Length * 2);
                        foreach (byte b in hash)
                        {
                            sb.Append(b.ToString("X2"));
                        }

                        string newf = Root.LocalAppData + sb.ToString() + ".temp";
                        File.Copy(files[0], newf);
                        files[0] = newf;

                        fs = File.Open(files[0], FileMode.Open, FileAccess.Read, FileShare.Read);
                    }

                    //using (FileStream fs = File.Open(files[0], FileMode.Open, FileAccess.Read, FileShare.Read))
                    try
                    {
                        if (fs.Length < 1)
                        {
                            return;
                        }
                        if ((read = fs.Read(magicbuffer, 0, magicbuffer.Length)) < 1)
                        {
                            return;
                        }

                        for (int i = 0; i < Root.ModuleAttribs.Count; i++)
                        {
                            if (fs.Length < Root.ModuleAttribs[i].MinFileSize)
                            {
                                continue;
                            }

                            if (Root.ModuleAttribs[i].Magic != null && Root.ModuleAttribs[i].Magic.Length > 0 && Root.ModuleAttribs[i].Magic.Length <= magicbuffer.Length)
                            {
                                int j = 0;
                                for (; j < Root.ModuleAttribs[i].Magic.Length; j++)
                                {
                                    if (Root.ModuleAttribs[i].Magic[j] != magicbuffer[j])
                                    {
                                        break;
                                    }
                                }

                                if (j < Root.ModuleAttribs[i].Magic.Length)
                                {
                                    continue;
                                }
                            }

                            fs.Seek(0, SeekOrigin.Begin);
                            if (Root.Modules[i].CanRead(fs))
                            {
                                valid.Add(i);
                            }
                        }

                        //StringBuilder sb = new StringBuilder();
                        //foreach (int i in valid)
                        //    sb.AppendLine(Root.ModuleAttribs[i].Name);
                        //MessageBox.Show(sb.ToString(), "Valid Modules", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        this.Invoke((Action) delegate
                        {
                            if (valid.Count < 1)
                            {
                                MessageBox.Show("No modules that were able to read the specified file could be found.", "Unsupported Format", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }
                            else if (valid.Count > 1)
                            {
                                MessageBox.Show("More than one applicable module.", "NYI", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }
                            else
                            {
                                fs.Seek(0, SeekOrigin.Begin);
                                RootInfoNode root = new RootInfoNode(fname, files[0], Root.Modules[valid[0]])
                                {
                                    ImageKey = "file", SelectedImageKey = "file"
                                };
                                Root.Modules[valid[0]].Read(root, fs);
                                Bridge.AddRootNode(root);
                            }
                        });
                    }
                    finally
                    {
                        if (fs != null)
                        {
                            fs.Close();
                            fs.Dispose();
                        }
                    }
                };
                //});
                bg.RunWorkerAsync();
            };
        }