Sunday, August 18, 2013

Cracking and making keygen of a C# App

Cracking and Keygen:

    This tutorial aims to provide you with the knowledge of cracking an application and creating a keygen.

    Tools:
       GRE 4800 words from ienglishtest or mediafire.com
       OllyDBG 
       IDAPro v5.0 Freeware or find the v6.0 via google.com
       ILspy
       Visual Studio 2012 or any other

Install the software and then try to enter any activation code. You'll fail.
Now try to open the Pacific Lava.exe from OllyDBG.

click to view larger size

Click to view larger size





























But we get Error!!
Error : Unable to start 'E:\PacificLava\PacificLava.exe'
Well, this sucks. But let's open it with IDAPro.
On analyzing the executable with IDAPro, you'll find out that the executable is not PE but a .net executable.
Due to this reason OllyDBG was not able to disassemble it.

Now, Open the executable in ILspy. ILSpy is the best open-source .NET assembly browser and decompiler.
Press CTRL+O and Select the Pacific Lava.exe
This should load the exe in ILSpy.
Now expand the PacificLava upto the point shown in the fig. below.

Click to view larger size















The class highlighted above is ActivationCode. Expand it and you'll see the implementation of that class.
Click on File->Save Code.(Ctrl+S) and save the code in a new directory. I'll name the directory "PCode". Close ILSpy and open the "PCode" directory. You'll find PacificLava.csproj inside that folder. Double click on it and your Visual Studio will open that project. Expand the project upto ActivationCode as shown in the figure below. Scroll down the code pane until you see VerifyActivationCode() method.

Click to view larger size
















The method is as below:


        public bool VerifyActivationCode(string code) //code is the string entered by the user.
        {
            bool result = false;
            if (code.Substring(0, 2).Equals("H1")) //The first two characters of code are compared to "H1"
            {
                string x = code.Substring(2, 6);  //gets 6 characters from the 2nd pos.
                string value = code.Substring(8, 3); //gets 3 characters from the 8th pos.
                string sXCH = this.GetSXCH(x, "CAT"); //GetSXCH method is called
                if (sXCH.Equals(value))  // if values are equal
                {
                    result = true;      // then the key entered is correct
                }
            }
            return result;
        }
Let the code = "H1757807300".
Now, the first two character of "code" are "H1" so the if() statement in the third line is evaluated as true and the code continues to execute.

The variable x becomes = "757807" and value becomes = "300".
The function GetSXCH is called with "757807" and "CAT"and the returned string is stored in sXCH.

Let's suppose the returned string is "500". Now this "500" is compared to our "300" (stored in the variable "value") in the 7th statement. and if they are equal then the key entered is correct. So we need to understand how to bypass this if() statement to correctly register the program.

Copy the VerifyActivationCode() block and the GetSXCH() block into a file.
Click on File->Close Solution.
Click on File->New Project(Ctrl+Shift+N) and select a form based Visual C# application.
Name it PLGREKeygen or whatever you like.

Click to view larger size
















 After that add  a text box and Button via the toolbox.
The name property of the textbox should be "textBox1". Put whatever you like in the "Text" property of the textbox and the button.
















After that double click on the button. and you'll be in the code editing window.
Paste the ActivateVerificationCode() and the GetSXCH() functions in this window below the closing bracket of button1_Click. So your code will look like this.

















private void button1_Click(object sender, EventArgs e)
{

}
public bool VerifyActivationCode(string code)
{
            bool result = false;
            if (code.Substring(0, 2).Equals("H1"))
            {
                string x = code.Substring(2, 6);
                string value = code.Substring(8, 3);
                string sXCH = this.GetSXCH(x, "CAT");
                if (sXCH.Equals(value))
                {
                    result = true;
                }
            }
            return result;
}

private string GetSXCH(string x, string d)
{
            string text = d;
            try
            {
                int num = (int)Convert.ToInt16(x.Substring(0, 1));
                int num2 = (int)Convert.ToInt16(x.Substring(0, 2));
                int num3 = (int)Convert.ToInt16(x.Substring(0, 3));
                int num4 = (int)Convert.ToInt16(x.Substring(1, 1));
                int num5 = (int)Convert.ToInt16(x.Substring(1, 2));
                int num6 = (int)Convert.ToInt16(x.Substring(1, 3));
                int num7 = (int)Convert.ToInt16(x.Substring(3, 3));
                int num8 = (int)Convert.ToInt16(x.Substring(4, 1));
                int num9 = (int)Convert.ToInt16(x.Substring(4, 2));
                int num10 = (int)Convert.ToInt16(x.Substring(5, 1));
                text = "000" + Convert.ToString(Math.Abs(1000 - (num + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10)));
                text = text.Substring(text.Length - 3);
            }
            catch
            {
            }
            return text;
}

"private void button1_Click(object sender, EventArgs e)" is called and the code inside this function is executed whenever user clicks on the button on the keygen.

To generate a legitimate key we need:
1. a string starting with H1
2. 6 digit number
3. 3 digit number which can validate the 6 digit number.

The first two conditions can be satisfied easily but what about the third one? Look at the VerifyActivationCode function. In the 6th line a call to GetSXCH is made with 6 digit number string and "CAT" string. We'll use this for our benefit.

Code:

private void button1_Click(object sender, EventArgs e)
{
            Random rand = new Random(DateTime.Now.Second);
            int ran = rand.Next(0,1000000);
            int code = this.GetSXCH(ran.ToString(), "CAT");
            String key = "H1" + ran.ToString() + code.ToString();
            textBox1.text = key;
}

This function above will generate the key and display it on the textbox. Save the file and click on Debug->Start Debugging or Press F5.

To check if your key gen is working properly you can also code the above function like this:

private void button1_MouseClick(object sender, MouseEventArgs e)
{
            Random rand = new Random(DateTime.Now.Second);
            int ran = rand.Next(0,1000000);
            textBox1.Text = "H1" + ran.ToString() + this.GetSXCH(ran.ToString() , "CAT");
            if (VerifyActivationCode(textBox1.Text))
            {
                button1.Text = "Done.";
            }
}












Cheers!!!

Friday, July 12, 2013

Subway Surfers with Keyboard Play, Script and torrent

Subway Surfers PC Keyboard Playable with script and torrent

Subway Surfers is usually pain in the hand when played with the mouse. The solution is to use the scripting language 'Autohotkey'.

http://cdn2.angrygadgets.com/wp-content/uploads/2013/05/subway-surfers-Logo.png 
The autohotkey Script to simulate mouse in the game would be:

#NoTrayIcon
#SingleInstance force
Loop
{
    Process, Exist, Subway_Surfers.exe
        If ! errorLevel
            ExitApp
    Sleep, 200
}
Right::MouseClickDrag, Left, 720, 450, 820, 450
return
Left::MouseClickDrag, Left, 720, 450, 620, 450
return
Up::MouseClickDrag, Left, 720, 450, 720, 350
return
Down::MouseClickDrag, Left, 720, 450, 720, 550
return
Space::MouseClick, Left, , , 2
return

Friday, June 21, 2013

Tic Tac Toe clone in Turbo C

/*

    Criss-Cross is a clone of popular game Tic-Tac-Toe

    Copyright (C) 2013 Abhishek Baddi

    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 3 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, see http://www.gnu.org/licenses/.

*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>

int board[3][3], r=20, play=1, coorx[9], coory[9], midx, midy, cellspc=50, x, y, i, j;

int xwin=0, owin=0;

void xoxo(int , int, int );
void drawboard();
int checkr(int );
int checkc(int );


int main() {
 int gd=DETECT, gm, winner=-1,count=0;
 char cha[8];
 initgraph(&gd, &gm, "C:\\TC\\BGI");
 midx=getmaxx()/2;
 midy=getmaxy()/2;
 itoa(midx,cha,10);
 outtext(cha);
 itoa(midy,cha,10);
 outtext(cha);
start: count=1;
 settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
 for(i=0;i<3;i++)
  for(j=0;j<3;j++)
   board[i][j]=2;
 for(x=0,j=midy+cellspc;j>=midy-cellspc;j-=cellspc) {
  for(i=midx-cellspc;i<=midx+cellspc;x++,i+=cellspc) {
   coorx[x]=i;
   coory[x]=j;
//   outtext("x=");
//   itoa(coorx[x],cha,10);
//   outtext(cha);
//   outtext("y=");
//   itoa(coory[x],cha,10);
//   outtext(cha);
//   circle(coorx[x],coory[x],r);
//   getch();
  }
 }
 while(1) {
  setcolor(WHITE);
  drawboard();
  if(kbhit()){
  getcharacter:
  setcolor(WHITE);
  /*
  itoa(board[0][0],cha,10);
  outtextxy(0,midy,cha);
  itoa(board[0][1],cha,10);
  outtextxy(10,midy,cha);
  itoa(board[0][2],cha,10);
  outtextxy(20,midy,cha);
  itoa(board[1][0],cha,10);
  outtextxy(0,midy+30,cha);
  itoa(board[1][1],cha,10);
  outtextxy(10,midy+30,cha);
  itoa(board[1][2],cha,10);
  outtextxy(20,midy+30,cha);
  itoa(board[2][0],cha,10);
  outtextxy(0,midy+30+30,cha);
  itoa(board[2][1],cha,10);
  outtextxy(10,midy+30+30,cha);
  itoa(board[2][2],cha,10);
  outtextxy(20,midy+30+30,cha);*/
   cha[0]=getch();
   switch(cha[0]) {
    case '1':
     if(board[2][0]!=0&&board[2][0]!=1) {
      xoxo(coorx[0],coory[0],play);
      board[2][0]=play;
      count++;
      play^=0x1;
     }
     break;
    case '2':
     if(board[2][1]!=0&&board[2][1]!=1) {
      xoxo(coorx[1],coory[1],play);
      board[2][1]=play;
      count++;
      play^=0x1;
     }
     break;
    case '3':
     if(board[2][2]!=0&&board[2][2]!=1) {
      xoxo(coorx[2],coory[2],play);
      board[2][2]=play;
      count++;
      play^=0x1;
     }
     break;
    case '4':
     if(board[1][0]!=0&&board[1][0]!=1) {
      xoxo(coorx[3],coory[3],play);
      board[1][0]=play;
      count++;
      play^=0x1;
     }
     break;
    case '5':
     if(board[1][1]!=0&&board[1][1]!=1) {
      xoxo(coorx[4],coory[4],play);
      board[1][1]=play;
      count++;
      play^=0x1;
     }
     break;
    case '6':
     if(board[1][2]!=0&&board[1][2]!=1) {
      xoxo(coorx[5],coory[5],play);
      board[1][2]=play;
      count++;
      play^=0x1;
     }
     break;
    case '7':
     if(board[0][0]!=0&&board[0][0]!=1) {
      xoxo(coorx[6],coory[6],play);
      board[0][0]=play;
      count++;
      play^=0x1;
     }
     break;
    case '8':
     if(board[0][1]!=0&&board[0][1]!=1) {
      xoxo(coorx[7],coory[7],play);
      board[0][1]=play;
      count++;
      play^=0x1;
     }
     break;
    case '9':
     if(board[0][2]!=0&&board[0][2]!=1) {
      xoxo(coorx[8],coory[8],play);
      board[0][2]=play;
      count++;
      play^=0x1;
     }
     break;
    case 'q':
     goto end;
    case 'r':
     goto start;
    case 'x':
     owin=0;
     xwin=0;
     goto start;
    default:
     break;
   }
  }
  winner=analyse();
  if(winner>=0&&winner!=2){
   if(winner==0){
    setcolor(YELLOW);
    outtextxy(midx,10,"The winner is O.");
    owin++;
    setcolor(WHITE);
    goto getcharacter;
   }
   else if (winner==1) {
    setcolor(GREEN);
    outtextxy(midx,10,"The winner is X.");
    xwin++;
    setcolor(WHITE);
    goto getcharacter;
   }
  }
  if(count>9) {
   setcolor(RED);
   outtextxy(midx,10,"The game's a draw.");
   setcolor(WHITE);
   goto getcharacter;
  }
  delay(500);
  clearviewport();
 }
 end:
 getch();
 closegraph();
 return 0;
}


int analyse() {
 int i,j;
 for(i=0;i<3;i++){
  if(checkr(i))
   continue;
  if(board[i][0]==board[i][1]&&board[i][0]==board[i][2]){
   return board[i][0];
  }
 }
 for(i=0;i<3;i++){
  if(checkc(i))
   continue;
  if(board[0][i]==board[1][i]&&board[0][i]==board[2][i]){
   return board[0][i];
  }
 }
 if(board[0][0]==board[1][1]&&board[0][0]==board[2][2]) {
  if(board[0][0]==2&&board[1][1]==2&&board[2][2]==2)
   return 2;
  else
   return board[0][0];
 }
 if(board[0][2]==board[1][1]&&board[0][2]==board[2][0]) {
  if(board[0][2]==2&&board[1][1]==2&&board[2][0]==2)
   return 2;
  else
   return board[0][2];
 }
 return -1;
}

void xoxo(int x, int y,int p) {
 if(p==0) {
  setcolor(YELLOW);
  circle(x,y,r);
 }
 else {
  setcolor(GREEN);
  line(x-r,y-r,x+r,y+r);
  line(x+r,y-r,x-r,y+r);
 }
 setcolor(WHITE);
}

void drawboard() {
 int i, j, x;
 char cha[10];
 setcolor(GREEN);
 outtextxy(1,10,"X wins:");
 itoa(xwin,cha,10);
 outtextxy(110,10,cha);
 setcolor(YELLOW);
 outtextxy(1,30,"O wins:");
 itoa(owin,cha,10);
 outtextxy(110,30,cha);
 setcolor(WHITE);
 outtextxy(1,50,"Press r to restart, q to quit.");
 line(midx-cellspc/2,midy-1.5*cellspc,midx-cellspc/2,midy+1.5*cellspc);
 line(midx+cellspc/2,midy-1.5*cellspc,midx+cellspc/2,midy+1.5*cellspc);
 line(midx-1.5*cellspc,midy-cellspc/2,midx+1.5*cellspc,midy-cellspc/2);
 line(midx-1.5*cellspc,midy+cellspc/2,midx+1.5*cellspc,midy+cellspc/2);
 for(x=0,j=2;j>=0;j--) {
  for(i=0;i<3;i++,x++) {
   if(board[j][i]!=2) {
//    outtext(" ");
//    itoa(x,cha,10);
//    outtext(cha);
//    outtext(",");
//    itoa(coorx[x],cha,10);
//    outtext(cha);
//    outtext(",");
//    itoa(coory[x],cha,10);
//    outtext(cha);
    xoxo(coorx[x],coory[x],board[j][i]);
   }
  }
 }
}

int checkr(int i) {
 if((board[i][0]==2)&&(board[i][1]==2)&&(board[i][2]==2))
  return 1;
 else
  return 0;
}

int checkc(int i) {
 if((board[0][i]==2)&&(board[1][i]==2)&&(board[2][i]==2))
  return 1;
 else
  return 0;
}

int getcoor(int i, int j) {
 for(x=0,j=2;j>=0;j--) {
  for(i=0;i<3;i++,x++) {
   if(board[j][i]!=2) {
    return x;
   }
  }
 }
}

Fruits in the basket Turbo C game

/*

    Fruits in the basket is a fun and addicting game. Catch falling

    fruits and score points.

    Copyright (C) 2013 Abhishek Baddi

    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 3 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, see http://www.gnu.org/licenses/.

*/


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80
#define ENTER 13

int r=20, coorx[9], x, midx, midy, cellspc=50;

int score=0, miss=0, speed=10;

char cha[8];

void drawboard(struct basket );
void drawfruit(struct fruit * );

int fruity;

struct fruit {
 int x, y;
};

struct basket {
 int x, y, rad;
};

int main() {
 int gd=DETECT, gm, i;
 char ch;
 struct basket one;
 struct fruit f1;
 randomize();
 initgraph(&gd, &gm, "C:\\TC\\BGI");
 midx=getmaxx()/2;
 midy=getmaxy()/2;
 one.y=getmaxy()-40;
 coorx[0]=170-100;
 coorx[1]=220-100;
 coorx[2]=270-100;
 coorx[3]=320-100;
 coorx[4]=370-100;
 coorx[5]=420-100;
 coorx[6]=470-100;
 one.x=coorx[random(7)];
 one.rad=20;
 f1.x=coorx[random(7)];
 f1.y=0;
 settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
 outtext("Welcome to ");
 setcolor(MAGENTA);
 outtext("F");
 setcolor(GREEN);
 outtext("r");
 setcolor(RED);
 outtext("u");
 setcolor(YELLOW);
 outtext("i");
 setcolor(CYAN);
 outtext("t");
 setcolor(BROWN);
 outtext("s");
 setcolor(WHITE);
 outtext(" in the basket.");
 outtextxy(0,40,"Use \'a\' & \'d\' or the arrow keys to move");
 outtextxy(0,80,"the basket.");
 outtextxy(0,120,"Press \'q\' to quit anytime.");
 getch();

 while(1) {
  if(kbhit()) {
   ch=getch();
   switch(ch) {
    case 72:
     //up arrow pressed

     break;

    case 75:
     //left arrow pressed
     one.x-=cellspc;
     if(one.x<coorx[0])
      one.x+=cellspc;
     break;
    case 77:
     //right arrow pressed
     one.x+=cellspc;
     if(one.x>coorx[6])
      one.x-=cellspc;
     break;
    case 80:
     //down arrow pressed

     break;


case 'a':

     //'a' = left arrow pressed


one.x-=cellspc;
     if(one.x<coorx[0])
      one.x+=cellspc;
     break;
    case 'd':

     //'d' = right arrow pressed


one.x+=cellspc;
     if(one.x>coorx[6])
      one.x-=cellspc;
     break;
    case '\r':
     goto end;
    case 'q':
     goto end;
    default:
     break;
   }
  }
  delay(50);
  drawboard(one);
  drawfruit(&f1);

  if(((f1.y+50)>=one.y)&&(f1.x==one.x)) {
   ++score;
   f1.x=coorx[random(7)];
   f1.y=0;
  }
  if(f1.y>one.y) {
   ++miss;
   f1.x=coorx[random(7)];
   f1.y=0;
  }
  speed=0.2*score+10;
  if(speed<10)
   speed=10;
  if(speed>30)
   speed=30;
  if(miss>10) {
   end:
   clearviewport();
   setcolor(WHITE);
   itoa(score,cha,10);
   outtextxy(2,10,"Your score is:");
   setcolor(GREEN);
   outtextxy(2,40,cha);
   getch();
   return 0;
  }
 }
}

void drawboard(struct basket one) {
 char st[8];
 clearviewport();
 setcolor(WHITE);
 line(440,0,440,480);
 itoa(miss,st,10);
 outtextxy(450,20,"Your misses are:");
 setcolor(RED);
 outtextxy(450,50,st);
 itoa(score,st,10);
 setcolor(WHITE);
 outtextxy(450,80,"Your score is:");
 setcolor(GREEN);
 outtextxy(450,110,st);
 setcolor(YELLOW);
 setfillstyle(XHATCH_FILL, YELLOW);
 pieslice(one.x, one.y, 180, 360, one.rad);
 setcolor(WHITE);
 itoa(one.x,st,10);
 outtextxy(550,130,"x=");
 outtextxy(565,130,st);
 itoa(one.y,st,10);
 outtextxy(550,150,"y=");
 outtextxy(565,150,st);
}

void drawfruit(struct fruit *f) {
 char st[8];
 setcolor(RED);
 setfillstyle(SOLID_FILL, RED);
 pieslice(f->x, f->y, 0, 360, r-2);
 f->y+=speed;
 setcolor(WHITE);
 itoa(f->x,st,10);
 outtextxy(450,130,"x=");
 outtextxy(465,130,st);
 itoa(f->y,st,10);
 outtextxy(450,150,"y=");
 outtextxy(465,150,st);

}