Link to home
Start Free TrialLog in
Avatar of carceri
carceri

asked on

Writing data in EXE files

I want to store an encrypted password inside my program's EXE file. I also need to be able to change it from my program. How do I do that ?

Thanks in advance
Avatar of inter
inter
Flag of Türkiye image

How long does your password?
igor
Any way,

//**************************************************************
type
  TReserved = packed record
    pad : array[0..9]of byte;
  end;
  // This is old dos header
  TDosHeader = packed record
    FS : WORD;
    BOL: WORD;
    OTHER    : array[0..17] of WORD;
    res : TReserved;
    //forget the rest
  end;
//
// F is filename
// D is data you want to store in it (MAX 10) for this method
//
procedure ReadPassWord(const F : string;var Pass : string);
var
  T : TDosHeader;
  S : TFileStream;
begin
  // try opening in different modes
  try
   S := TFileStream.Create(F, fmOpenRead or fmShareDenyNode);
  except
   S := TFileStream.Create(F, fmOpenRead or fmShareCompat);
  end;
  //read dos header that we gonna modify
  try
    S.Read(T,sizeof(T));
    SetLength(Pass ,10);
    move(Pass[1], T.Res.Pad);
  finally
   S.Free;
  end;
end;

procedure WritePassWord(const F : string;const Pass : string);
var
  T : TDosHeader;
  S : TFileStream;
begin
  try
   S := TFileStream.Create(F, fmOpenWrite or fmShareDenyNode);
  except
   S := TFileStream.Create(F, fmOpenWrite or fmShareCompat);
  end;
  try
   S.Seek(0,0);
   // write what you want in T.Res.pas (it is 10 bytes long)
   move(T.Res.pas, Pass[1], Length(Pass));
   S.Write(T, Sizeof(T));
   // Done!
  finally
   S.Free;
  end;
end;
//**************************************************************
The other way that can store any amount of data it as follows:

- The file structure should will be
[original exe][signiture][your data][length of your data(DWORD)]
so if you want to store 'hello world' and your signiture-which we
can detect that file is modified by ourselves- is '@@@@' the file
would be('[' and ']' denotes the segments they are not in image)
[original exe][@@@@][hello world][0B000000]
(note that 11 = $0000000B in little indian = $0B000000)

SO:


//**************************************************************
uses
  SysUtils, ...etc;

function IsMyFile(F : string):boolean;
var
  S   : TFileStream;
  Len : DWord;
  Sign: array[0..3] of byte;
const
  DefSign : array[0..3] of char = "@@@@";
begin
  Result := false;
  // try opening in different modes
  try
   S := TFileStream.Create(F, fmOpenRead or fmShareDenyNode);
  except
   S := TFileStream.Create(F, fmOpenRead or fmShareCompat);
  end;
  try
    //go to end
    S.Seek( 4, soFromEnd);
    //read our length(as if file is ours)
    S.Read( Len, sizeof(Len));
    //seek to signiture from back
    S.Seek(4 + Len + 4, soFromEnd);
    // read signiture and compare
    S.Read(Sign[0], SizeOf(Sign[0]));
    // compare memories and set result
    Result := CompareMem(@Sign[0], @DefSign[0], SizeOf(Sign));
  finally
   S.Free;
  end;
end;

// reads password if no password set returns empty string
function ReadPassword(F : string):string;
var
  S   : TFileStream;
  Len : DWord;
  Sign: array[0..3] of byte;
const
  DefSign : array[0..3] of char = "@@@@";
begin
  if not IsMyFile then EXIT; // no password ****************
  Result := '';
  // try opening in different modes
  try
   S := TFileStream.Create(F, fmOpenRead or fmShareDenyNode);
  except
   S := TFileStream.Create(F, fmOpenRead or fmShareCompat);
  end;
  try
    //go to end
    S.Seek( 4, soFromEnd);
    //read our length(as if file is ours)
    S.Read( Len, sizeof(Len));
    //seek to it from back
    S.Seek(4 + Len, soFromEnd);
    // size return value accordingly
    SetLength(Result, Len);
    // read signiture and compare
    S.Read(Result[1], Len);
  finally
   S.Free;
  end;
end;

// writes the password
procedure WritePassword(F : string; const Pass:string);
var
  S   : TFileStream;
  Len : DWord;
  MyFile : Boolean;
const
  DefSign : array[0..3] of char = "@@@@";
begin
  MyFile := IsMyFile;
  // try opening in different modes
  try
   S := TFileStream.Create(F, fmOpenReadWrite or fmShareDenyNode);
  except
   S := TFileStream.Create(F, fmOpenReadWrite or fmShareCompat);
  end;
  try
    if MyFile Then
    begin
      //go to end
      S.Seek( 4, soFromEnd);
      //read our old length
      S.Read( Len, sizeof(Len));
      //seek to the begining of the old password
      S.Seek(4 + Len, soFromEnd);
      // write new password
      Len := Length(Pass);
      S.Write( Pass[1], Len);
      // write lenghth
      S.Write( Len, sizeof(Len));
    end
    else begin
      //go to end
      S.Seek( 0, soFromEnd);
      // write signiture
      S.Write( DefSign[0], SizeOf(DefSign))
      // write password
      Len := Length(Pass);
      S.Write( Pass[1], Len);
      // write lenghth
      S.Write( Len, sizeof(Len));
    end;
  finally
   S.Free;
  end;
end;
//**************************************************************


May this helps...igor
Avatar of Holger101497
Holger101497

Are you sure this will work?
I thought Windows didn't allow you to modify an EXE-file that is in use....
which would mean you cannot modify the file to change the password!
Avatar of carceri

ASKER

Thanks, I'll see if this works and then I'll come back
I have do such things in NT, if you can find the correct mode to open the file it works.
Avatar of carceri

ASKER

Windows didn't allow me to modify this file since it's in use
It is wise for one to accept if he/she is wrong. Yes I am WRONG, we can not modify exe only able to read it (because it is opened by operation system as fmShareDenyWrite). So why do not you store your encrypted password in registry? If it is encrypted I think it is no problem...
Avatar of carceri

ASKER

Yes, that's a possibility, but I want to make sure that if people copy the program, they'll get the password too. I know that there aren't any documented ways of doing so, but I also know that a lot of programs are doing it
Why not have the password-change-program as a separate EXE, then you can modify the original EXE, along as it is closed.
Avatar of carceri

ASKER

Hmmm. That could be a solution, but now I'm curious... how do you write to the programs exe file from the program itsel ?

Thanks d_kieman
Hi, I think d_kieman talks about modifying our main exe from another program while it is not running...On that case, my procedures above works correctly, just give the filename as the filename of our main exe. So you have two programs, one is your main program call it main.exe and the other is password writer; call it passwrite.exe so when you run passwrite.exe it writes the password to the main.exe as above. Then when your program runs it can read password from itself.
However I can not understand why you want to write the password in your exe. If you do it for copy protection, moving your file to another machine must ask new password because it is coppied which is the case you do not want to allow.
So do you use your password for copy protection?
igor
Avatar of carceri

ASKER

No, the password is not used for copy protection, but to protect certain features in the program, that even people who "steals" the program (it must run om computers in a public library) can't have access to.

Of cource the idea with two programs would work here, but if it's possible to keep it in one EXE that would be prefered
ASKER CERTIFIED SOLUTION
Avatar of zac
zac

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial