lunedì 5 aprile 2021

Eseguire automaticamente un antivirus su un file scaricato usando Chrome , alla fine del download

 Il codice è in versione pre-alpha . Qui il codice sorgente https://sourceforge.net/projects/antivirusdefenderforchrome/  . 
Supponiamo di utilizzare su un pc due antivirus: ad esempio Avira e Defender , il primo di occupa della scansione a realtime , il secondo viene utilizzato per eseguire delle scansioni periodiche , in questo modo si può usufruire,su richiesta, di due distinte liste aggiornate di segnature di virus per effettuare le scansioni e di due motori euristici .
Durante il download, il file scaricato da Chrome nella dir Download assume una o più fra tre estensioni temporanee : ".tmp" , ".crdownload",".part" ,gli altri browser hanno funzionamenti differenti ed il codice seguente richiede l'uso di una versione attuale di Chrome ,se il file ha un'altra estensione significa che il download è finito, il cambiamento del nome del file viene intercettato dall'evento Renamed  dell'oggetto FileSystemWatcher  e l'estensione è verificata dal metodo FindExtension :
scegliamo la directory da monitorare :







 public partial class Form1 : Form

    {
        string selectedPath = null;
        FileSystemWatcher watcher = new FileSystemWatcher();
        
        public Form1()
        {
            InitializeComponent();
        }             

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = this.folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                selectedPath = folderBrowserDialog1.SelectedPath;
                this.textBox1.Text = selectedPath;
            }
            watcher.Path = selectedPath;
            watcher.NotifyFilter = NotifyFilters.FileName;
            watcher.Filter = "*.*";
            watcher.Created += new FileSystemEventHandler(OnCreated);
            watcher.Renamed += new RenamedEventHandler(OnRenamed);
            watcher.EnableRaisingEvents = true;            
        }


 per eseguire chiamate  threadsafe  al controllo textBox2 utilizziamo la classe ParametrizedThread  ,

 il percorso sul vostro pc del file MpCmdRun.exe (l'eseguibile da linea di comando di Defender) potrebbe essere diverso da  "C:\Program Files\Windows Defender\MpCmdRun.exe" :

private void OnCreated(object source, FileSystemEventArgs e)
        {
            string name = e.Name;
            string s = this.textBox2.Text;
            s = s + name + " created file " + Environment.NewLine;
            ParametrizedThread p = new ParametrizedThread();
            p.currentForm = this;
            p.c = this.textBox2;
            p.text = s;
            Thread t = new Thread(p.ThreadProcSafe);
            t.Start();
        }

private void OnRenamed(object source, FileSystemEventArgs e)

        {   
            string ext=Path.GetExtension(e.FullPath);
            bool b = FindExtension(ext);
            if (!b)
            {
                string name = e.Name;
                string s = this.textBox2.Text;
                s = s + name + " renamed file completo" + Environment.NewLine;
                ParametrizedThread p = new ParametrizedThread();
                p.currentForm = this;
                p.c = this.textBox2;
                p.text = s;
                Thread t = new Thread(p.ThreadProcSafe);
                t.Start();
                
                ProcessStartInfo info = new ProcessStartInfo(@"C:\Program Files\Windows Defender\MpCmdRun.exe");               
                string fileName = e.FullPath;
                info.Arguments = " -Scan -ScanType 3 -File " + '"' + fileName + '"';
                info.WindowStyle = ProcessWindowStyle.Maximized;
                info.UseShellExecute = false;
                info.RedirectStandardOutput = true;
                Process proc = Process.Start(info);
                StreamReader sr = proc.StandardOutput;
                string msg = sr.ReadToEnd();
                string w = this.textBox2.Text;
                w = w + msg + Environment.NewLine;
                ParametrizedThread pt = new ParametrizedThread();
                pt.currentForm = this;
                pt.c = this.textBox2;
                pt.text = w;
                Thread t1 = new Thread(pt.ThreadProcSafe);
                t1.Start();
            } else { 
                string name = e.Name;
                string s = this.textBox2.Text;
                s = s + name + " renamed" + Environment.NewLine;
                ParametrizedThread p = new ParametrizedThread();
                p.currentForm = this;
                p.c = this.textBox2;
                p.text = s;
                Thread t = new Thread(p.ThreadProcSafe);
                t.Start();
            }
        }

         private bool FindExtension(string ext )
        {
            bool b = true;
            List<string> l = new List<string>();
            //funziona solo con chrome
            l.Add(".tmp");
            l.Add(".crdownload");
            l.Add(".part");            
            int result=l.FindIndex(element => element==ext);
            if(result == -1)
            {
                b = false;
            }
            return b;
        }
         ...................................
         ................................
         ...........................


    public class ParametrizedThread
    {

        public TextBox c = null;
        public string text = null;        
        delegate void SetTextCallback(string text ,Control c);       
        public Form currentForm = null;        
        public ParametrizedThread()
        {
        }
        public void ThreadProcSafe()
        {
            SetText(this.text,this.c);
        }           

        private void SetText(string text,Control c)
        {            
            if (c.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                currentForm.Invoke(d, new object[] { text, c});
            }
            else
            {                
                c.Text = text;                
            }
        }        
    }









Verificare la correttezza dall'hash per essere sicuri che il file scaricato non sia stato modificato

Per verificare che un file scaricato  sia proprio quello che volevamo downloadare possiamo verificare l'hash del file , questo dato è sp...