发新话题
打印

[原创]自己写的枚举进程和杀进程的小工具

[原创]自己写的枚举进程和杀进程的小工具

文章作者: zhouzhen[E.S.T]
信息来源: 邪恶八进制中国 http://www.eviloctal.com/forum

process list & process kill
http://zhouzhen.blogchina.com

Usage:  ps.exe <OPTION>
Example: ps.exe -l
       ps.exe -k pid
Code by zhouzhen

ProcessName     ProcessID
[System Process]      0
System                8
smss.exe              148
csrss.exe              172
WINLOGON.EXE        192
services.exe            220
LSASS.EXE            232
svchost.exe            412
SPOOLSV.EXE          440
svchost.exe            476

C:\Documents and Settings\Administrator>ps -k 1292

[+]SetPrivilege ok!
[+]process 1292 has been killed!

kill process Code by zhouzhen

可以杀系统进程 :)

附件

ps.rar (16 KB)

2005-3-14 14:39, 下载次数: 199

安全就象毒品一样,上瘾了就戒不掉了 http://www.6code.net

TOP

鼓励一下!

杀系统进程只须提升一下本进程的令牌权限,让它有调试权限即可:

HANDLE     hToken,hProcess;

     TOKEN_PRIVILEGES tp;

     char *pSEDEBUG="SeDebugPrivilege";

     hProcess=GetCurrentProcess();


OpenProcessToken
(
hProcess,TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken);

     LookupPrivilegeValue(NULL,pSEDEBUG,&tp.Privileges[0].Luid);

     tp.PrivilegeCount=1;

     tp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;

     AdjustTokenPrivileges(hToken,FALSE,&tp,NULL,NULL,NULL
);
http://www.xyzreg.net

TOP

老大,,
能不能把源代码公开,

让俺也学学呢
====================== 我的网站:http://www.anhacker.com 请您联系:anmeihong@sina.com ****************************** ******************************

TOP

呵呵。。精华都被xyzreg 给说了。。就是提升一下进程权限拉。。:) 代码很烂就是加上了出错处理。 枚举进程用 TOOLHELP32 API  hProcessSnap , Process32First, Process32Next
安全就象毒品一样,上瘾了就戒不掉了 http://www.6code.net

TOP

现在都用Native API了.

ZwQuerySystemInformation

SYSTEM_INFORMATION_CLASS

TOP

引用:
下面是引用zhouzhen于2005-03-16 20:42发表的:
呵呵。。精华都被xyzreg 给说了。。就是提升一下进程权限拉。。:)
[s:51]  [s:39]
http://www.xyzreg.net

TOP

在窗体上添加一个ListView,设置其ViewStyle为vsReport,在ListView上添加三个Column,再添加一个Button。

void __fastcall TForm1::Button1Click(TObject *Sender)
{
ListView2->Items->Clear();
TListItem *mItem;
AnsiString ExeFile;
AnsiString PathFile;
Pointer pt,pt2;
unsigned int s;
DWORD size,size2;
HANDLE snapshot;
PROCESSENTRY32 processinfo;
processinfo.dwSize = sizeof(processinfo);
snapshot =CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if (snapshot==NULL) return;
bool flag = Process32First (snapshot,&processinfo);
while (flag)
{
  mItem=ListView2->Items->Add();
  ExeFile=AnsiString(processinfo.szExeFile);

  mItem->Caption=ExeFile;
  mItem->SubItems->Add(
  IntToStr(int(processinfo.th32ParentProcessID)));

  mItem->SubItems->Add(
  IntToHex(int(processinfo.th32ProcessID),8).UpperCase());
  HANDLE hProcess=OpenProcess(PROCESS_QUERY_INFORMATION |
      PROCESS_VM_READ,FALSE,processinfo.th32ProcessID);
  if(hProcess)
  {
    char buf[MAX_PATH+1];
    HMODULE hMod;
    DWORD cbNeeded;
    if( EnumProcessModules( hProcess, &hMod, sizeof( hMod ),
      &cbNeeded ) )
    {
      int len=GetModuleFileNameEx( hProcess, hMod,buf,sizeof buf);
      buf[len]=&#39;\0&#39;;
      PathFile=buf;
      if(FileExists(PathFile))
      {
        size=GetFileVersionInfoSize(ExeFile.c_str(),&size2);
        pt=malloc(size);
        if(GetFileVersionInfo(ExeFile.c_str(),NULL,size,pt))
        {
          if(VerQueryValue(pt,"\\StringFileInfo\\040904E4\\FileVersion",&pt2,&s))
            mItem->SubItems->Add(PChar(pt2));
          if(VerQueryValue(pt,"\\StringFileInfo\\040904E4\\CompanyName",&pt2,&s))
            mItem->SubItems->Add(PChar(pt2));
          if(VerQueryValue(pt,"\\StringFileInfo\\040904E4\\FileDescription",&pt2,&s))
            mItem->SubItems->Add(PChar(pt2));
        }
        free(pt);
      }
    }
    CloseHandle(hProcess);
  }
  flag = Process32Next(snapshot,&processinfo);
}
}

//杀死进程这里是选择进程,你可以判断是否===你的进程.:

//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
if (ListView2->SelCount==0)
{
  //MessageBox(Handle,"请首先选择一个进程!","中止进程",MB_OK&brvbar,MB_ICONWARNING);
  ShowMessage("请首先选择一个进程!");
  return;
}
int pPid=StrToInt(ListView2->Selected->SubItems->Strings[0]);
HANDLE ps = OpenProcess(1,false,pPid);
// if(ps&&TerminateProcess(ps,1)){
if(ps&&TerminateProcess(ps,-9))
{
  //MessageBox(Handle,"成功中止进程!","中止进程",MB_OK&brvbar,MB_ICONINFORMATION);
  ShowMessage("成功中止进程!");
}
else
ShowMessage("中止进程失败!");
//MessageBox(Handle,"中止进程失败!","中止进程",MB_OK&brvbar,MB_ICONWARNING);
//ViewProcess();
}

TOP

我是用c++builder 写的,怎么样,也不错吧

TOP

我的网军小工兵中也有此功能,哈哈啊~~
http://hi.baidu.com/fengze

TOP

  6楼用bcb写的程序可视化增强了不少,但是我认为这个程序运行在Windows 9x/Me上应该没有问题但是在Windows NT以上的系统上由于Windows NT的安全机制,不能杀掉某些进程,OpenProcess函数会返回ERROR_ACCESS_DENIED的错误,如一些以服务启动的进程。在Windows NT中要杀掉以服务启动的进程要用到一楼的方法。

4楼说的未文档化ZwQuerySystemInformation函数是一个NTDLL.DLL中导出的函数,通过他可以存取大量系统信息,可以用它列出当前运行的系统进程。这个函数用在Windows NT(<5.0)中,在杀进程的时候用它也只是为获取一些要杀进程的信息,最后杀的时候还是用到一楼说的方法的。所以要是win2000或是xp的话不如用ToolHelp32 API。未文档化的函数并不是有多神秘,而且也在发展变化,不是很稳定,在各版本的windows中兼容性不是很好。

我也试着写了一个类似的程序,vc6.0编译,运行在win2k或xp系统上。
复制内容到剪贴板
代码:
#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>

void ValidateArgs(int argc, char **argv); //Parse the command line arguments
void usage(); //Print usage information and exit
void CheckOS(); //Check the OS we are running on
void EnumPS(); //Enumerate processes
BOOL KillPS(DWORD id); //kill process
BOOL SetPrivilege(HANDLE hToken,LPCTSTR lpszPrivilege,BOOL bEnablePrivilege); //need this function to set privilege

int main(int argc,char *argv[])
{
     CheckOS();
     ValidateArgs(argc,argv);
   return 0;
}

void ValidateArgs(int argc, char **argv)
{
   int           i;
     if(argc!=1){
         
          for(i = 1; i < argc; i++)
          {
              if ((argv[i][0] == &#39;-&#39;))
              {
                   switch (tolower(argv[i][1]))
                   {
           case &#39;e&#39;:
                        EnumPS();
              break;
           case &#39;k&#39;:   
                        KillPS(atol(&argv[i+1][0]));
                        break;
                        
           default:
              usage();
              break;
                   }
              }
          }

     }else{ usage(); }
     
}


void usage()
{
     printf("\n");
     printf("\t\t--- code by 恶猫[E.S.T]          ---\n");
     printf("\t\t--- E-mail: [email]EvilC4t@126.com[/email]        ---\n");
     printf("\t\t--- HomePage: [url]www.eviloctal.com/forum[/url] ---\n");
     printf("\t\t--- Date: 04-17-2005            ---\n\n");
     printf("usage: ps.exe <OPTION>\n\n");
     printf("     -e    enumerate processes\n");
     printf("     -k:ProcessID    the ProcessID you want to kill\n");
     ExitProcess(1);
}
void CheckOS()
{
     OSVERSIONINFO osinfo;

     osinfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
     if (!GetVersionEx(&osinfo))
     {
          printf("Unable to get OS version!\n");
          ExitProcess(1);
     }
     
     if (osinfo.dwPlatformId & VER_PLATFORM_WIN32_NT)
     {
          if (osinfo.dwMajorVersion<5)
          {
              printf("ToolHelp API isn&#39;t support on NT versions prior to Windows 2000!\n");
              ExitProcess(1);
          }
     }
}


void EnumPS()
{
     HANDLE hSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
     if (!hSnapshot)
     {
          printf("Unable to create snapshot!\n");
          return;
     }
     
     PROCESSENTRY32 pe;
     ULONG count=0;
     
     pe.dwSize=sizeof(PROCESSENTRY32);
     BOOL retval=Process32First(hSnapshot,&pe);
     while(retval)
     {
          count++;
          printf("[%d] %s\n",pe.th32ProcessID,pe.szExeFile);
          retval=Process32Next(hSnapshot,&pe);
     }
     
     CloseHandle(hSnapshot);

     printf("%lu processes enumerated\n",count);
}




BOOL KillPS(DWORD id)
{
     HANDLE hProcess=NULL,hProcessToken=NULL;
     BOOL IsKilled=FALSE,bRet=FALSE;
     __try
     {
         
          if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ALL_ACCESS,&hProcessToken))
          {
              printf("\nOpen Current Process Token failed:%d",GetLastError());
              __leave;
          }

          if(!SetPrivilege(hProcessToken,SE_DEBUG_NAME,TRUE))
          {
              __leave;
          }
         
          if((hProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,id))==NULL)
          {
              printf("\nOpen Process %d failed:%d",id,GetLastError());
              __leave;
          }

          if(!TerminateProcess(hProcess,1))
          {
              printf("\nTerminateProcess failed:%d",GetLastError());
              __leave;
          }else{ printf("process %d has been killed",id); }

          IsKilled=TRUE;
     }
     __finally
     {
          if(hProcessToken!=NULL) CloseHandle(hProcessToken);
          if(hProcess!=NULL) CloseHandle(hProcess);
     }
     return(IsKilled);
}




BOOL SetPrivilege(HANDLE hToken,LPCTSTR lpszPrivilege,BOOL bEnablePrivilege)
{
     TOKEN_PRIVILEGES tp;
     LUID luid;
     
     if(!LookupPrivilegeValue(NULL,lpszPrivilege,&luid))
     {
          printf("\nLookupPrivilegeValue error:%d", GetLastError() );
          return FALSE;
     }
     tp.PrivilegeCount = 1;
     tp.Privileges[0].Luid = luid;
     if (bEnablePrivilege)
          tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
     else
          tp.Privileges[0].Attributes = 0;
     
     AdjustTokenPrivileges(
          hToken,
          FALSE,
          &tp,
          sizeof(TOKEN_PRIVILEGES),
          (PTOKEN_PRIVILEGES) NULL,
          (PDWORD) NULL);
     
     if (GetLastError() != ERROR_SUCCESS)
     {
          printf("AdjustTokenPrivileges failed: %u\n", GetLastError() );
          return FALSE;
     }
     return TRUE;
}
我用青春赌明天

TOP

哇~~
各高手,各显神通啊~~
学习ING~~
通往胜利的路不只一条!

TOP

楼上几位说的ZwQuerySystemInformation,我建议在枚举进程的时候尽量不要用他.

TOP

pjf  [s:52]
你的意思是那通过修改进程活动链表的进程,不会被ZwQuerySystemInformation发现吧?当然还有其他很多的办法隐藏进程.
如果要是改用读取windows的dispatcher所使用的内核链表的话,就可以了吧?
关于同志们所说的native api 就参考我写的这个吧  :)http://www.xfocus.net/articles/200503/782.html

TOP

汗 看来没用delphi写一个的 我研究下 回来发上来

TOP

写得真是好。。。。。。。什么时候我才能写得出来啊。。。。。。
我有点郁闷了啊。。。。努力学习ING~
昨天是明天的前天今天是昨天的明天明天是昨天的后天    ╰o╯╰o╯

TOP

下面这个是枚举进程以及进程加载的模块,杀进程的reg兄已经说了。。。:

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "psapi.h"

void PrintModules( DWORD processID )
{
   HMODULE hMods[1024];
   HANDLE hProcess;
   DWORD cbNeeded;
   unsigned int i;

   // Print the process identifier.

   printf( "\nProcess ID: %u\n", processID );

   // Get a list of all the modules in this process.

   hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION |
                        PROCESS_VM_READ,
                        FALSE, processID );
   if (NULL == hProcess)
      return;

   if( EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
   {
      for ( i = 0; i < (cbNeeded / sizeof(HMODULE)); i++ )
      {
        char szModName[MAX_PATH];

        // Get the full path to the module&#39;s file.

        if ( GetModuleFileNameEx( hProcess, hMods[ i], szModName,
                          sizeof(szModName)))
        {
           // Print the module name and handle value.

           printf("\t%s (0x%08X)\n", szModName, hMods[ i] );
        }
      }
   }

   CloseHandle( hProcess );
}

void PrintProcessNameAndID( DWORD processID )
{
   char szProcessName[MAX_PATH] = "unknown";

   // Get a handle to the process.

   HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                        PROCESS_VM_READ,
                        FALSE, processID );

   // Get the process name.

   if (NULL != hProcess )
   {
      HMODULE hMod;
      DWORD cbNeeded;

      if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
         &cbNeeded) )
      {
        GetModuleBaseName( hProcess, hMod, szProcessName,
                     sizeof(szProcessName) );
      }
      else return;
   }
   else return;

   // Print the process name and identifier.

   printf( "%s (Process ID: %u)\n", szProcessName, processID );

   CloseHandle( hProcess );
}

void usage(char *);

void main(int argc,char *argv[] )
{
   // Get the list of process identifiers.

   DWORD aProcesses[1024], cbNeeded, cProcesses;
   unsigned int i;
     switch(argc)
     {
          case 2:
              if(lstrcmpi(argv[1],"-l"))
                   usage(argv[0]);
   if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
      return;

   // Calculate how many process identifiers were returned.

   cProcesses = cbNeeded / sizeof(DWORD);

   // Print the name of the modules for each process.

   for ( i = 0; i < cProcesses; i++ )
      PrintProcessNameAndID( aProcesses[ i] );
     break;

          case 3:
              if(lstrcmpi(argv[1],"-p"))
                   usage(argv[0]);
              DWORD spProcess=(DWORD)atoi(argv[2]);
              PrintModules(spProcess);
              break;
          default:
          usage(argv[0]);

}

}

void usage(char *name)
{
     printf("\t  Processes and modules enumerator\n\n");
     printf("\t   wrriten by superlone@EST\n\n");
     printf("Usage:\n\t %s -l    enumerate Processes\n",name);
     printf("\t %s -p PID Enumerate modules of specific Process\n",name);
     exit(0);

}

都是msdn上的,HOHO~~~
俺是mika!别叫错了! 俺的QQ:794773 http://hi.baidu.com/stealthwalker/ my private area ------------------------------------------------------------ <a href=http://hi.baidu.com/stealthwalker target=_blank></a>

TOP

忘了说明以下了,这个程序是在lcc下成功编译执行效果如下:

H:\temp\lcc>print
       Processes and modules enumerator

        wrriten by superlone@EST

Usage:
      print -l    enumerate Processes
      print -p PID Enumerate modules of specific Pro

H:\temp\lcc>print -l
smss.exe (Process ID: 504)
winlogon.exe (Process ID: 576)
services.exe (Process ID: 620)
lsass.exe (Process ID: 632)
svchost.exe (Process ID: 804)
svchost.exe (Process ID: 852)
svchost.exe (Process ID: 1052)
svchost.exe (Process ID: 1180)
mdm.exe (Process ID: 1216)
vmware-authd.exe (Process ID: 1456)
vmnat.exe (Process ID: 1468)
vmnetdhcp.exe (Process ID: 1484)
Explorer.EXE (Process ID: 1732)
conime.exe (Process ID: 1832)
SOUNDMAN.EXE (Process ID: 232)
pfw.exe (Process ID: 352)
ctfmon.exe (Process ID: 452)
svchost.exe (Process ID: 1104)
CCENTER.EXE (Process ID: 1804)
Ravmond.exe (Process ID: 1720)
RavStub.exe (Process ID: 1012)
RAVMON.EXE (Process ID: 1372)
RAVTIMER.EXE (Process ID: 1648)
SysInfoMyWork.exe (Process ID: 1836)
dexplore.exe (Process ID: 1800)
QQ.exe (Process ID: 488)
TIMPlatform.exe (Process ID: 1572)
asiadial.exe (Process ID: 1896)
qqpet.exe (Process ID: 260)
gsfbwsr.exe (Process ID: 176)
XDICT.EXE (Process ID: 2084)
msiexec.exe (Process ID: 2276)
wedit.exe (Process ID: 2548)
cmd.exe (Process ID: 2652)
print.exe (Process ID: 2684)

H:\temp\lcc>print -p 2652

Process ID: 2652
      D:\WINDOWS\system32\cmd.exe (0x4AD00000)
      D:\WINDOWS\system32\ntdll.dll (0x77F40000)
      D:\WINDOWS\system32\kernel32.dll (0x77E40000)
      D:\WINDOWS\system32\msvcrt.dll (0x77BA0000)
      D:\WINDOWS\system32\ADVAPI32.dll (0x77DA0000)
      D:\WINDOWS\system32\RPCRT4.dll (0x77C50000)
      D:\WINDOWS\system32\USER32.dll (0x77D00000)
      D:\WINDOWS\system32\GDI32.dll (0x77C00000)
      D:\WINDOWS\system32\MPR.dll (0x71BD0000)
      D:\WINDOWS\system32\IMM32.DLL (0x76290000)
      D:\WINDOWS\system32\LPK.DLL (0x62D80000)
      D:\WINDOWS\system32\USP10.dll (0x73010000)
俺是mika!别叫错了! 俺的QQ:794773 http://hi.baidu.com/stealthwalker/ my private area ------------------------------------------------------------ <a href=http://hi.baidu.com/stealthwalker target=_blank></a>

TOP

用wmi也能实现:

pro="PID ProcessName"
for each ps in getobject("winmgmts:\\.\root\cimv2:win32_process").instances_
pro=pro&chr(10)&ps.handle&vbtab&ps.name
next
wscript.echo pro

双击这个vbe文件就可以了。
You're my FF~, forever~ ^_^

TOP

以上是枚举进程。
杀进程同样可以用wmi,我在这儿找到了个完整的代码:
http://seac.blogchina.com/blog/424925.html
You're my FF~, forever~ ^_^

TOP

引用:
下面是引用sunwear于2005-06-01 06:36发表的:
pjf  [s:52]
你的意思是那通过修改进程活动链表的进程,不会被ZwQuerySystemInformation发现吧?当然还有其他很多的办法隐藏进程.
如果要是改用读取windows的dispatcher所使用的内核链表的话,就可以了吧?
关于同志们所说的native api 就参考我写的这个吧  :)http://www.xfocus.net/articles/200503/782.html
这是pjf_fs,不是pjf

记得上次有个论坛有个一模一样的id pjf,我还去搭讪,要QQ,结果不用说,很ft
[s:51]

TOP

好用啊.....对于webshell可以杀一些东东.....
可是偶马上上传后用cmd.exe.
E:\web\benben\IMG\sob\ps.exe -l

ProcessName     ProcessID
[System Process]   0
System          8
smss.exe        172
csrss.exe        200
winlogon.exe      220
services.exe      248
lsass.exe        260
svchost.exe      464
spoolsv.exe      492
msdtc.exe        520
svchost.exe      628
rmserver.exe      640
KVSrvXP.exe      664
llssrv.exe       704
regsvc.exe       756
MSTask.exe       788
ServUDaemon.exe    808
WinMgmt.exe      868
svchost.exe      888
inetinfo.exe      900
dllhost.exe      1164
Dfssvc.exe       1172
Explorer.EXE      1296
dllhost.exe      1304
mdm.exe         1456
Rundll32.exe      1712
igfxtray.exe      1732
hkcmd.exe        1736
realsched.exe     1748
realplaiy.exe     1764
SOUNDMAN.EXE      1804
internat.exe      1824
QQ.exe          1856
svchost.exe      1780
TIMPlatform.exe    2072
sstext3d.scr      1480
cmd.exe         2028
ps.exe          584

process list Code by zhouzhen


然后
E:\web\benben\IMG\sob\ps.exe -k 664
显示
AdjustTokenPrivileges failed: 1300
[+]process 664 has been killed!

kill process Code by zhouzhen

然后又
E:\web\benben\IMG\sob\ps.exe -l
显示
ProcessName     ProcessID
[System Process]   0
System          8
smss.exe        172
csrss.exe        200
winlogon.exe      220
services.exe      248
lsass.exe        260
svchost.exe      464
spoolsv.exe      492
msdtc.exe        520
svchost.exe      628
rmserver.exe      640
KVSrvXP.exe      664      --------------------------我挫哦....
llssrv.exe       704
regsvc.exe       756
MSTask.exe       788
ServUDaemon.exe    808
WinMgmt.exe      868
svchost.exe      888
inetinfo.exe      900
dllhost.exe      1164
Dfssvc.exe       1172
Explorer.EXE      1296
dllhost.exe      1304
mdm.exe         1456
Rundll32.exe      1712
igfxtray.exe      1732
hkcmd.exe        1736
realsched.exe     1748
realplaiy.exe     1764
SOUNDMAN.EXE      1804
internat.exe      1824
QQ.exe          1856
svchost.exe      1780
TIMPlatform.exe    2072
sstext3d.scr      1480
cmd.exe         2028
ps.exe          1176

process list Code by zhouzhen

....................

TOP

发新话题