Can Bash commands be converted to Assembly like C source code?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986
Rep:
Can Bash commands be converted to Assembly like C source code?
I have read where C is first converted to
Assembly before its final compilation to binary. Is there a way to do this with Bash commands? I would like the understanding that Assembly allows to Bash somehow.
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986
Original Poster
Rep:
To answer your question, No.
With Assembly you know exactly what is going on and it really can't be confused. It is just a matter of a lot of small steps. With a high level language a command is abstracted way to much to a point of being USELESS. Currently I would rather hit two birds with one stone and learn the CPU instruction set commands "Assembly Commands" that Bash is being converted to so I know what is going on. I have Micro Controller Assembly experience , Some C and have discovered for most of my desires that Bash would fill the roll the best. The Problem is that high level Languages don't teach you much. I want to learn PC Assembly but it would be just for learning what is going on. If I could do this with Bash it would save a lot of time but I have to have Bash commands converted to Assembly source. I doubt is is possible but would rather come across as crazy and informed instead of sane and ignorant.
Last edited by theKbStockpiler; 11-30-2010 at 10:48 PM.
I think I follow. Is this not just a case of grabbing the source for bash and then compiling into assembly and doing your work from there?
Or have I missed the point again? (sorry if I am being dense)
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986
Original Poster
Rep:
That is a different perspective and I think you understand.
The CPU is fed binary code that has a one to one ratio to assembly source so every assembly source command or line is matched by exactly one command or line of binary code. I apologize for the lose use of terms. A one word(command) does not do one hundred things. It does one thing. A C compiler changes C source to assembly first so you can see and understand what a C command does in assembly language. This is not by design to allow a person to see C in assembly language. The C compiler naturally goes through steps before it is finally binary code and one of these steps is assembly language. In other words I would like to see Bash commands expressed in Assembly terms (commands).{Edit: Setup() is a C function but it makes no difference to the purpose of this thread.} I was investigating the Bash command Setup() and found basically no information on it. I don't want to learn bash without knowing what the commands do if I can help it. I don't care if it is involved so long as it is detailed enough so it is not like magic. I want to reverse engineer (BASH) a high level language to the lowest level source language (Assembly).
Last edited by theKbStockpiler; 12-01-2010 at 09:55 AM.
Reason: I will never be able to spell.
You can see the C source to bash builtin commands here: http://git.savannah.gnu.org/cgit/bash.git/tree/builtins. As grail suggested, if you compile bash you will get the assembler equivalent, although I doubt this will give much insight.
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986
Original Poster
Rep:
Compiling an Interpreted Language make sense to me. And they said it could not be done.
I read a thread at http://www.mail-archive.com/redhat-l.../msg80653.html that suggested using the C Language System() function to basically encrypt interpreter code, but from what I gather about it, it just uses the API or whatever of the BASH interpreter and BASH just runs the script. Any thoughts on this?
I only want to compile BASH to learn from it in assembly so compiling BASH is actually the way to go. If I can throw Bash commands in and get the assembly equivalent out, I will be happy for a measurable length of time. Any pointers on how to do this?
I read a thread at http://www.mail-archive.com/redhat-l.../msg80653.html that suggested using the C Language System() function to basically encrypt interpreter code, but from what I gather about it, it just uses the API or whatever of the BASH interpreter and BASH just runs the script.
The linked page says nothing about encryption.
Running the strings command on the executable would give a good idea of what it was doing.
The C system() function does not use bash -- it uses /bin/sh which may or may not be bash (if it is bash, bash runs as an approximation of the POSIX shell).
Bash scripts are essentially automated command line sessions and typically use many external commands, most of which are binaries so the effective assembly language is in the binaries, not in Bash.
The bash API is the command line.
The system() function gets /bin/sh to run the passed string as a command. That command is typically not a script but could be.
It seems you are trying to get a good grasp of bash by relating it to assembly language. This approach is not going to work because the assembly language environment (hardware) is so far removed from the bash environment (command line) that there are almost no meaningful analogies.
It' is like a word-processor power user trying to learn assembly language by analogy with the word-processor GUI; they had better start from scratch.
You would have an easier and more productive time getting to know bash by simply learning it in its own right, at the command line and by following introductory tutorials. If you then want to know how bash does what it does, examine the assembler in the bash binary and use debug tools to trace its activity.
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986
Original Poster
Rep:
Thanks for the replies. This is a communal response.
I want to make sure that it is not implied that I will use an unrelated link.
"
> I have some bash scripts ( they are not very complicated, so bash is
>enough ) and i'd like to deploy them on some customer machines. But it would
>be very nice if i could, somehow, 'compile' them so customers wouldnt be
>able to see the source of the script."
LQs editor stopped working so this is the best I can do. Java won't work anymore.
This implies encryption. It does not actually use the term but I was trying the paraphrase it to save some time.
I had a thread here about What the Bash Interpreter actually was and the best explanation I received is that it is an application. I know that it must run commands from the CPU instruction set and that these all have an assembly equivalent. I have no clue as to why I'm not inputting C into a BASH terminal and the interpreter does its job and runs the C source automatically. As I reflect from this post, It appears to be self evident that I have to study how interpreters work in general . It would seem that the benefits of an interpreter must be out weighed by how it accomplishes things from a proxy type of control of the entire computer system.
In reference to ( "readelf -a /usr/bin/bash), I'm not getting the connection of a Linux compiler standard at this time.
Thanks again!
Last edited by theKbStockpiler; 12-02-2010 at 11:00 AM.
I had a thread here about What the Bash Interpreter actually was and the best explanation I received is that it is an application. I know that it must run commands from the CPU instruction set and that these all have an assembly equivalent. I have no clue as to why I'm not inputting C into a BASH terminal and the interpreter does its job and runs the C source automatically.
There are some C interpreters, for instance CINT. CINT doesn't translate the C it reads into machine/assembler though, it has a byte-code compiler (like Java).
Quote:
I only want to compile BASH to learn from it in assembly so compiling BASH is actually the way to go. If I can throw Bash commands in and get the assembly equivalent out, I will be happy for a measurable length of time. Any pointers on how to do this?
You could run bash under gdb which can show a disassembly of the currently running code.
You could run bash under gdb which can show a disassembly of the currently running code.
But he's not interested in disassembling bash itself. He's interested in seeing the assembly code produced by an actual bash command. And bash doesn't work that way. It doesn't translate into assembler, assemble that output, and then run it. See this?
Quote:
Originally Posted by theKbStockpiler
But it would
>be very nice if i could, somehow, 'compile' them so customers wouldnt be
>able to see the source of the script.
Ok, now we see what you really want: script obfuscation. Against a casual observer, it's quite easy; to that end, I've just whipped up this toy program I call "encode":
Code:
#!/bin/sh
# Takes as its parameters an input filename and an output filename, thus:
#
# encode inputfilename outputfilename
if [ "$2" = "" ]
then
echo encode wants an input filename and an output filename 1>&2
exit 1
fi
if [ -e $2 ]
then
echo file $2 already exists 1>&2
exit 1
fi
echo '#!/bin/sh' > $2
echo >> $2
echo 'uudecode -o /dev/stdout <<EOD | zcat | /bin/sh' >> $2
gzip -c $1 | uuencode -m /dev/stdout >> $2
echo EOD >> $2
chmod 755 $2
But it would be obvious to any decent hacker how to see the original code. You want something more obfuscated than that? Build that obfuscation product, and show it to us!
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986
Original Poster
Rep:
I will put anything in GCC and just blame it on Richard Stallman if it does not work.
The quote I posted was for catkin to demonstrate that the link I left virtually included encryption. Part of the miscommunication is because My Browser will not work correctly with LQs web page. The encryption part has no significance. I don't know what I'm doing. Source code is encrypted as far as I'm concerned. The GDB manual I read does not include BASH as being supported. Intuitively I don't believe it will work but maybe it will because BASH script can be used as an argument for System(). There is next to nothing on the web about it. I'm sure that the terms I'm using are not one hundred percent accurate but so long as we are on the same sheet of music its' purpose has been served. I'm currently involved with Linux Kernel guides so I won't be very agile with trying out inserting BASH in GDB. I want to try to get assembly from C in GCC anyways ;so although not on the front burner, I will work on this at a slow pace.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.