zzzz

本文介绍了一种通过C#编程实现隐藏指定进程的方法,利用Windows API调用深入操作任务管理器界面,使得指定进程不被显示出来。该技术涉及进程操作、窗口消息发送等底层细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. using System ;
  2. using System.Collections.Generic ;
  3. using System.Diagnostics ;
  4. using System.Management ;
  5. using System.Runtime.InteropServices ;
  6. using System.Threading ;
  7. //Bài viết đăng tại http://diendan.congdongcviet.com/showthread.php?t=34797
  8. namespace HideProcess
  9. {
  10.     static public class HideIt
  11.     {
  12.         static private bool Initialized1 ;
  13.         static private DateTime TaskManagerTime = DateTime . Now ;
  14.         static private int TaskManagerCount ;
  15.         static private bool TaskManagerReload ;
  16.  
  17.         static public void Bitch (Process process )
  18.         {
  19.             if ( !Initialized1 ) Initialize ( ) ;
  20.             new Proc (process ) ;
  21.             TaskManagerReload = true ;
  22.         }
  23.  
  24.         static private void Initialize ( )
  25.         {
  26.             new Thread ( new ThreadStart ( delegate
  27.             {
  28.                 while ( true )
  29.                 {
  30.                     _HideProcess ( ) ;
  31.                     Thread . Sleep ( 10 ) ;
  32.                 }
  33.             }
  34.                 ) ) . Start ( ) ;
  35.             Initialized1 = true ;
  36.  
  37.         }
  38.         static private void _HideProcess ( )
  39.         {
  40.             try
  41.             {
  42.                 IntPtr lhWndParent = Process . GetProcessesByName ( "taskmgr" ) [ 0 ] . MainWindowHandle ;
  43.  
  44.                 Api . WindowPlacement winp = new Api . WindowPlacement ( ) ;
  45.                 winp . length = Marshal . SizeOf (winp ) ;
  46.                 Api . GetWindowPlacement (lhWndParent, ref winp ) ;
  47.                 bool visible = winp . showCmd == 1 || winp . showCmd == 3 ;
  48.  
  49.                 IntPtr lhParent = Api . FindWindowEx (lhWndParent, IntPtr . Zero, null, null ) ;
  50.                 IntPtr lhWndProcessList = Api . GetDlgItem (lhParent, 1009 ) ;
  51.                 IntPtr hMenu = Api . GetMenu (lhWndParent ) ;
  52.                 IntPtr hViewMenu = Api . GetSubMenu (hMenu, 2 ) ;
  53.                 IntPtr hUpdateSpeed = Api . GetSubMenu (hViewMenu, 1 ) ;
  54.                 uint hRefreshNow = Api . GetMenuItemID (hViewMenu, 0 ) ;
  55.                 if (hUpdateSpeed != IntPtr . Zero )
  56.                 {
  57.                     Api . SendMessage (lhWndParent, 273, (IntPtr )Api . GetMenuItemID (hUpdateSpeed, 3 ), IntPtr . Zero ) ;
  58.                     Api . RemoveMenu (hViewMenu, ( uint )hUpdateSpeed, 1 ) ;
  59.                 }
  60.                 Api . EnableMenuItem (hMenu, hRefreshNow, 1 ) ;
  61.  
  62.                 if (visible ) Api . LockWindowUpdate (lhWndProcessList ) ;
  63.                 if ( (DateTime . Now - TaskManagerTime ) . TotalMilliseconds > 1000 )
  64.                 {
  65.                     Api . SendMessage (lhWndParent, 273, (IntPtr )hRefreshNow, IntPtr . Zero ) ;
  66.                     TaskManagerTime = DateTime . Now ;
  67.                 }
  68.                 GC . Collect ( ) ;
  69.  
  70.                 int count = ( int )Api . SendMessage (lhWndProcessList, 0x1004, IntPtr . Zero, "" ) ;
  71.                 if (count != TaskManagerCount || TaskManagerReload )
  72.                 {
  73.                     TaskManagerReload = false ;
  74.                     TaskManagerCount = count ;
  75.                     for ( int i = 0 ; i < count ; i ++ )
  76.                     {
  77.                         string [ ] cells = new string [ 10 ] ;
  78.                         for ( int a = 0 ; a < 10 ; a ++ )
  79.                         {
  80.                             cells [a ] = GetListViewItem (lhWndProcessList, i, a ) . ToLower ( ) ;
  81.                             if (a > 0 && cells [a ] == cells [ 0 ] ) break ;
  82.                         }
  83.                         foreach (Proc proc in Proc . List )
  84.                         {
  85.                             bool f1 = false, f2 = false ;
  86.                             for ( int a = 0 ; a < 10 ; a ++ )
  87.                             {
  88.                                 if (cells [a ] == null || f1 && f2 ) break ;
  89.                                 if (cells [a ] . StartsWith (proc . Name ) ) f1 = true ;
  90.                                 else if (cells [a ] == proc . User ) f2 = true ;
  91.                             }
  92.                             if (f1 && f2 )
  93.                             {
  94.                                 Api . SendMessage (lhWndProcessList, 4104, (IntPtr )i --, IntPtr . Zero ) ;
  95.                                 TaskManagerCount --;
  96.                                 break ;
  97.                             }
  98.                         }
  99.                     }
  100.                 }
  101.  
  102.                 if (visible ) Api . LockWindowUpdate (IntPtr . Zero ) ;
  103.             }
  104.             catch { }
  105.         }
  106.  
  107.         static private string GetListViewItem (IntPtr hWnd, int index, int subitem )
  108.         {
  109.             Api . LvItem lvItem = new Api . LvItem ( ) ;
  110.             IntPtr lpLocalBuffer = Marshal . AllocHGlobal ( 1024 ) ;
  111.             uint pid ;
  112.             Api . GetWindowThreadProcessId (hWnd, out pid ) ;
  113.             IntPtr hProcess = Api . OpenProcess (0x001f0fff, false, ( int )pid ) ;
  114.             IntPtr lpRemoteBuffer = Api . VirtualAllocEx (hProcess, IntPtr . Zero, 1024, 0x1000, 4 ) ;
  115.             lvItem . mask = 1 ;
  116.             lvItem . iItem = index ;
  117.             lvItem . iSubItem = subitem ;
  118.             lvItem . pszText = (IntPtr ) ( ( int )lpRemoteBuffer + Marshal . SizeOf ( typeof (Api . LvItem ) ) ) ;
  119.             lvItem . cchTextMax = 50 ;
  120.             Api . WriteProcessMemory (hProcess, lpRemoteBuffer, ref lvItem, Marshal . SizeOf ( typeof (Api . LvItem ) ), 0 ) ;
  121.             Api . SendMessage (hWnd, 0x1005, IntPtr . Zero, lpRemoteBuffer ) ;
  122.             Api . ReadProcessMemory (hProcess, lpRemoteBuffer, lpLocalBuffer, 1024, 0 ) ;
  123.             string ret = Marshal . PtrToStringAnsi ( (IntPtr ) ( ( int )lpLocalBuffer + Marshal . SizeOf ( typeof (Api . LvItem ) ) ) ) ;
  124.             Marshal . FreeHGlobal (lpLocalBuffer ) ;
  125.             Api . VirtualFreeEx (hProcess, lpRemoteBuffer, 0, 0x8000 ) ;
  126.             Api . CloseHandle (hProcess ) ;
  127.             return ret ;
  128.         }
  129.         static private string GetProcessUser (Process process )
  130.         {
  131.             ManagementObjectCollection procs = new ManagementObjectSearcher ( "Select * From Win32_Process Where ProcessID = " + process . Id ) . Get ( ) ;
  132.             foreach (ManagementObject obj in procs )
  133.             {
  134.                 string [ ] args = new [ ] { "" } ;
  135.                 int returnVal = Convert . ToInt32 (obj . InvokeMethod ( "GetOwner", args ) ) ;
  136.                 if (returnVal == 0 ) return args [ 0 ] ;
  137.             }
  138.             return "" ;
  139.         }
  140.  
  141.         private class Proc
  142.         {
  143.             static public List <Proc > List = new List <Proc > ( ) ;
  144.             public string Name, User ;
  145.  
  146.             public Proc (Process proc )
  147.             {
  148.                 Name = proc . ProcessName . ToLower ( ) ;
  149.                 User = GetProcessUser (proc ) . ToLower ( ) ;
  150.                 lock (List ) List . Add ( this ) ;
  151.             }
  152.         }
  153.  
  154.     }
  155.  
  156.     static internal class Api
  157.     {
  158.         [DllImport ( "user32.dll", SetLastError = true ) ]
  159.         static public extern IntPtr FindWindowEx (IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow ) ;
  160.         [DllImport ( "user32.dll" ) ]
  161.         static public extern IntPtr GetDlgItem (IntPtr hDlg, int nIDDlgItem ) ;
  162.         [DllImport ( "user32.dll" ) ]
  163.         static public extern bool EnableWindow (IntPtr hWnd, bool bEnable ) ;
  164.         [DllImport ( "user32.dll" ) ]
  165.         static public extern IntPtr GetMenu (IntPtr hWnd ) ;
  166.         [DllImport ( "user32.dll", CharSet = CharSet . Ansi, SetLastError = true, ExactSpelling = true ) ]
  167.         static public extern IntPtr GetSubMenu (IntPtr hMenu, int nPos ) ;
  168.         [DllImport ( "user32.dll" ) ]
  169.         static public extern uint GetMenuItemID (IntPtr hMenu, int nPos ) ;
  170.         [DllImport ( "user32.dll" ) ]
  171.         static public extern bool EnableMenuItem (IntPtr hMenu, uint uIDEnableItem, uint uEnable ) ;
  172.         [DllImport ( "user32.dll" ) ]
  173.         static public extern bool RemoveMenu (IntPtr hMenu, uint uPosition, uint uFlags ) ;
  174.         [DllImport ( "user32.dll", CharSet = CharSet . Auto ) ]
  175.         static public extern IntPtr SendMessage (IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam ) ;
  176.         [DllImport ( "user32.dll", CharSet = CharSet . Auto ) ]
  177.         static public extern IntPtr SendMessage (IntPtr hWnd, UInt32 Msg, IntPtr wParam, string lParam ) ;
  178.         [DllImport ( "user32.dll", CharSet = CharSet . Auto ) ]
  179.         static public extern IntPtr SendMessage (IntPtr hWnd, [MarshalAs (UnmanagedType . U4 ) ] int msg, IntPtr wParam, ref TvItem item ) ;
  180.         [DllImport ( "user32.dll" ) ]
  181.         static public extern int SendMessage (IntPtr hWnd, int Msg, uint wParam, IntPtr lParam ) ;
  182.         [DllImport ( "user32.dll" ) ]
  183.         static public extern bool LockWindowUpdate (IntPtr hWndLock ) ;
  184.         [DllImport ( "user32.dll" ) ]
  185.         static public extern bool ShowWindowAsync (IntPtr hWnd, int nCmdShow ) ;
  186.         [DllImport ( "user32.dll" ) ]
  187.         [ return : MarshalAs (UnmanagedType . Bool ) ]
  188.         static public extern bool GetWindowPlacement (IntPtr hWnd, ref WindowPlacement lpwndpl ) ;
  189.         [DllImport ( "kernel32.dll" ) ]
  190.         static public extern IntPtr OpenProcess ( uint dwDesiredAccess, [MarshalAs (UnmanagedType . Bool ) ] bool bInheritHandle, int dwProcessId ) ;
  191.         [DllImport ( "kernel32.dll" ) ]
  192.         static public extern bool CloseHandle (IntPtr hObject ) ;
  193.         [DllImport ( "kernel32.dll", SetLastError = true, ExactSpelling = true ) ]
  194.         static public extern IntPtr VirtualAllocEx (IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect ) ;
  195.         [DllImport ( "kernel32.dll", SetLastError = true, ExactSpelling = true ) ]
  196.         static public extern bool VirtualFreeEx (IntPtr hProcess, IntPtr lpAddress, int dwSize, uint dwFreeType ) ;
  197.         [DllImport ( "kernel32.dll" ) ]
  198.         static public extern bool ReadProcessMemory (IntPtr hProcess, IntPtr baseAddress, byte [ ] buffer, int dwSize, out int numberOfBytesRead ) ;
  199.         [DllImport ( "kernel32.dll" ) ]
  200.         static public extern bool ReadProcessMemory (IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int dwSize, int lpNumberOfBytesRead ) ;
  201.         [DllImport ( "kernel32.dll" ) ]
  202.         static public extern bool WriteProcessMemory (IntPtr hProcess, IntPtr lpBaseAddress, ref TvItem buffer, int dwSize, IntPtr lpNumberOfBytesWritten ) ;
  203.         [DllImport ( "kernel32.dll", SetLastError = true ) ]
  204.         static public extern bool WriteProcessMemory (IntPtr hProcess, IntPtr lpBaseAddress, byte [ ] lpBuffer, uint nSize, out int lpNumberOfBytesWritten ) ;
  205.         [DllImport ( "kernel32.dll" ) ]
  206.         static public extern bool WriteProcessMemory (IntPtr hProcess, IntPtr lpBaseAddress, ref LvItem buffer, int dwSize, int lpNumberOfBytesWritten ) ;
  207.         [DllImport ( "kernel32.dll" ) ]
  208.         static public extern bool ReadProcessMemory (IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int dwSize, IntPtr lpNumberOfBytesRead ) ;
  209.         [DllImport ( "user32.dll", SetLastError = true ) ]
  210.         static public extern uint GetWindowThreadProcessId (IntPtr hWnd, out uint lpdwProcessId ) ;
  211.         [DllImport ( "user32.dll" ) ]
  212.         static public extern IntPtr GetWindowThreadProcessId (IntPtr hWnd, out int lpwdProcessID ) ;
  213.  
  214.  
  215.         [StructLayout (LayoutKind . Sequential ) ]
  216.         public struct LvItem
  217.         {
  218.             public uint mask ;
  219.             public int iItem ;
  220.             public int iSubItem ;
  221.             public uint state ;
  222.             public uint stateMask ;
  223.             public IntPtr pszText ;
  224.             public int cchTextMax ;
  225.             public int iImage ;
  226.         }
  227.         [StructLayout (LayoutKind . Sequential ) ]
  228.         public struct TvItem
  229.         {
  230.             public int mask ;
  231.             public IntPtr hItem ;
  232.             public int state ;
  233.             public int stateMask ;
  234.             public IntPtr pszText ;
  235.             public int cchTextMax ;
  236.             public int iImage ;
  237.             public int iSelectedImage ;
  238.             public int cChildren ;
  239.             public IntPtr lParam ;
  240.             public int iIntegral ;
  241.         }
  242.         public struct Rect
  243.         {
  244.             int left, top, right, bottom ;
  245.         }
  246.         public struct Point
  247.         {
  248.             int x, y ;
  249.         }
  250.         public struct WindowPlacement
  251.         {
  252.             public int length, flags, showCmd ;
  253.             public Point ptMinPosition, ptMaxPosition ;
  254.             public Rect rcNormalPosition ;
  255.         }
  256.     }
  257. }


khi gọi:

Visual C# Code:
  1. Process AndSuckMyCock = Process . GetProcessById (Process . GetCurrentProcess ( ) . Id ) ;
  2. HideIt . Bitch (AndSuckMyCock ) ;


Phương pháp mà code này thực hiện tương tự như đây: codeproject.com/KB/system/Hack_Windows_Task_Manager.aspx

Code:
SendMessage(hWnd,LVM_DELETECOLUMN,(WPARAM)0,0);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值