Editing resource of executable

classic Classic list List threaded Threaded
24 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Editing resource of executable

Krzysztof
Hi,

I have some problem. Example:

I created some exec using free pascal and lazarus. It is placed on
http server. User in webbrowser click download, and server should
attach link from where it is clicked into this exec. So when user run
downloaded exec, this exec can read this link. The idea is that
everything is in one file (and must be). My question is, exists any
commandline tool which can edit executable file for edit some resource
(like ResEdit or Restorator for Windows) which can be used by http
server? By resource I mean some const (or resourcestring section, free
pascal binary can replace it in runtime, can some tool do this same
outside?). So main problem is in server side. Finally we can use free
pascal compiler and compile modified source by server, but this
solution we use when everything fails (admins, restrictions etc.). I
need this only for windows (for now), so I'm considering some
commandline installer makers like InnoSetup or something. Any idea?

Regards
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Editing resource of executable

Mark Morgan Lloyd-5
Krzysztof wrote:

> Hi,
>
> I have some problem. Example:
>
> I created some exec using free pascal and lazarus. It is placed on
> http server. User in webbrowser click download, and server should
> attach link from where it is clicked into this exec. So when user run
> downloaded exec, this exec can read this link. The idea is that
> everything is in one file (and must be). My question is, exists any
> commandline tool which can edit executable file for edit some resource
> (like ResEdit or Restorator for Windows) which can be used by http
> server? By resource I mean some const (or resourcestring section, free
> pascal binary can replace it in runtime, can some tool do this same
> outside?). So main problem is in server side. Finally we can use free
> pascal compiler and compile modified source by server, but this
> solution we use when everything fails (admins, restrictions etc.). I
> need this only for windows (for now), so I'm considering some
> commandline installer makers like InnoSetup or something. Any idea?

Let's see if we can get this straight. What you're basically looking for
is something which can take one or more of the environment variables (or
equivalent) that the web server knows about, and use it to "brand" the
executable.

This is probably going to be very much specific to the version of
Windows that the client is running: it used to be possible to simply
append data to an executable since there was an unambiguous file length
in the header, but this obviously won't work if the file is checksummed
or signed.

Unless you're doing it by re-linking the program when sending it to the
client, this is probably outside the remit of FPC and you'd be better
off looking for non-specific tools.

In any case, you've obviously got the problem of copying the unbranded
binary to a working area before manipulating it, unless you can
serialise the CGI or whatever you're using.

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Editing resource of executable

Reinier Olislagers
On 30-8-2012 10:07, Mark Morgan Lloyd wrote:

> Krzysztof wrote:
> everything is in one file (and must be). My question is, exists any
>> commandline tool which can edit executable file for edit some resource
>> (like ResEdit or Restorator for Windows) which can be used by http
>> server?
>
> Let's see if we can get this straight. What you're basically looking for
> is something which can take one or more of the environment variables (or
> equivalent) that the web server knows about, and use it to "brand" the
> executable.
>
> This is probably going to be very much specific to the version of
> Windows that the client is running: it used to be possible to simply
> append data to an executable since there was an unambiguous file length
> in the header, but this obviously won't work if the file is checksummed
> or signed.
For my CheckRide remote control/end user screen sharing program, I
wanted to be able to let admins create a personalized copy that connects
to their own server, so I had the same problem.

Although FPC does provide functions to modify resources on Windows, I
didn't have much luck with it. Perhaps the situation has improved since
I last tried, it - haven't looked.

I turned to what Mark suggested: just slapping on some (zipped) extra
data after the exe. And yes, this messes with signing the executable etc.

If you want to investigate this path, have a look at
https://bitbucket.org/reiniero/checkride/src
Source
CheckRideResourceZipper.lpi (which is used to create the "unbranded",
standard resources in the build process)
CheckRideHelper.lpi (which demonstrates stamping/branding the resources)
CheckRide.lpi (which demonstrates unpacking and using the resources)
they all use the poormansresource.pas file
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Editing resource of executable

Krzysztof
Maybe I can write own commandline tool for server admins, which can
edit resource of another (written in FPC too) executable?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Editing resource of executable

Jonas Maebe-2

Krzysztof wrote on Thu, 30 Aug 2012:

> Maybe I can write own commandline tool for server admins, which can
> edit resource of another (written in FPC too) executable?

I think the easiest is to add the data at the end of the binary, as  
has been suggested earlier. It breaks signed applications, but so does  
changing a resource, so there's no difference as far as that is  
concerned. And adding/reading data at the end of the binary is much  
easier than changing a resource.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Editing resource of executable

Krzysztof
Hmm sounds good. So I can normally open another exec in for example
TFileStream and write something at the end and this exec run without
error?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Editing resource of executable

Jonas Maebe-2

Krzysztof wrote on Thu, 30 Aug 2012:

> Hmm sounds good. So I can normally open another exec in for example
> TFileStream and write something at the end and this exec run without
> error?

Yes. Do keep in mind that the original exe has to be able to find the  
length of the data you appended to it. So it's easiest if you add a 4  
byte length or so after the appended data, which the original exe then  
can read to find the start of that data.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Editing resource of executable

Reinier Olislagers
On 30-8-2012 16:27, Jonas Maebe wrote:

>
> Krzysztof wrote on Thu, 30 Aug 2012:
>
>> Hmm sounds good. So I can normally open another exec in for example
>> TFileStream and write something at the end and this exec run without
>> error?
>
> Yes. Do keep in mind that the original exe has to be able to find the
> length of the data you appended to it. So it's easiest if you add a 4
> byte length or so after the appended data, which the original exe then
> can read to find the start of that data.

... or just use my readymade code that does exactly that instead of
writing your own ;) It's released under a very liberal MIT license (so
compatible with closed-source development, (L)GPL etc)

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Editing resource of executable

Krzysztof
Thanks to all!
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Editing resource of executable

wkitty42
In reply to this post by Krzysztof
On 8/29/2012 17:46, Krzysztof wrote:

> Hi,
>
> I have some problem. Example:
>
> I created some exec using free pascal and lazarus. It is placed on
> http server. User in webbrowser click download, and server should
> attach link from where it is clicked into this exec. So when user run
> downloaded exec, this exec can read this link. The idea is that
> everything is in one file (and must be). My question is, exists any
> commandline tool which can edit executable file for edit some resource
> (like ResEdit or Restorator for Windows) which can be used by http
> server? By resource I mean some const (or resourcestring section, free
> pascal binary can replace it in runtime, can some tool do this same
> outside?). So main problem is in server side. Finally we can use free
> pascal compiler and compile modified source by server, but this
> solution we use when everything fails (admins, restrictions etc.). I
> need this only for windows (for now), so I'm considering some
> commandline installer makers like InnoSetup or something. Any idea?

i think i know what you are asking for and my thought actually matches what mark
loyd seemed to be saying... with that in mind, i remember reading about some
sort of resource editor to allow editing of resources in a binary... it was
posted in this area back in jan 2012... i don't know if this is what you are
looking for but here's what i found...

[QUOTE]
Date: Mon, 30 Jan 2012 09:09:42 +0100
From: Reinier Olislagers <[hidden email]>
To: FPC Mailing list <[hidden email]>,
        Lazarus mailing list <[hidden email]>
Subject: [fpc-pascal] Poor man's resource code available and CheckRide update:
now with editor

Hi all,

(Cross posted to Lazarus+FPC lists)

Thanks to UPayload (http://www.delphidabbler.com/articles?article=7) and some
help on the forum, I could implement an alternative way of storing files in an
executable file (basically it just appends them with a footer). See source:
poormansresource.pas in https://bitbucket.org/reiniero/checkride/src/, and the
CheckRideResourceZipper project for sample code

This method allows me to edit such a "resource" using an FPC/Lazarus program. As
- I think - DoDi predicted, I had a lot of trouble trying to work with regular
Windows resources, but whether that is due to bugs in FPC, Windows or me, I
don't know ;)

I updated my CheckRide remote control package with this functionality: a helper
can edit CheckRide.exe with his hostname and port number and distribute that
single exe to his clients/helped persons. This allows 1 click operation at the
helped side.

I also tested operation with a Linux helper running stunnel+vncviewer and a
WIndows helped party running CheckRide.exe

See the site mentioned above.

Comments, patches, as well as criticism welcome ;)

Thanks,
Reinier
[/QUOTE]

if i'm reading the above correctly, it would appear that it can possibly do what
you are looking for... maybe...
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Editing resource of executable

Reinier Olislagers
On 31-8-2012 17:48, waldo kitty wrote:
> On 8/29/2012 17:46, Krzysztof wrote:
>> Hi,
>>
>> I have some problem. Example:
>>
<snip>

>
> i think i know what you are asking for and my thought actually matches
> what mark loyd seemed to be saying... with that in mind, i remember
> reading about some sort of resource editor to allow editing of resources
> in a binary... it was posted in this area back in jan 2012... i don't
> know if this is what you are looking for but here's what i found...
>
> [QUOTE]
> Date: Mon, 30 Jan 2012 09:09:42 +0100
> From: Reinier Olislagers
> <[hidden email]>
<snip>
> Subject: [fpc-pascal] Poor man's resource code available and CheckRide
> update: now with editor
>
> Hi all,
>
> (Cross posted to Lazarus+FPC lists)
>
> Thanks to UPayload (http://www.delphidabbler.com/articles?article=7) and
> some help on the forum, I could implement an alternative way of storing
<snip>
> See source: poormansresource.pas in https://bitbucket.org/reiniero/checkride/src/, and the CheckRideResourceZipper project for sample code
<snip>
> Thanks,
> Reinier
> [/QUOTE]
>
> if i'm reading the above correctly, it would appear that it can possibly
> do what you are looking for... maybe...
I'd say it can definitely do what he's asking for - but I'm biased: you
did notice the guy posting that message is me? And that I again posted
the link to my CheckRide code project in this thread? ;)

Thanks for looking up that post and kudos to your memory of ancient threads.

Regards,
Reinier
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Editing resource of executable

wkitty42
On 8/31/2012 11:55, Reinier Olislagers wrote:
> On 31-8-2012 17:48, waldo kitty wrote:
>> if i'm reading the above correctly, it would appear that it can possibly
>> do what you are looking for... maybe...
> I'd say it can definitely do what he's asking for - but I'm biased:

as well you should be :)

> you did notice the guy posting that message is me? And that I again posted
> the link to my CheckRide code project in this thread? ;)

yup... i've been jumping back and forth thru several things while catching up on
my emails...

> Thanks for looking up that post and kudos to your memory of ancient threads.

you are welcome and thanks :)

your original posting and Krzysztof's reminded me so much of what i use in some
of my TP6 stuff... but what i use is slightly different in that it uses typed
constants and modifies them in the executable during execution...

one purpose of this would be to limit the execution of a trial program to only X
executions or that it will only run for Y days after it is first executed or
even a combination of those such that only X executions are allowed within Y
days and if either is reached, then the program stops...

i also used this technique to store registration data and options settings
directly in the executable instead of having a separate and external
configuration file...

i can post those old sources if anyone is interested... they only need ask...
the sources are dated May 1993 and i likely saved them from the Fidonet PASCAL
echo (message area)... they might have even ended up in the SWAG archives but i
don't know ;) -=B-)

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Editing resource of executable

José Mejuto
In reply to this post by wkitty42
El 31/08/2012 17:48, waldo kitty escribió:

> i think i know what you are asking for and my thought actually matches
> what mark loyd seemed to be saying... with that in mind, i remember
> reading about some sort of resource editor to allow editing of resources
> in a binary... it was posted in this area back in jan 2012... i don't
> know if this is what you are looking for but here's what i found...

Hello,

If you need to access dynamic data in a single file, maybe my code may
help you. Basically it is a filesystem that can be embedded in a single
file, so you can create, delete, open, read, write and seek any
individual file inside. The "problem" is that in write mode only a test
filesystem (not very usefull), Excel filesystem and ZIP filesystem are
supported. The ZIP one is cool but to rebuild the final ZIP once updated
it could need some temp files as it can not be edited in place. Maybe
the BCF (Binary Coumpound File, the one that Excel uses) is a good
choice as it has quite small initial tables (like a format) and grow as
more data is being added.

The virtual filesystem can be mounted over an encrypted TFileStream
derivate to allow transparent encryption of the whole virtual
filesystem. It can read also raw images in FAT16 and FAT32 but can not
write them.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Editing resource of executable

Mark Morgan Lloyd-5
In reply to this post by wkitty42
waldo kitty wrote:

> i also used this technique to store registration data and options
> settings directly in the executable instead of having a separate and
> external configuration file...
>
> i can post those old sources if anyone is interested... they only need
> ask... the sources are dated May 1993 and i likely saved them from the
> Fidonet PASCAL echo (message area)... they might have even ended up in
> the SWAG archives but i don't know ;) -=B-)

I did this sort of thing back in the days of MZ files, where there was a
simple length field in the header. I believe that NE files similarly had
an accessible overall length, but they also had a checksum field even
though this was rarely (if ever) used. Later Windows formats (PE etc.)
might use signing/blessing/branding/checksumming to varying extents, and
I also think there was discussion of unix-style signing in this (or a
related) list a few weeks ago.

Which leaves me thinking that the safest way of doing it would be to
look for an "official" program, i.e. from MS or from unix binutils,
which- if a program was already signed- might request some sort of key
before it would change anything.

Of course, an even safer way would be to leave the executable alone and
to put an early check in the startup code that a subsidiary key file
existed, and for that key to include something that identified the
machine or site on which the program was entitled to run.

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Editing resource of executable

Graeme Geldenhuys-3
On 31/08/12 21:18, Mark Morgan Lloyd wrote:
> Of course, an even safer way would be to leave the executable alone and
> to put an early check in the startup code that a subsidiary key file
> existed, and for that key to include something that identified the
> machine or site on which the program was entitled to run.

Take a look at OnGaurd. It has many ways of doing this. We use it in our
commercial software (linux and windows) to lease our software on a
yearly basis, or to enabled a 1 month trial etc. You can even lock your
application down to a specific PC.

   https://github.com/graemeg/onguard


   Graeme.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Editing resource of executable

wkitty42
In reply to this post by Mark Morgan Lloyd-5
On 8/31/2012 16:18, Mark Morgan Lloyd wrote:

> waldo kitty wrote:
>
>> i also used this technique to store registration data and options settings
>> directly in the executable instead of having a separate and external
>> configuration file...
>>
>> i can post those old sources if anyone is interested... they only need ask...
>> the sources are dated May 1993 and i likely saved them from the Fidonet PASCAL
>> echo (message area)... they might have even ended up in the SWAG archives but
>> i don't know ;) -=B-)
>
> I did this sort of thing back in the days of MZ files, where there was a simple
> length field in the header.

and that's one of the thing this main routine uses to jump to the end real
quickly... but there's also a set of starting and ending markers... plus the
fact that the data is a "Typed Constant" is another factor... i don't recall for
sure, but i think i've been told that FPC doesn't have typed constants???

> I believe that NE files similarly had an accessible overall length, but they
> also had a checksum field even though this was rarely (if ever) used.

interesting... this routine has a checksum item built into it... IIRC, it was so
we could detect if the executable had been modified and abort operation if true...

> Later Windows formats (PE etc.) might use signing/blessing/branding/checksumming
> to varying extents, and I also think there was discussion of unix-style signing
> in this (or a related) list a few weeks ago.
>
> Which leaves me thinking that the safest way of doing it would be to look for an
> "official" program, i.e. from MS or from unix binutils, which- if a program was
> already signed- might request some sort of key before it would change anything.
>
> Of course, an even safer way would be to leave the executable alone and to put
> an early check in the startup code that a subsidiary key file existed, and for
> that key to include something that identified the machine or site on which the
> program was entitled to run.

true... but as i recall, one of the goals of this capability was to not have
extra files laying about... i remember the days of dongles and never liked them
at all...
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Editing resource of executable

Graeme Geldenhuys-3
On 01/09/12 01:18, waldo kitty wrote:
> true... but as i recall, one of the goals of this capability was to not
> have extra files laying about... i remember the days of dongles and
> never liked them at all...

+1
I hated those dongles too.


The OnGuard code we use, I simply store the unique unlock code in the
same .ini file I store other application settings like window position
and size in the GetAppConfigDir() location. You can even have a site
unlock key, where the unlock code is then stored in the database (if
your app is a Client/Server app). Thus if one client is unlocked, all
other clients are automatically good to go.


    Graeme.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Editing resource of executable

Mark Morgan Lloyd-5
In reply to this post by wkitty42
waldo kitty wrote:

>> Of course, an even safer way would be to leave the executable alone
>> and to put
>> an early check in the startup code that a subsidiary key file existed,
>> and for
>> that key to include something that identified the machine or site on
>> which the
>> program was entitled to run.
>
> true... but as i recall, one of the goals of this capability was to not
> have extra files laying about... i remember the days of dongles and
> never liked them at all...

Oh yes. And all those copy-protection systems that transferred a token
from a floppy to a file on disc.

But if the choice was between having an extra file or patching the
executable, and if the patched executable failed on 5% of customer
systems due to an OS or anti-virus check, I'd settle for the extra file
and count myself lucky.

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Editing resource of executable

Rainer Stratmann
In reply to this post by Krzysztof
Am Wednesday 29 August 2012 23:46:55 schrieb Krzysztof:
> I have some problem. Example:
>
> I created some exec using free pascal and lazarus. It is placed on
> http server. User in webbrowser click download, and server should
> attach link from where it is clicked into this exec. So when user run
> downloaded exec, this exec can read this link.

> The idea is that
> everything is in one file (and must be).

This can be achieved easily with including data as I suggest some times here.
(Including binary data making easy).

Then you can do with your included data what you want. You are more
independend than relying to (an) additional tool(s).

> My question is, exists any
> commandline tool which can edit executable file for edit some resource
> (like ResEdit or Restorator for Windows) which can be used by http
> server?

Complicated in comparison to the solution above.

> By resource I mean some const (or resourcestring section, free
> pascal binary can replace it in runtime, can some tool do this same
> outside?). So main problem is in server side. Finally we can use free
> pascal compiler and compile modified source by server, but this
> solution we use when everything fails (admins, restrictions etc.). I
> need this only for windows (for now), so I'm considering some
> commandline installer makers like InnoSetup or something. Any idea?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Editing resource of executable

wkitty42
In reply to this post by Mark Morgan Lloyd-5
On 9/1/2012 02:56, Mark Morgan Lloyd wrote:

> waldo kitty wrote:
>
>>> Of course, an even safer way would be to leave the executable alone and to put
>>> an early check in the startup code that a subsidiary key file existed, and for
>>> that key to include something that identified the machine or site on which the
>>> program was entitled to run.
>>
>> true... but as i recall, one of the goals of this capability was to not have
>> extra files laying about... i remember the days of dongles and never liked
>> them at all...
>
> Oh yes. And all those copy-protection systems that transferred a token from a
> floppy to a file on disc.

yup! plus there were also those floppy and some CD based ones that insisted on
the floppy or CD being in the drive so they could check for a specific flaw or
data in a specific place that couldn't normally be read by standard tools...

> But if the choice was between having an extra file or patching the executable,
> and if the patched executable failed on 5% of customer systems due to an OS or
> anti-virus check, I'd settle for the extra file and count myself lucky.

agreed... i run an old(er) firewall product that performs MD5 checksums on all
executable files so that it can alert when a file has changed since its last
execution... when i'm updating lazarus from SVN, i like that this firewall pops
up a box alerting me when svn2inc, fpmake, and a few others change during the
build... this lets me know, without having to go hunting, that my new stuff is
up to date... or, at least i think it is... the process will hold everything
until i acknowledge the file update... it gets in the way sometimes but more
often than not, i'm happy to see its alerts...
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
12