modifying and saving global variables on a executable file using libelf !!!!

This is a discussion on modifying and saving global variables on a executable file using libelf !!!! within the Linux Programming forums, part of the Platform Specific Boards category; i have a project that requires to use global varibales an array for example, append to it some items and ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    10

    Post modifying and saving global variables on a executable file using libelf !!!!

    i have a project that requires to use global varibales an array for example, append to it some items and save this array using only the executable file (im not allowed to use other files ) modification should be done on the exe file .The only indication i have is to use "libelf" i tried to look at the structure of an elf file but im still not able to access global variables(data segment) and change them.plz i need some help tx in advance.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    And which OS are you trying to accomplish this on?

    The first big question is are you even allowed to open your own executable image for writing?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    If you are in Linux, you could always use getenv() in your code to gather "global" variables passed in by the outside world doing a export <VAR>=<STUFF>.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    I m using ubuntu .Here is the project :
    "Managing user names and passwords for email, network, accounts etc. can be a real hassle. A solution consist in listing ones credentials (in the form of tuples usernames, passwords) in a text file, then encrypting the entire file with a strong encryption algorithm requiring a pass phrase. To retrieve ones credentials i.e., usernames and passwords one has to have both the encrypted textfile and the decryption algorithm at hand. A more elegant solution uses only one executable file that contains both encryption and decryption algorithms as well as the credentials (in a data segment in OS/compiler terms). In this project you are required to implement this elegant solution. When executed, this executable file runs user choices to either view/append or delete credential tuples.
    Hints: You can use libelf for managing data segments of executable files dynamically
    "
    I think this would be better to understand my problem
    tanx for your replies

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Took about 5 seconds
    libelf by Example
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    The project description says "You can use libelf", not that you MUST. There are far easier ways of doing this than mucking with ELF format. In fact, you can do this in a completely portable way with no knowledge at all of the underlying executable format.

    The trick is to embed a large block of data (as a global array) into the program. The beginning of this data is marked by some kind of unique signature. To read or write from this data block, then, all you need to do is open the file, scan until you find the signature, and then you know you are in the correct position to read or write data.

    Of course, this is not even possible if the OS doesn't allow you to alter the program file while the program is executing. But most UNIX-like operating systems do allow you to do this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by brewbuck View Post
    Of course, this is not even possible if the OS doesn't allow you to alter the program file while the program is executing. But most UNIX-like operating systems do allow you to do this.
    Neat trick. Linux will presumably allow it, since you can recompile an executable while it is running.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by MK27 View Post
    Neat trick. Linux will presumably allow it, since you can recompile an executable while it is running.
    When I was working at a document imaging company, we did something exactly like that to embed customer license information into the executable. We supported 7 different platforms (officially) and had a single tool to read and write this information (we called it a "DIB" which I think stands for "deployment info block" which is a weird name but that's what it was)

    The tool didn't need any knowledge of the executable format, it just scanned the file for the signature and then it knew where the data was. The downside is that the size of the DIB is fixed and can't be changed, but we didn't need something like that anyway.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    Quote Originally Posted by brewbuck View Post
    The project description says "You can use libelf", not that you MUST. There are far easier ways of doing this than mucking with ELF format. In fact, you can do this in a completely portable way with no knowledge at all of the underlying executable format.

    The trick is to embed a large block of data (as a global array) into the program. The beginning of this data is marked by some kind of unique signature. To read or write from this data block, then, all you need to do is open the file, scan until you find the signature, and then you know you are in the correct position to read or write data.
    how do i know the signature of the variable if possible give a source code example
    thanksi advance

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by redone View Post
    how do i know the signature of the variable if possible give a source code example
    thanksi advance
    There may be some problems with this idea, in fact, altho I haven't done any reading about the structure of executables, so I'm kind of acting blindly, but:
    Code:
    #include <string.h>
    
    int main() {
    	char sig[1029]="XOXOX", *start=&sig[5];
    	memset(start,'Z',1024);
    	strcpy(start,"hello world");
    	return 0;
    }
    Looking at that with hexdump -c ./a.out, it's possible to find XOXOX, or at least XOXO, which is to say that is not always contiguous. It is also possible to find "hello world", altho it is no where near XOXOX. And there is not a single Z in the file.

    There are some interesting things in the hexdump that imply to me you could access the data segment without using libelf but probably not in as naive a way as this...

    ps. anyone know why gcc's default output file is still called a.out? AFAIK an "a.out" binary is historically not the same as an ELF binary, but of course the a.out from gcc on linux is an ELF binary.
    Last edited by MK27; 09-11-2009 at 08:51 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    Normally what i understood is that I should scan my executable file until i find the the location of the global variable is that right ?? if it's right then how can compare a file descriptor with the my global variable

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > how do i know the signature of the variable if possible give a source code example
    You mean it's not YOUR executable?
    And why does it seem like we're the ones doing all the work all of a sudden.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by redone View Post
    Normally what i understood is that I should scan my executable file until i find the the location of the global variable is that right ?? if it's right then how can compare a file descriptor with the my global variable
    Try hexdump, it's probably installed by default. There's also readelf, which "readelf -h ./a.out" gives you output like this:
    Code:
    ELF Header:
      Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
      Class:                             ELF64
      Data:                              2's complement, little endian
      Version:                           1 (current)
      OS/ABI:                            UNIX - System V
      ABI Version:                       0
      Type:                              EXEC (Executable file)
      Machine:                           Advanced Micro Devices X86-64
      Version:                           0x1
      Entry point address:               0x400420
      Start of program headers:          64 (bytes into file)
      Start of section headers:          2992 (bytes into file)
      Flags:                             0x0
      Size of this header:               64 (bytes)
      Size of program headers:           56 (bytes)
      Number of program headers:         8
      Size of section headers:           64 (bytes)
      Number of section headers:         30
      Section header string table index: 27
    It looks to me like it may be more complicated than just finding an offset and writing to it. Brewbuck is a smart cookie, but he didn't say he had done this, he said "we", which might imply he has actually not, and therefore may be unaware of some complications.

    On the other hand, it must be possible:
    http://www.cse.iitm.ac.in/moodle/mod...iew.php?id=459

    But you are the one studying this stuff! Since you have to write something to the binary anyway, you might as well start experimenting. It is not as if it will matter if you accidentally wreck your executable.
    Last edited by MK27; 09-11-2009 at 09:21 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> if it's right then how can compare a file descriptor with the my global variable

    Why would you want to do that? The point is simply to find the target block so that you can read/replace the stored data. Does that make sense?

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Sebastiani View Post
    >> if it's right then how can compare a file descriptor with the my global variable

    Why would you want to do that? The point is simply to find the target block so that you can read/replace the stored data. Does that make sense?
    I think the OP understands that and perhaps by fd meant like, what is in the file.

    You won't find the name of the variable (eg, from my previous example, "sig" or "start"), but what you will find is the content ("XOXOX" and "hello world").

    Now get busy, try some code and then ask more questions.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 02:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21