找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 10660|回复: 8

[原创]Ring 3文件保护[X64ASM/VB/VC]

 火.. [复制链接]

857

主题

2632

回帖

2

精华

管理员

此生无悔入华夏,  长居日耳曼尼亚。  

积分
36130
发表于 2011-1-5 00:15:01 | 显示全部楼层 |阅读模式

  1. IncludeLib User32.Lib
  2. IncludeLib Kernel32.Lib
  3. EXTERN LoadLibraryA:PROC
  4. EXTERN GetProcAddress:PROC
  5. EXTERN ExitProcess:PROC
  6. EXTERN CreateFileA:PROC
  7. EXTERN OpenProcess:PROC
  8. EXTERN MessageBoxA:PROC
  9. EXTERN GetCurrentProcess:PROC
  10. EXTERN CloseHandle:PROC
  11. EXTERN SetHandleInformation:PROC

  12. .DATA?
  13.         hMod        qword 0
  14.         pApi1        qword 0
  15.         pApi2        qword 0
  16.         bRet        qword 0
  17.         hsFile        qword 0
  18.         htFile        qword 0
  19.         hProc        qword 0
  20.         hMyProc qword 0
  21.         iii                qword 0
  22.        
  23. .DATA
  24.         szTit        BYTE  "c:\TryToDel2.txt",0
  25.         szDll        BYTE  "ntdll.dll",0
  26.         szFun1        BYTE  "RtlAdjustPrivilege",0
  27.         szFun2        BYTE  "NtDuplicateObject",0
  28.         szStr1        BYTE  "Tips",0
  29.         szStr2        BYTE  "OK!!",0
  30.        
  31. .CODE
  32.         Entry PROC
  33.                 ;hMod=LoadLibraryA("ntdll.dll")
  34.                 sub rsp,16
  35.                 lea rcx,szDll
  36.                 call LoadLibraryA
  37.                 mov hMod,rax
  38.                 add rsp,16
  39.                 ;pApi1=GetProcAddress(hMod,"RtlAdjustPrivilege")
  40.                 sub rsp,24
  41.                 mov rcx,hMod
  42.                 lea rdx,szFun1
  43.                 call GetProcAddress
  44.                 mov pApi1,rax
  45.                 add rsp,24
  46.                 ;pApi2=GetProcAddress(hMod,"NtDuplicateObject")
  47.                 sub rsp,24
  48.                 mov rcx,hMod
  49.                 lea rdx,szFun2
  50.                 call GetProcAddress
  51.                 mov pApi2,rax
  52.                 add rsp,24
  53.                 ;RtlAdjustPrivilege(20,1,0,&bRet);
  54.                 sub rsp,40
  55.                 mov rcx,20
  56.                 mov rdx,1
  57.                 mov r8,0
  58.                 lea r9,bRet
  59.                 call pApi1
  60.                 add rsp,40
  61.                 ;hMyProc=GetCurrentProcess()
  62.                 sub rsp,8
  63.                 call GetCurrentProcess
  64.                 mov hMyProc,rax
  65.                 add rsp,8
  66.                 ;hsFile=CreateFileA(pFile, 0x80000000, 0, 0, 3, 0, 0)
  67.                 sub rsp,64
  68.                 xor rax,rax
  69.                 mov [rsp+48],rax                 
  70.                 mov [rsp+40],rax               
  71.                 mov rax,3
  72.                 mov [rsp+32],rax                 
  73.                 xor r9,r9                         
  74.                 xor r8,r8                         
  75.                 mov rdx,80000000h                 
  76.                 lea rcx,szTit                         
  77.                 call CreateFileA
  78.                 mov hsFile,rax
  79.                 add rsp,64
  80.                 ;SetHandleInformation(hsFile,0,2)
  81.                 sub rsp,32
  82.                 mov rcx,hsFile
  83.                 mov rdx,0
  84.                 mov r8,2
  85.                 call SetHandleInformation
  86.                 add rsp,32
  87.                 ;for(i=100;i<19996;i+=4) [copy file handle to other process]
  88.                 mov iii,100 ;start pid
  89.         addto19996:
  90.                         ;OpenProcess(0x1F0FFF, 0, iii);
  91.                         sub rsp,32
  92.                         mov rcx,1F0FFFh
  93.                         mov rdx,0
  94.                         mov r8,iii
  95.                         call OpenProcess
  96.                         mov hProc,rax
  97.                         add rsp,32
  98.                         ;if (rax==0) goto [loopnext]
  99.                         cmp rax,0
  100.                         je loopnext
  101.                         ;NtDuplicateObject((HANDLE)-1, hsFile, hProcess, &htFile, 0, 0, 4);
  102.                         sub rsp,64
  103.                         mov rax,4
  104.                         mov [rsp+48],rax                 
  105.                         xor rax,rax
  106.                         mov [rsp+40],rax               
  107.                         mov [rsp+32],rax                 
  108.                         lea r9,htFile                         
  109.                         mov r8,hProc                         
  110.                         mov rdx,hsFile                         
  111.                         mov rcx,hMyProc                       
  112.                         call pApi2
  113.                         add rsp,64
  114.                         ;CloseHandle(hProc)
  115.                         sub rsp,16
  116.                         mov rcx,hProc
  117.                         call CloseHandle
  118.                         add rsp,16
  119.         loopnext:
  120.                 mov rbx,iii       
  121.                 add rbx,4       
  122.                 mov iii,rbx               
  123.                 ;if (rbx<>19996) goto [addto19996]
  124.                 cmp rbx,19996 ;end pid
  125.                 jb addto19996
  126.                 ;MessageBoxA(0,"OK!!","TIPS",0)
  127.                 sub rsp,40
  128.                 mov rcx,0
  129.                 lea rdx,szStr2
  130.                 lea r8,szStr1
  131.                 mov r9,0
  132.                 call MessageBoxA
  133.                 add rsp,40
  134.                 ;ExitProcess(0)
  135.                 sub rsp,8
  136.                 mov rcx,0
  137.                 call ExitProcess
  138.                 add rsp,8
  139.                 ret
  140.         Entry ENDP
  141. END
复制代码

857

主题

2632

回帖

2

精华

管理员

此生无悔入华夏,  长居日耳曼尼亚。  

积分
36130
 楼主| 发表于 2011-1-5 00:16:13 | 显示全部楼层
VB2010(要编译成64位程序):

  1. Module Module1

  2.     Private Declare Function EnumProcesses Lib "psapi" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
  3.     Private Declare Function RtlAdjustPrivilege Lib "ntdll" (ByVal Privilege As Long, ByVal NewValue As Long, ByVal NewThread As Long, ByRef OldValue As Long) As Long
  4.     Private Declare Function NtClose Lib "ntdll" (ByVal ObjectHandle As Long) As Long
  5.     Private Declare Function NtDuplicateObject Lib "ntdll" (ByVal SourceProcessHandle As Long, ByVal SourceHandle As Long, ByVal TargetProcessHandle As Long, ByRef TargetHandle As Long, ByVal DesiredAccess As Long, ByVal HandleAttributes As Long, ByVal Options As Long) As Long
  6.     Private Declare Function CreateFileA Lib "kernel32" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
  7.     Private Declare Function OpenProcess Lib "kernel32" (ByVal p1 As Long, ByVal p2 As Long, ByVal p3 As Long) As Long
  8.     Private Declare Function SetHandleInformation Lib "kernel32.dll" (ByVal hObject As Long, ByVal dwMask As Long, ByVal dwFlags As Long) As Long
  9.     Private Declare Function CreateHardLinkA Lib "kernel32.dll" (ByVal lpFileName As String, ByVal lpExistingFileName As String, ByVal lpSecurityAttributes As Long) As Long

  10.     Public Function GetProcesses() As Long()
  11.         Dim lngCbNeeded As Long, lngProcArr() As Long
  12.         ReDim lngProcArr(1024)
  13.         EnumProcesses(lngProcArr(0), 4 * 1024, lngCbNeeded)
  14.         ReDim Preserve lngProcArr(lngCbNeeded / 4 - 1)
  15.         GetProcesses = lngProcArr
  16.     End Function

  17.     Sub Main()
  18.         Dim pFile As String, hsFile As Long, hProcess As Long, htFile As Long, lProcess() As Long, fProcess As Long
  19.         RtlAdjustPrivilege(20, 1, 0, 0)
  20.         pFile = InputBox("Input File Name:", "Protect File", "c:\TryToDel3.txt")
  21.         hsFile = CreateFileA(pFile, &H80000000, 0, 0, 3, 0, 0)
  22.         SetHandleInformation(hsFile, 0, 2)
  23.         lProcess = GetProcesses()
  24.         For fProcess = 0 To UBound(lProcess)
  25.             hProcess = OpenProcess(&H1F0FFF, 0, lProcess(fProcess))
  26.             If hProcess <> 0 Then
  27.                 NtDuplicateObject(-1, hsFile, hProcess, htFile, 0, 0, 4)
  28.                 NtClose(hProcess)
  29.             End If
  30.         Next
  31.         MsgBox("OK")
  32.     End Sub

  33. End Module

复制代码

评分

参与人数 1水晶币 +10 +10 收起 理由
yxd199512041 + 10 + 10 好东西啊

查看全部评分

857

主题

2632

回帖

2

精华

管理员

此生无悔入华夏,  长居日耳曼尼亚。  

积分
36130
 楼主| 发表于 2011-1-5 00:16:53 | 显示全部楼层
C++:

  1. #include <stdio.h>
  2. #include <Windows.h>

  3. typedef long (*RTLADJUSTPRIVILEGE)(ULONG,BOOLEAN,BOOLEAN,PBOOLEAN);
  4. typedef long (*NTDUPLICATEOBJECT)(HANDLE,HANDLE,HANDLE,PHANDLE,ACCESS_MASK,BOOLEAN,ULONG);

  5. void GetAllProcessA(int pids[],int *procount)
  6. {
  7.         int i=0,c=0;
  8.         HANDLE hProcess=0;
  9.         for(i=8;i<19996;i+=4)
  10.         {
  11.                 hProcess=OpenProcess(0x10,0,i);
  12.                 if (hProcess!=0)
  13.                 {
  14.                         pids[c]=i;
  15.                         CloseHandle(hProcess);
  16.                         c++;
  17.                 }
  18.         }
  19.         *procount=c;
  20. }

  21. int main()
  22. {
  23.         BOOLEAN bRet;
  24.         int pids[260];
  25.         int procsnum=0;
  26.         char pFile[260];
  27.         RTLADJUSTPRIVILEGE getdbg=(RTLADJUSTPRIVILEGE)GetProcAddress(GetModuleHandleW(L"ntdll.dll"),"RtlAdjustPrivilege");
  28.         NTDUPLICATEOBJECT NtDuplicateObject=(NTDUPLICATEOBJECT)GetProcAddress(GetModuleHandleW(L"ntdll.dll"),"NtDuplicateObject");
  29.         getdbg(20,1,0,&bRet);
  30.         memset(pids,0,4*260);
  31.         memset(pFile,0,260);
  32.         printf("Input the file name you want to protect: ");
  33.         scanf("%s",pFile);
  34.         HANDLE hsFile = CreateFileA(pFile, 0x80000000, 0, 0, 3, 0, 0);
  35.         SetHandleInformation(hsFile,0,2);
  36.         GetAllProcessA(pids,&procsnum);
  37.         for(int i=0;i<procsnum;i++)
  38.         {
  39.                 HANDLE htFile=0;
  40.                 HANDLE hProcess = OpenProcess(0x1F0FFF, 0, pids[i]);
  41.                 if (hProcess!=0)
  42.                 {
  43.                         NtDuplicateObject((HANDLE)-1, hsFile, hProcess, &htFile, 0, 0, 4);
  44.                         CloseHandle(hProcess);
  45.                 }
  46.         }
  47.         getchar();
  48.         printf("OK!\n");
  49.         getchar();
  50.         return 0;
  51. }
复制代码

5

主题

39

回帖

1

精华

铂金会员

积分
1567
发表于 2011-1-11 16:14:39 | 显示全部楼层
支持一个,,,能用x64写。的却很牛叉!

857

主题

2632

回帖

2

精华

管理员

此生无悔入华夏,  长居日耳曼尼亚。  

积分
36130
 楼主| 发表于 2011-2-26 23:19:10 | 显示全部楼层
回复 nbboy 的帖子

小伎俩而已。

12

主题

144

回帖

0

精华

铜牌会员

积分
281
发表于 2011-10-18 01:54:02 | 显示全部楼层
顶一下

28

主题

116

回帖

0

精华

铜牌会员

积分
273
发表于 2012-6-13 01:07:18 | 显示全部楼层
好吧支持个

7

主题

414

回帖

1

精华

铂金会员

积分
2173
发表于 2013-4-11 09:51:48 | 显示全部楼层
Tesla.Angela 发表于 2011-1-5 00:16
C++:

支持一下

30

主题

693

回帖

0

精华

钻石会员

积分
2815
发表于 2015-4-3 09:13:07 | 显示全部楼层
不愧坛主 功底深厚
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

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