基于C#实现文件伪装技术

 更新时间:2024年02月03日 11:01:19   作者:lingxiao16888  
这篇文章主要为大家详细介绍了如何基于C#实现文件伪装功能,将一般文件夹伪装成计算机,控制面板,打印机等,感兴趣的小伙伴可以跟随小编一起学习一下

1.目的

将一般文件夹伪装成计算机,控制面板,打印机等,当双击该文件夹时打开相应的伪装组件(计算机,控制面板,打印机等)而不是文件夹从而达到隐藏的目的。

2.原理

在需要进行伪装的文件夹下,建立desktop.ini 文本,并在其中写入需要伪装的系统标识符。如下:

系统标识符格式:

[.ShellClassInfo]
CLSID={21EC2020-3AEA-1069-A2DD-08002B30309D}     

注解:

.ShellClassInfo:根节点 ;

CLSID:类标识符;

{21EC2020-3AEA-1069-A2DD-08002B30309D}:系统标识符

常用系统标识符目录:

我的电脑  :{20D04FE0-3AEA-1069-A2D8-08002B30309D}

我的文档  :{450D8FBA-AD25-11D0-98A8-0800361B1103}

拨号网络  :{992CFFA0-F557-101A-88EC-00DD010CCC48}

控制面板  :{21EC2020-3AEA-1069-A2DD-08002B30309D}

计划任务  :{D6277990-4C6A-11CF-8D87-00AA0060F5BF}

打 印 机  :{2227A280-3AEA-1069-A2DE-08002B30309D}

网上邻居  :{208D2C60-3AEA-1069-A2D7-08002B30309D}

回 收 站  :{645FF040-5081-101B-9F08-00AA002F954E}

公 文 包  :{85BBD920-42A0-1069-A2E4-08002B30309D}

字    体  :{D20EA4E1-3957-11D2-A40B-0C5020524152}

Web文件夹 :{BDEADF00-C265-11D0-BCED-00A0C90AB50F}

写字板文档:{73FDDC80-AEA9-101A-98A7-00AA00374959}

3.效果

1>软件效果

2>伪装操作文件夹后效果:

双击"伪装测试2"文件夹打开呈现效果:

3>还原之后的效果(还原后等候30秒后效果方能呈现)

4.实现代码

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        List<Clsid> cLSID;
        private void btnDirc_Click(object sender, EventArgs e)
        {
            using(FolderBrowserDialog fbd=new FolderBrowserDialog())
            {
                if (fbd.ShowDialog() == DialogResult.OK)
                {
                    label3.Text = fbd.SelectedPath;
                }
                
            }
        }
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //保存Json数据
            using (TextWriter writer=new StreamWriter("../../Data.json"))
            {
                string jsonStr = JsonConvert.SerializeObject(cLSID, Formatting.Indented);
                writer.Write(jsonStr);
            }
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            if (File.Exists("../../Data.json"))
            {
                string objstr = File.ReadAllText("../../Data.json", Encoding.UTF8);
                cLSID = JsonConvert.DeserializeObject<List<Clsid>>(objstr);
            }
            else
            {
                cLSID = new List<Clsid>();
            }
            if (cLSID.Count > 0)
            {
                cLSID.ForEach(a =>
                {
                    comboBox1.Items.Add(a);
                });
                comboBox1.DisplayMember = "Key";
                comboBox1.ValueMember = "Value";
                comboBox1.SelectedIndex = 0;
            }
           
            
        }
 
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                Clsid csid = comboBox1.SelectedItem as Clsid;
                textBox1.Text = csid.Value;
            }
        }
 
        private void btnLogin_Click(object sender, EventArgs e)
        {
            string txtContent = textBox1.Text.Trim().ToUpper();
            if (System.Text.RegularExpressions.Regex.IsMatch(txtContent, @"^\{\w{8}-\w{4}-\w{4}-\w{4}-\w{12}\}$"))
            {
                //判断Clsid是否已经存在
                bool hascLsid = cLSID.Any(a => a.Value == txtContent);
                if (hascLsid)
                {
                    MessageBox.Show("该CLSID已存在请勿重复登录!","提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    LoginFrm win = new LoginFrm(txtContent);
                    win.StartPosition = FormStartPosition.CenterParent;
                    win.LoginCompleted += Win_LoginCompleted;
                  if(win.ShowDialog() == DialogResult.OK)
                    {
                       
                        MessageBox.Show("添加成功!","提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
 
            }
            else
            {
                MessageBox.Show("格式错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
 
        private void Win_LoginCompleted(string obj)
        {
            Clsid cs = new Clsid
            {
                Key = obj,
                Value = textBox1.Text.Trim().ToUpper()
            };
            cLSID.Add(cs);
            comboBox1.Items.Add(cs);
        }
 
        private void btnDisguise_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(label3.Text))
            {
                //用于保存windows 文件标识符的文本
                string desktopFile = Path.Combine(label3.Text, "desktop.ini");
                if (!File.Exists(desktopFile))
                {                  
                  FileStream fs=   File.Create(desktopFile);
                    fs.Dispose();
                }
                Clsid csid = comboBox1.SelectedItem as Clsid;
                string cmdStr = csid.Value;
                INIHelper.WriteToINI(desktopFile, ".ShellClassInfo", "CLSID", cmdStr);
                //给ini文件设置特性
                File.SetAttributes(desktopFile, FileAttributes.Hidden);
                File.SetAttributes(label3.Text, FileAttributes.System);
                MessageBox.Show("伪装成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("该文件夹不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
 
        private void btnRestore_Click(object sender, EventArgs e)
        {
            //所谓还原就是删除ini文件
            string desktopFile = Path.Combine(label3.Text, "desktop.ini");
            if (File.Exists(desktopFile))
            {
                try
                {
                    File.Delete(desktopFile);
                    MessageBox.Show("还原成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
 
                    MessageBox.Show("还原失败,原因:"+ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
               
            }
            else
            {
                MessageBox.Show("该文件不需要还原", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
    class Clsid
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }
/// <summary>
    /// 对INI文件进行读写
    /// </summary>
    class INIHelper
    {
        /// <summary>
        /// 从INI文件中读取数据
        /// </summary>
        /// <param name="filePath">INI文件的全路径</param>
        /// <param name="rootValue">根节点值,例如根节点[ConnectString]的值为:ConnectString</param>
        /// <param name="key">根节点下的键</param>
        /// <param name="defValue">当标记值未设定或不存在时的默认值</param>
        /// <returns></returns>
        public static string ReadFromINI(string filePath, string rootValue, string key, string defValue = "")
        {
            StringBuilder sb = new StringBuilder(1024);
            GetPrivateProfileString(rootValue, key, defValue, sb, 1024, filePath);
            return sb.ToString();
        }
        public static void WriteToINI(string filePath, string rootValue, string key, string newVal)
        {
 
            WritePrivateProfileString(rootValue, key, newVal, filePath);
 
        }
 
        /// <summary>
        /// 对INI文件进行读取操作
        /// </summary>
        /// <param name="IpAppName">表示INI文件内部根节点的值</param>
        /// <param name="IpKeyName">表示根节点下子标记的值</param>
        /// <param name="IpDefault">表示当标记值未设定或不存在时的默认值</param>
        /// <param name="IpReturnString">返回读取节点的值</param>
        /// <param name="nSize">读取的节点内容的最大容量</param>
        /// <param name="IpFileName">文件的全路径</param>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("kernel32")]
        static extern int GetPrivateProfileString(string IpAppName, string IpKeyName, string IpDefault, StringBuilder IpReturnString, int nSize, string IpFileName);
        /// <summary>
        /// 对INI文件进行写入操作
        /// </summary>
        /// <param name="mpAppName">INI文件内部根节点的值</param>
        /// <param name="mpKeyName">将要修改的标记名称</param>
        /// <param name="mpDefault">想要修改的内容</param>
        /// <param name="mpFileName">INI文件的全路径</param>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("kernel32")]
        static extern long WritePrivateProfileString(string mpAppName, string mpKeyName, string mpDefault, string mpFileName);
    }
 

对INI文件的详细操作指南请参考:C#对INI文件的读写操作

到此这篇关于基于C#实现文件伪装技术的文章就介绍到这了,更多相关C#文件伪装内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:

相关文章

  • c#定时器使用示例详解

    c#定时器使用示例详解

    这篇文章主要介绍了c#定时器的使用示例,大家参考使用吧
    2014-01-01
  • 详解C#中String.ToCharArray方法的使用

    详解C#中String.ToCharArray方法的使用

    这篇文章主要为大家详细介绍了C#中String.ToCharArray方法的使用的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-01-01
  • 利用C#操作WMI指南

    利用C#操作WMI指南

    WMI提供了一套内置在Microsoft Windows操作系统中的丰富的系统管理服务,可以在有大量的应用程序、服务和设备的系统中提供全方位的管理功能。它允许应用程序的开发者,使用简单的、一致的机制,去查询企业中的任一台计算机上的信息,或是进行系统配置
    2016-11-11
  • C#实现文件Move和Copy操作

    C#实现文件Move和Copy操作

    这篇文章介绍了C#实现文件Move和Copy操作的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • Unity3D在Preview中打印日志的方法

    Unity3D在Preview中打印日志的方法

    这篇文章主要为大家详细介绍了Unity3D在Preview中打印日志的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-09-09
  • C#控制台带参数程序源码编写实例讲解

    C#控制台带参数程序源码编写实例讲解

    像ipconfig /all 这样的CMD命令想必大家都知道,但是很多童鞋可能不知道怎么写这样的控制台带参数的程序,需要的朋友可以了解下
    2012-12-12
  • 浅谈C#泛型的用处与特点

    浅谈C#泛型的用处与特点

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。泛型将类型参数的概念引入 .NET Framework,类型参数使得设计如下类和方法成为可能:这些类和方法将一个或多个类型的指定推迟到客户端代码声明并实例化该类或方法的时候
    2013-09-09
  • C#中常量和只读变量的区别小结

    C#中常量和只读变量的区别小结

    本篇文章主要是对C#中常量和只读变量的区别进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助
    2014-01-01
  • C#中Winform 实现Ajax效果自定义按钮

    C#中Winform 实现Ajax效果自定义按钮

    这篇文章主要介绍了C#中Winform 实现Ajax效果自定义按钮的相关资料,需要的朋友可以参考下
    2017-12-12
  • 快速学习C# 设计模式之职责链模式

    快速学习C# 设计模式之职责链模式

    这篇文章主要介绍了C# 设计模式之职责链模式的的相关资料,文中代码非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-06-06

最新评论