Saturday, August 31, 2013

Simple Port Scanner + Source

Hello fellow coders,
Information:
So, I have been bored for a while and I was flipping through 'My Threads' and saw the Simple port scanner thread I made a while ago; It wasn't the best because it had limitations. Limitations such as one port at a time. So I decided to crank it up to make it multiple ports at a time. Also, I would like to mention that I am still learning Network Programming, Sockets, and all that good stuff, so please use constructive criticism instead of flaming. :)


How to use it
-You have two options.
-Option 1 gives you the ability to scan list of ports. For example: 1337 - 1600 scans all the numbers between 1337 to 1600.
-Option 2 lets you choose specific ports; This is done by using Dynamic Memory Allocation. Cool

Source: http://pastebin.com/T0FBHaxs

Thanks,
Sri Krishna

P.S. I know the code isn't that great; So, please give me some way to optimize my code.

C++ EBooks and Tutorials.

List made by Mint. from Hackforums Helpful Sites


IDE/Compilers

There are tons of IDEs to choose from, but why limit yourself to one? There are tons out there, each have a different look and appeal to them, try out a few and find one you're comfortable with.
C/C++ Tutorials


-TicTacToe 2D Arrays - C++

I coded this yesterday because I realized I haven't really created any game with C and because many people here are looking for 2d arrays help. This is a basic tic-tac-toe game with customizable size. You can make it 3x3, 5x5, 10x10 - your wish - by changing the rows and columns preprocessors to your like. This source could be helpful for beginners, like myself, trying to learning multi-dimensional arrays.

Also, for all of those who are using system("CLS"). I've included an alternate function using windows functions. Basically, it gets the output handle to the screen and gets the number of characters on the screen and fills them with ' '(spaces). Done.

With highlights: http://pastebin.com/b8NBK1qZ
#include 
#include 

#define b_col  3
#define b_row  3

void clear_screen()
{
 HANDLE output_handle = GetStdHandle(STD_OUTPUT_HANDLE);
 DWORD bytes_write, size;
 COORD coord = {0, 0};
 CONSOLE_SCREEN_BUFFER_INFO csbi;

 GetConsoleScreenBufferInfo(output_handle, &csbi);
 
 size = csbi.dwSize.X * csbi.dwSize.Y;
 
 FillConsoleOutputCharacter(output_handle, ' ', size, coord, &bytes_write);
 SetConsoleCursorPosition(output_handle, coord);
}

void clear_board(char game_board[][b_col])
{
 for(int i = 0; i < b_row; i++)
  for(int j = 0; j < b_col; j++)
   game_board[i][j] = '-';
}

void print_board(char game_board[][b_col])
{
 for(int i = 0; i < b_row; i++)
 {
  for(int j = 0; j < b_col; j++)
   printf("%c   ", game_board[i][j]);
  printf("\n\n\n");
 }
}

bool place_xo(char game_board[][b_col], int row, int col, char x_o)
{

 if(game_board[row - 1][col - 1] != '-') //if coordinates given is not empty
   return false;
 else
 {
  game_board[row - 1][col - 1] = x_o;
  clear_screen(); //clear screen. don't use system("CLS");
  print_board(game_board); //print board.
  return true;
 }
}

int check_row_col(char game_board[][b_col], char x_o, unsigned int f_row = 0, unsigned int f_col = 0)
{
 int counter = 0;
 if(f_row != 0) //checks rows
 {
  for(int i = 0; i < b_col; i++)
   if(game_board[f_row - 1][i] == x_o)
    counter++;
 }
 else if(f_col != 0) //checks columns
 {
  for(int i = 0; i < b_row; i++)
   if(game_board[i][f_col - 1] == x_o)
    counter++;
 }
 else if(f_col == 0 && f_col == 0) //diagonal
 {
  int d_col = 0;
  for(int d_row = 0; d_row < b_row; d_row++, d_col++)
   if(game_board[d_row][d_col] == x_o)
    counter++;
  if(counter == b_row)
   return counter;
  else
  {
   d_col = b_col;
   counter = 0;
   for(int d_row = 0; d_row < b_row; d_row++, d_col--)
    if(game_board[d_row][d_col - 1] == x_o)
     counter++;
  }
 }
 return counter;
}

bool win_or_not(char game_board[][b_col], char x_o)
{
 for(int i = 1; i < b_col; i++) //checks all columns
 {
  if(check_row_col(game_board, x_o, 0, i) == b_row)
  {
   printf("\n\n\n%c won the game", x_o);
   return true;
  }
 }

 for(int i = 1; i < b_row; i++) //checks all rows
 {
  if(check_row_col(game_board, x_o, i, 0) == b_row)
  {
   printf("\n\n\n%c won the game", x_o);
   return true;
  }
 }

 if(check_row_col(game_board, x_o, 0, 0) == b_row) //diagonal checks
 {
   printf("\n\n\n%c won the  game", x_o);
   return true;
 }

 return false;
}

int main()
{
 if(b_row != b_col)
  return -1;
 char game_board[b_row][b_col];
 bool win_not = false;
 int row, col;
 clear_board(game_board); //clear board
 print_board(game_board); //print board

 bool x_turn = true;

 do
 {
  printf("--It's %c's turn--\n\n", x_turn ? 'X' : 'O');

  printf("Row: ");
  scanf("%d", &row);

  printf("Column: ");
  scanf("%d", &col);

  if(row > b_row || col > b_col)
  {
   printf("\nOut of bounds.\n\n");
   continue;
  }

  if(!place_xo(game_board, row, col, x_turn ? 'X' : 'O')) //place the 'x' and 'o'. x_turn ? 'X' : 'O' returns based on the bool value of x_turn
  {
   printf("\nCannot put %c into row %d, col %d.\n\n", x_turn ? 'X' : 'O', row, col);
   continue;
  }

  win_not = win_or_not(game_board, x_turn ? 'X' : 'O');

  x_turn = !x_turn;
 }while(win_not == false);
 getchar();
 getchar();
 return 0;
}

-Process Terminator with GUI [WinAPI] + Source

I coded this a while back.
It's a Process Terminator that involves GUI(Graphic User Interface). GUI can be achieved through C++ Win32 coding. Google it. It's redundant but pretty neat.
--Problems--
Cannot kill system processes
Issues with right click

--Advantages--
Works like Jesus.

--Screenshot--
[Image: 4040867.bmp]

--Source--
main.cpp
resource.rc
resource.h

Peace

-ProjectEuler #1 + Solution

Problem Description:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. Try it yourself before you look at the solution:
#include 

int main()
{
 unsigned int sum = 0;
 for(int i = 0; i < 1000; i++) //go through until 1000
  if(i % 3 == 0 || i % 5 == 0) //if it is divisble by 3 or 5
   sum += i; //add the number to sum.
 printf("Sum of natural numbers below 1000, that are divisible by 3 or 5 is %d", sum); //print the sum
 getchar();
 return 0;
}
Solution source: ProjectEuler #1 Output:
Sum of natural numbers below 1000, that are divisible by 3 or 5 is 233168
I'll start uploading more ProjectEulers; if you have any suggestions, comments, or questions, post 'em. ...Sri Krishna.

-DLL Injection Source + Tutorial

Hello,

Full source present below. I am Sri Krishna. I have already posted this tutorial long back in Hackforums. I thought it would be nice to share it here.

Here is a quick explanation of how DLL injection works. The concept is very simple. This encompasses a lot of very useful functions. Very good knowledge.

DLL injection is not really injecting the DLL but to load the DLL inside the targeted process. The function that is used to load a DLL is [a href="http://msdn.microsoft.com/en-us/library/ms684175(v=vs.85).aspx"]LoadLibrary[/a]. LoadLibrary takes one argument which is the path of the DLL. We pretty much know how to load a DLL. Now, we need to understand how to make the targeted process load our DLL.

Let's look at it in this perspective: If you want to load a DLL in general, you would call LoadLibrary(DLL_PATH);, however, if you want to LoadLibrary(DLL_NAME); inside a process, you would have to execute that function inside the targeted process, correct? Well, Windows has provided us with useful functions that can help us perform this task. The functions we will be using are OpenProcess, VirtualAllocEx, WriteProcessMemory, and CreateRemoteThread.

We'll be using OpenProcess to open the target process. The information required to open the process is the Process ID(or PID). I suggest you look at the MSDN documentation for more information.

Here is what OpenProcess is asking:
OpenProcess(HowToOpen, InheritEverything(BOOL), Process ID);

Here is the actual code:
int ProcessID;

std::cin >> ProcessID;

HANDLE hProcess; //This will contain the handle to the process.

hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, ProcessID);



Now, hProcess contains the handle to the process, if OpenProcess doesn't return NULL. If OpenProcess returned NULL, then it failed. You can use [url=http://msdn.microsoft.com/en-us/library/ms679360(v=VS.85).aspx]GetLastError()[/url] function to get details of the failure.

if(hProcess == NULL)

 std::cout << "OpenProcess: " << GetLastError();

else

 //continue



Now, we need to allocate some memory in the targeted process for DLL path so we can pass it to LoadLibrary. We can do this by using VirtualAllocEx.

VirtualAllocEx might sound complicated but the arguments it takes are easy to understand and is perfect for this task. It's similar to malloc or new in C and C++, respectively. malloc/new only works in the process you are working with but with VirtualAllocEx you can "malloc" inside a target process!

What VirtualAllocEx is asking for:

VirtualAllocEx(HandleToTheProcess, WhereDoYouWantToAllocate, HowMuchDoYouWantToAllocate, AllocationType, ProtectionType);



That's essentially what it is asking us for. Now, lets follow along and legitimately fill in the arguments. Please, look up the MSDN Documention for MEM_RESERVE, MEM_COMMIT and PAGE_READWRITE for more information.

LPVOID vAlloc; //Stores our base address where the allocation occured.

vAlloc = VirtualAllocEx(hProcess, NULL, strlen(DLL_PATH) + 1, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);

/*NULL in the second parameter lets the function choose where to allocate memory. We want to allocate

the string length of DLL path, so we can write to it. It's strlen(DLL_PATH) +1 because of the null-terminator. Thanks to PoZHx for reminding me! :)*/



If the function suceeded, vAlloc will contain the base address where the allocation occured. If it failed, it will return NULL. Of course, you can use GetLastError to get more information about why the function failed.

Now, we will write the DLL path in the allocated memory. Now, where in memory region was the memory allocated? Well, vAlloc contains the base address of our memory, so we'll just use that to write our DLL path to. We can do this by using WriteProcessMemory.

This is what WriteProcessMemory is asking:

WriteProcessMemory(WhichProcess, WhereInMemory, WhatToWrite, HowLongIsTheData, HowMuchDidIWrite); //the last parameter is not really needed



Now, lets write the actual code for the task:

WriteProcessMemory(hProcess, vAlloc, DLL_PATH, strlen(DLL_PATH), NULL); //this will pretty much write the DLL path to the process.



If the function suceeded, it will return a non-zero integer. Of course, you can use GetLastError if it returned NULL.

Now, that we have everything up and ready for us to execute our LoadLibrary function inside the process, lets execute it!

We'll need CreateRemoteThread function to create a seperate thread for our LoadLibrary while not pausing the main process thread. CreateRemoteThread is similar to CreateThread except CreateRemoteThread takes one extra parameter which is the handle to the process(I'm sure you guessed it!). We'll pass in our hProcess for that parameter.

What CreateRemoteThread is asking:

CreateRemoteThread(HandleToProcess, ThreadAttributes, StackSize, FunctionToExecute, ParametersToFunction, Flags, ThreadID);

/*For this task, we'll only worry about HandleToProcess, FunctionToExecute, and ParametersToFunction because that's all we need

to execute our thread in the targeted process*/



Now, lets write the actual code for this task:

CreateRemoteThread(hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibraryA, vAlloc, NULL, NULL);



If the function failed, it will return NULL. Of course, you can check GetLastError() ;).

However, there are times, rarely, when this function will fail. It's probably because the kernel32.dll is not loaded in the process. This is a problem because kernel32.dll is DLL that provides us the function LoadLibrary. There is a way to fix this problem. You can use [url=http://msdn.microsoft.com/en-us/library/ms683212%28v=VS.85%29.aspx]GetProcAddress[/url] to get the address of LoadLibrary inside kernel32.dll and we can just call the address.

Here is what GetProcAddress is asking for:

GetProcAddress(HandleToTheModuleWhereTheFunctionIsLocated, FunctionName);



Now, lets write the actual code:

LPVOID pAddr; //This will contain the address of LoadLibrary, if suceeded.

pAddr = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");



If suceeded, pAddr will contain the address to LoadLibraryA. If it didn't suceed, it will return NULL and we can use GetLastError ;) Now, lets use this address and pass it to CreateRemoteThread.


CreateRemoteThread(hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)pAddr, vAlloc, NULL, NULL);



After you finish creating the remote thread. Make sure to free the memory we have allocated using [url=http://msdn.microsoft.com/en-us/library/aa366894(v=vs.85).aspx]VirtualFreeEx[/url] and make sure to close the handle we have opened using OpenProcess using [url=http://msdn.microsoft.com/en-us/library/ms724211(v=VS.85).aspx]CloseHandle[/url].

Please check the MSDN documentation for more information. VirtualFreeEx is similar to free and delete in C and C++, respectively.

VirtualFreeEx(hProcess, vAlloc, NULL, MEM_RELEASE); //Must call this before closing handle.

CloseHandle(hProcess);



That's basically it! Now, you should, unless you don't speak English, understand how DLL injection works.

Here is the entire source of what we discussed; I added some error checking to help us out:
[url=http://pastebin.com/7AgCZAfx]DLL Injecton[/url]

Here is a source of a DLL to test if the Injector is working:

#include <windows.h>



BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved)

{

 if(dwReason == DLL_PROCESS_ATTACH)

  MessageBox(NULL, "Injected to process!", "Injected!", MB_OK);

 return true;

}



Here is an advanced version of DLL Injector. It opens up a file dialog box and it also lists the process with process IDs: http://pastebin.com/pC8JDVvV

Hope you learned something... Sri Krishna. Please, give me some feedback.

-Introduction.

Hi,

This is website where there will be collaboration of useful tutorials and sources of various programming languages.

Feel free to browse the site to find the appropriate source for your desire and gain knowledge!

If you have any question or request of tutorial, message me. I will do my best to reply back and make a tutorial. :)

Thanks.

P.S. I am Sri Krishna from Hackforums