Dynamic Password - Logic

Authentication is very much important if you are programming Encryption or security tools. Generally we all apply a common logic to authenticate the user. Following are the very well known code :-

C code :-

char password[100];
printf("\n\n\tEnter your Password :: ");
gets(password);
if(strcpy(password,"secretpassword")==0)
{
printf("\n\n\tWelcome..");
/*------- Next applicatuon code ---------- */
}
else
{
printf("\n\n\tIntruder suspected...");
exit(0); // Application aborted

C# code :-
String password;
Console.Write("\n\n\tEnter Password :: ");
password=Console.Readline();
if(password=="secretpassword")
{
Console.Write("\n\n\tWelcome..");
/*------- Next applicatuon code ---------- */
}
else
{
Console.Write("\n\n\tIntruder suspected..");
/* appliction abortion code or as specified.. */
Image source from internet

But it has a major disadvantage. This code won't help you to change the password. Once you code the application there would be no way to modify by the assembly code in ordinary manner. You can imagine a scenario where one of your friend has seen the password of the application, thereafter there's no way to change it. Either you have to write another program or you have to take that friend under your confidence :P (..this seems to be ridiculous :D :D..)

Storing Password in some file seems the solution but it also carries some problems. Here the application becomes dependent on that file and would be worthless if the file gets deleted.

But there's way by which you can store the password in the application itself. This is like playing with the executable code, where we'll store the password in the executable in such a way that it doesn't crashes it.

The Logic is simple. It includes following steps :-
  • Scan the Password
  • Create a copy/dummy of self
  • Encrypt the password and append it in that copy. Try to append it somewhere at the beginning of the copy. Be sure about the place where you append, else it can crash the EXE
  • Rename self with other name.
  • Rename the dummy to the application name
  • Self-Destruct the self
Similary to scan the password. It includes following step :-
  • Create a copy/dummy of self
  • Read the password from the dummy
  • Delete the dummy
  • Decrypt the password to get the actual password


Following is the deployment of the code in C. It's tested in Borland C++ 5.02

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


char content[100];
void encryptstr(char *str,int key)
{

/* Customize your own or built-in encryption code */

long i=0;
char c;
while((c=str[i])!=NULL)
{
content[i]=c+key;
i++;
}
content[i]=NULL;
}

void decryptstr(char *str,int key)
{
/* Customize your own or built-in decryption code */

long i=0;
char c;
while((c=str[i])!=NULL)
{
content[i]=c-key;
i++;
}
content[i]=NULL;
}
void SelfDestruct(char *appname)
{

/* Customize your self-destruction code */

FILE *f;
f=fopen("Selfdestruct.bat","w");
fprintf(f,"@echo off\nping -n 1 0 >NUL\n\n\n\n");

fprintf(f,"del %s\ncls\n",appname);



fprintf(f,"echo rjprozrjprozrjprozrjproz ");
fprintf(f," rjprozrjprozrjprozrjpro");
fprintf(f,"zrjproz\necho rjprozrjprozrjpro");
fprintf(f,"zrjproz rjprozrjprozrjp");
fprintf(f,"rozrjprozrjproz\necho rjproz ");
fprintf(f," rjproz ");
fprintf(f," rjproz\necho rjproz ");
fprintf(f," rjproz r");
fprintf(f,"jproz \necho rjproz ");
fprintf(f," rjproz rj");
fprintf(f,"proz\necho rjproz rj");
fprintf(f,"proz rjproz\n");
fprintf(f,"echo rjproz rjproz ");
fprintf(f," rjproz\necho ");
fprintf(f,"rjproz rjproz ");
fprintf(f," rjproz\necho rjpro");
fprintf(f,"z rjproz ");
fprintf(f," rjproz\necho rjprozrjpr");
fprintf(f,"ozrjprozrjproz ");
fprintf(f," rjproz\necho rjprozrjprozrjp");
fprintf(f,"rozrjproz rj");
fprintf(f,"proz\necho rjprozrjproz ");
fprintf(f," rjproz\n");
fprintf(f,"echo rjproz rjproz ");
fprintf(f," rjproz\necho ");
fprintf(f,"rjproz rjproz ");
fprintf(f," rjproz\necho rjpro");
fprintf(f,"z rjproz ");
fprintf(f," rjproz\necho rjproz ");
fprintf(f," rjproz ");
fprintf(f," rjproz\necho rjproz rjp");
fprintf(f,"roz rj");
fprintf(f,"proz\necho rjproz rjproz ");
fprintf(f," rjproz\n");
fprintf(f,"echo rjproz rjproz ");
fprintf(f," rjproz\necho ");
fprintf(f,"rjproz rjproz ");
fprintf(f," rjproz\necho rjpro");
fprintf(f,"z rjproz rjpr");
fprintf(f,"oz rjproz\necho rjproz ");
fprintf(f," rjproz rjproz ");
fprintf(f," rjproz\necho rjproz ");
fprintf(f," rjproz rjprozrjprozrj");
fprintf(f,"proz\necho rjproz r");
fprintf(f,"jproz rjprozrjprozrjproz ");
fprintf(f," \n\n\n\n\nping -n 3 0 ");
fprintf(f,">NUL\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
fprintf(f,"\n\n\n\necho .\necho .\necho .\necho .\necho .\necho .\necho .\necho ");
fprintf(f,".\necho .\necho .\necho .\n\necho S");
fprintf(f,"elf destructed successfully...");
fprintf(f,"......\necho .\necho .\necho .\nec");
fprintf(f,"ho .\necho .\necho .\necho .\necho .\necho ");


fprintf(f,"Now terminate this program.......");

fprintf(f,"\necho .\necho .\necho .\necho .\ne");
fprintf(f,"cho .\necho .\nping -n 2 0 >NUL\n");
fprintf(f,"del selfdestruct.bat");



fclose(f);

system("start \"SElf Destruction\" selfdestruct.bat");
exit(0);
}
void main(int arc,char *argv[])
{


printf("\n\n\tEnter 1 to change password\n\n\tElse press any other key to view password\n\n\tEnter your choice ::");
char pass[100],ck[100];
int i=0;
FILE *f;
char c=getch();
clrscr();
if(c=='1')
{
// Change pass

printf("\n\n\tEnter new password:: ");
gets(pass);
encryptstr(pass,9);
rename(argv[0],"todie.rj_proz_trans"); // argv[0] stores the full path of self
system("copy todie.rj_proz_trans newapp.rj_proz_trans");
f=fopen("newapp.rj_proz_trans","rb+") ;
fseek(f,25L,0); // This step is very important.. choosing right place to append the password

fprintf(f,"%s ",content);
fclose(f);
rename("newapp.rj_proz_trans",argv[0]);
SelfDestruct("todie.rj_proz_trans");
}
else
{
rename(argv[0],"doscomp.rj_proz_trans");
system("copy doscomp.rj_proz_trans dupli.rj_proz_ref");
rename("doscomp.rj_proz_trans",argv[0]);
clrscr();
f=fopen("dupli.rj_proz_ref","rb+") ;
fseek(f,25L,0);
while((c=getc(f))!=' ')
{
ck[i]=c;
i++;
}
ck[i]=NULL;
decryptstr(ck,9);
printf("\n\n\t\tPassword is :: %s .",content);
getch();
fclose(f);
remove("dupli.rj_proz_ref");

}



Download this to see a demo application..

It is recommended to use one-way encryption like MD5 before storing the password.
If you are using dos commands to copy files in the code then don't use spaces in the application name, i.e. you can name it as "dynamic_pass.exe" but not "dynamic pass.exe".

Depending upon the compiler the right place to append the password may differ. You have to spent little time to study that with your own compiler. Keep that in mind that long password can crash the EXE as it can overwrite the EXE code :P

Comments

Popular Posts