C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-09-2009, 11:09 PM   #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.
redone is offline   Reply With Quote
Old 09-10-2009, 09:17 AM   #2
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 09-10-2009, 10:52 AM   #3
Jaxom's & Imriel's Dad
 
Kennedy's Avatar
 
Join Date: Aug 2006
Location: Alabama
Posts: 801
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>.
Kennedy is offline   Reply With Quote
Old 09-10-2009, 12:51 PM   #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
redone is offline   Reply With Quote
Old 09-10-2009, 02:33 PM   #5
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 09-10-2009, 03:46 PM   #6
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 09-10-2009, 04:40 PM   #7
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
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.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 09-10-2009, 05:46 PM   #8
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 09-10-2009, 07:39 PM   #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
redone is offline   Reply With Quote
Old 09-11-2009, 08:41 AM   #10
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
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.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS

Last edited by MK27; 09-11-2009 at 08:51 AM.
MK27 is offline   Reply With Quote
Old 09-11-2009, 09:05 AM   #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
redone is offline   Reply With Quote
Old 09-11-2009, 09:17 AM   #12
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> 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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 09-11-2009, 09:19 AM   #13
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
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.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS

Last edited by MK27; 09-11-2009 at 09:21 AM.
MK27 is offline   Reply With Quote
Old 09-11-2009, 09:25 AM   #14
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
>> 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?
Sebastiani is offline   Reply With Quote
Old 09-11-2009, 09:35 AM   #15
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
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.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing and modifying data in a file Micko C Programming 2 02-17-2005 03:42 AM


All times are GMT -6. The time now is 02:05 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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