首页
社区
课程
招聘
[原创]驱动加载器(增加PE文件有效性检查、保存路径功能)
发表于: 2013-10-1 12:38 4290

[原创]驱动加载器(增加PE文件有效性检查、保存路径功能)

2013-10-1 12:38
4290
作者:绿林科技
QQ:1473656864
文件名:NTDriverLoader.exe
OS:WinXP,Vista,Win7
功能:加载驱动
附加功能:对PE文件的合法性的检查、支持拖拽、保存路径

拖拽部分源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
void CDialog1::OnDropFiles(HDROP hDropInfo)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    wchar_t FileName[MAX_PATH + 1] = {0};//被拖拽的文件的绝对路径
    ::DragQueryFileW(hDropInfo, 0, FileName, MAX_PATH);//拖拽的文件名
    CString str1,str2,str3;
  
    HANDLE hFile;
    IMAGE_DOS_HEADER dosHeader;
    IMAGE_NT_HEADERS ntHeaders;
    BOOL bValid = FALSE;
    DWORD dwRead;
    //第一步,打开要检测的文件
    hFile = CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ, NULL,
        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    //第二步,检测DOS头部的有效性
    ReadFile(hFile, &dosHeader, sizeof(dosHeader), &dwRead, NULL);
    if(dwRead == sizeof(dosHeader))
    {
        if(IMAGE_DOS_SIGNATURE == dosHeader.e_magic)
        {
            //第三步,定位image_nt_headers的位置
            if(SetFilePointer(hFile, dosHeader.e_lfanew, NULL, FILE_BEGIN))
            {
                ReadFile(hFile, &ntHeaders, sizeof(ntHeaders), &dwRead, NULL);
                if(dwRead == sizeof(ntHeaders))
                {
                    //最后一步,检测PE头部的有效性
                    if(IMAGE_NT_SIGNATURE == ntHeaders.Signature)
                    {
                        bValid = TRUE;
                          
                    }
                }
            }
        }
    }
    if(bValid)
    {
        //AfxMessageBox(_T("该程序是有效PE文件"));
        str1=PathFindExtension(FileName);
        str2=_T(".sys");
        str3=_T(".SYS");
        if (str1==str2 || str1==str3)
        {
            m_EDIT1.SetWindowTextW(FileName);
        }
        else
            AfxMessageBox(_T("文件后缀名不符合要求!"));
    }
    else
    {
        AfxMessageBox(_T("该程序不是有效的PE文件"));
    }
    CloseHandle(hFile);
    // 结束此次拖拽操作,并释放分配的资源
    CDialogEx::OnDropFiles(hDropInfo);
}


安装的部分源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
void CDialog1::OnBnClickedButton2()
{
    // TODO: 在此添加控件通知处理程序代码
    CString DriverFilePath;
    CString DriverFileName;
    m_EDIT1.GetWindowTextW(DriverFilePath);
    if (!PathFileExists(DriverFilePath))
    {
        AfxMessageBox(_T("该文件不存在!"));
        return;
    }
    else
    {
        DriverFileName=PathFindFileName(DriverFilePath);
        DriverFileName = DriverFileName.Left(DriverFileName.GetLength()-4);
        //AfxMessageBox(DriverFileName);
    }
    if (InstallDriverCWinThread != NULL)
    {
        delete InstallDriverCWinThread;
        InstallDriverCWinThread = NULL;
    }
    InstallDriverCWinThread = AfxBeginThread(InstallDriverThread,this);
    InstallDriverCWinThread->m_bAutoDelete=FALSE;
}
  
UINT CDialog1::InstallDriverThread(LPVOID pParam)
{
    CString strPath;
    CString DriverFilePath;
    CString DriverFileName;
    CDialog1* pDlg = NULL;
    pDlg = (CDialog1*)pParam;
    pDlg->m_EDIT1.GetWindowTextW(strPath);
    pDlg->m_EDIT1.GetWindowTextW(DriverFilePath);
    if (!PathFileExists(DriverFilePath))
    {
        AfxMessageBox(_T("该文件不存在!"));
        return 0;
    }
    else
    {
        DriverFileName=PathFindFileName(DriverFilePath);
        DriverFileName = DriverFileName.Left(DriverFileName.GetLength()-4);
        //AfxMessageBox(DriverFileName);
    }
    wchar_t szDriverImagePath[MAX_PATH];
    //得到完整的驱动路径
    ::GetFullPathNameW(strPath, MAX_PATH, szDriverImagePath, NULL);
  
    BOOL bRet = FALSE;
  
    SC_HANDLE hServiceMgr=NULL;//SCM管理器的句柄
    SC_HANDLE hServiceDDK=NULL;//NT驱动程序的服务句柄
  
    //打开服务控制管理器
    hServiceMgr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
  
    if( hServiceMgr == NULL ) 
    {
        //OpenSCManager失败
        pDlg->m_STATIC1.SetWindowTextW(_T("OpenSCManager失败"));
        bRet = FALSE;
        goto BeforeLeave;
    }
    else
    {
        ////OpenSCManager成功
        pDlg->m_STATIC1.SetWindowTextW(_T("OpenSCManager成功"));
    }
    //创建驱动所对应的服务
    hServiceDDK = ::CreateServiceW( hServiceMgr,
        DriverFileName, //驱动程序的在注册表中的名字 
        DriverFileName, // 注册表驱动程序的 DisplayName 值 
        SERVICE_ALL_ACCESS, // 加载驱动程序的访问权限 
        SERVICE_KERNEL_DRIVER,// 表示加载的服务是驱动程序 
        SERVICE_DEMAND_START, // 注册表驱动程序的 Start 值 
        SERVICE_ERROR_IGNORE, // 注册表驱动程序的 ErrorControl 值 
        szDriverImagePath, // 注册表驱动程序的 ImagePath 值 
        NULL, 
        NULL, 
        NULL, 
        NULL, 
        NULL); 
    DWORD dwRtn;
    //判断服务是否失败
    if( hServiceDDK == NULL )
    {
        dwRtn = GetLastError();
        if( dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_EXISTS ) 
        
            //由于其他原因创建服务失败
            pDlg->m_STATIC1.SetWindowTextW(_T("由于其他原因创建服务失败"));
            bRet = FALSE;
            goto BeforeLeave;
        }
        else
        {
            //服务创建失败,是由于服务已经创立过
            pDlg->m_STATIC1.SetWindowTextW(_T("服务创建失败,是由于服务已经创立过"));
        }
        // 驱动程序已经加载,只需要打开 
        hServiceDDK = ::OpenServiceW( hServiceMgr, DriverFileName, SERVICE_ALL_ACCESS ); 
        if( hServiceDDK == NULL ) 
        {
            //如果打开服务也失败,则意味错误
            dwRtn = GetLastError(); 
            pDlg->m_STATIC1.SetWindowTextW(_T("如果打开服务也失败,则意味错误"));
            bRet = FALSE;
            goto BeforeLeave;
        }
        else
        {
            pDlg->m_STATIC1.SetWindowTextW(_T("OpenService() ok !"));
        }
    }
    else
    {
        pDlg->m_STATIC1.SetWindowTextW(_T("CrateService() ok ! "));
    }
    //开启此项服务
    bRet= ::StartServiceW( hServiceDDK, NULL, NULL );
    if( !bRet ) 
    {
        DWORD dwRtn = GetLastError(); 
        if( dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_ALREADY_RUNNING ) 
        
            pDlg->m_STATIC1.SetWindowTextW(_T("StartService() Faild!"));
            bRet = FALSE;
            goto BeforeLeave;
        }
        else
        {
            if( dwRtn == ERROR_IO_PENDING ) 
            
                //设备被挂住
                pDlg->m_STATIC1.SetWindowTextW(_T("设备被挂住!"));
                bRet = FALSE;
                goto BeforeLeave;
            }
            else
            
                //服务已经开启
                pDlg->m_STATIC1.SetWindowTextW(_T("服务已经开启!"));
                bRet = TRUE;
                goto BeforeLeave;
            }
        }
    }
    bRet = TRUE;
    pDlg->m_STATIC1.SetWindowTextW(_T("安装并启动服务成功!"));
    //离开前关闭句柄
BeforeLeave:
    if(hServiceDDK)
    {
        CloseServiceHandle(hServiceDDK);
    }
    if(hServiceMgr)
    {
        CloseServiceHandle(hServiceMgr);
    }
    return bRet;
}

源程序和源码:
NTDriverLoader(Src & Bin).rar
Visual C/C++,汇编语言,驱动交流群:177822398、 177822108

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

上传的附件:
收藏
免费
支持
分享
最新回复 (6)
雪    币: 257
活跃值: (67)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
2
占个沙发来看看
2013-10-1 14:09
0
雪    币: 478
活跃值: (50)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
我想多了      还以为是能绕过驱动防火墙的方法
2013-10-1 19:56
0
雪    币: 435
活跃值: (767)
能力值: ( LV9,RANK:170 )
在线值:
发帖
回帖
粉丝
4
代码风格不错!
2013-10-1 21:42
0
雪    币: 9961
活跃值: (2816)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
没怎么细看,好像逻辑性还不错,MARK
2013-10-1 22:33
0
雪    币: 12
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
学习了,感谢分享。
2013-10-15 07:44
0
雪    币: 9
活跃值: (230)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
7
谢谢分享!
2017-8-21 10:11
0
游客
登录 | 注册 方可回帖
返回

账号登录
验证码登录

忘记密码?
没有账号?立即免费注册