首页 > 解决方案 > 如何将 lambda 坐标输出插入 KeyValuePair?

问题描述

我正在尝试使用 C# 创建一个 MD5 恶意软件扫描程序。使用普通的字典比较有一个致命的缺陷,在目录中存在具有相同哈希值的重复文件,因此,相同的键(md5)将代表很多要关联的文件目录。我尝试切换到 KeyValuePair<> 但由于我的经验不足,我仍然无法弄清楚如何将 lambda 坐标输出插入 KeyValuePair<> (在下面的代码中由Idon'tknowwhatshouldbehere表示)。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Diagnostics;
using System.Text;
using System.Web;
using static System.Net.WebRequestMethods;

namespace RiskRemover
{
    class Program
    {
        private static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            var currDir = Directory.GetCurrentDirectory();
            Console.WriteLine("Stage 1: Update");
            HttpWebRequest updRq = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/drive/v3/files/15WR2yTVJzgwg2pn64IhxFUbfy2BmmsdL?alt=media&key=APIKey");
            updRq.Referer = "referrer";
            HttpWebResponse updRqF = (HttpWebResponse)updRq.GetResponse();
            using (Stream output = System.IO.File.OpenWrite("virushashesL.txt"))
            using (Stream input = updRqF.GetResponseStream())
            {
                input.CopyTo(output);
            }
            bool dbExist = System.IO.File.Exists($"{currDir}\\virushashesL.txt");
            if (!dbExist)
            {
                Console.WriteLine("Database Doesn't exist, Terminating...");
                return;
            }
            var lineCount = System.IO.File.ReadLines($"{currDir}\\virushashesL.txt").Count();
            Console.WriteLine(" ");
            Console.WriteLine($"Database Hash Count: {lineCount}");
            Console.WriteLine(" ");
            Console.Write("Press any key to continue...");
            Console.Clear();
            Console.Write("Scan Path:");
            string pathScan = @Console.ReadLine();
            Console.Clear();
            Console.WriteLine("Stage 2: MD5 Hashing");
            var data = GetHasList(@pathScan, false).Select(x => $"\"{x.fileName}\" {x.hash}");
            System.IO.File.WriteAllLines("output.txt", data);
            Console.Clear();
            Console.WriteLine("Stage 3: Comparing MD5 hashes to DB");
            KeyValuePair<string, string> dic = new KeyValuePair<string, string>();
             dic = System.IO.File.ReadAllLines("output.txt")
              .Select(l => l.Split(new[] { '<' }))
              .Idon'tknowwhatshouldbehere(s => s[1].Trim().Substring(0, 10), s => s[0].Trim());
            List<string> lines = System.IO.File.ReadAllLines("virushashesL.txt").ToList();
            foreach (var line in lines)
            {
                bool malicious = dic.ContainsKey(line);
                if (malicious)
                {
                    string malPath = dic[line];
                    System.IO.File.Delete(malPath);
                }
            }
            Console.Clear();
            sw.Stop();
            Console.Write($"Done in {sw.Elapsed}...");
            Console.ReadKey();
            return;
        }
        public static IEnumerable<(string fileName, string hash)> GetHasList(string path, bool isRelative)
        {
            foreach (var file in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories))
            {
                string hash;
                using (var md5 = MD5.Create())
                using (var stream = System.IO.File.OpenRead(file))
                    hash = BitConverter.ToString(md5.ComputeHash(stream)).ToLower();
                    hash = hash.Replace("-", "");
                if (isRelative)
                    yield return (file.Remove(0, path.TrimEnd('/').Length + 1), hash);
                else
                    yield return ($"{file}<", hash);
            }
        }
    }
}

示例 output.txt

"D:\EvaxHybrid\Downloads\CS8\insdir\CSMediaLibParser.dll<" a384ff0a72a89028fc5edc894309ce81
"D:\EvaxHybrid\Downloads\CS8\insdir\CSMediaLibTools.dll<" 62cd2374d3a2bbeb888a078dc20e6b18
...

示例 virushashesL.txt

2d3f18345c
2d427ec2c7
...

标签: c#.net-core

解决方案


D://output.txt


D:\EvaxHybrid\Downloads\CS8\insdir\CSMediaLibParser.dll< a384ff0a72a89028fc5edc894309ce81
D:\EvaxHybrid\Downloads\CS8\insdir\CSMediaLibTools.dll< 62cd2374d3a2bbeb888a078dc20e6b18 



D://virushashesL.txt
a384ff0a72a89028fc5edc894309ce81
62cd2374d3a2bbeb888a078dc20e6b18



private void fileintodis()
        {
             List<KeyValuePair<string, string>> dic = new List<KeyValuePair<string, string>>();
            dic = System.IO.File.ReadAllLines("D://output.txt").ToList()
                .Select(l => new KeyValuePair<string, string>(l.Split('<')[1].Trim(), l.Split('<')[0].Trim())).ToList();
            
           
            List<string> lines = System.IO.File.ReadAllLines("D://virushashesL.txt").ToList();
            foreach (var line in lines)
            {
               
                bool malicious = dic.Where(s => s.Key.Trim() == line).Count() > 0 ? true : false;
                if (malicious)
                {
                    string malPath = dic.Where(s => s.Key == line).Select(e => e.Value).FirstOrDefault().ToString();
                    System.IO.File.Delete(malPath);
                }
            }
        }


推荐阅读