LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-30-2010, 10:12 PM   #1
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986

Rep: Reputation: 53
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.

Thanks in advance!
 
Old 11-30-2010, 10:27 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am not sure I understand the question? Are you asking if you can create the assembler code from your source code using bash builtins?
 
Old 11-30-2010, 10:46 PM   #3
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986

Original Poster
Rep: Reputation: 53
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.
 
Old 11-30-2010, 10:58 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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)
 
Old 11-30-2010, 11:21 PM   #5
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986

Original Poster
Rep: Reputation: 53
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.
 
Old 12-01-2010, 11:25 AM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Bash scripts can't be be compiled, and it's a terrible idea that goes against everything bash is meant to be.
 
Old 12-01-2010, 12:47 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Bash commands converted to Assembly source.
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.
 
1 members found this post helpful.
Old 12-02-2010, 12:33 AM   #8
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986

Original Poster
Rep: Reputation: 53
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?



Thanks very much for the replies.
 
Old 12-02-2010, 02:22 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by theKbStockpiler View Post
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.
  1. The linked page says nothing about encryption.
  2. Running the strings command on the executable would give a good idea of what it was doing.
  3. 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).
  4. 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.
  5. The bash API is the command line.
  6. 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.
 
2 members found this post helpful.
Old 12-02-2010, 03:18 AM   #10
prodev05
Member
 
Registered: Jul 2009
Location: Planet Earth
Distribution: Unix & Linux Variants
Posts: 304

Rep: Reputation: 20
dude,

try the below command.

"readelf -a /usr/bin/bash"

you will some interesting output. you can find some assembly instructions there.

reg
 
1 members found this post helpful.
Old 12-02-2010, 08:13 AM   #11
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
In the name of the Flyng Spaghetti Monster

Quote:
Originally Posted by theKbStockpiler View Post
If I can throw Bash commands in and get the assembly equivalent out
You can't, because when you type a command, bash doesn't convert that command into assembly language.
 
Old 12-02-2010, 10:57 AM   #12
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986

Original Poster
Rep: Reputation: 53
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.
 
Old 12-02-2010, 11:44 AM   #13
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by theKbStockpiler View Post
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.
 
1 members found this post helpful.
Old 12-02-2010, 12:17 PM   #14
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
In the name of the Flying Spaghetti Monster

Quote:
Originally Posted by ntubski View Post
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 View Post
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
It takes something like this:
Code:
#!/bin/sh

echo hi
echo ho
and produces something like this:
Code:
#!/bin/sh

uudecode -o /dev/stdout <<EOD | zcat | /bin/sh
begin-base64 600 /dev/stdout
H4sICFXh90wAAzIuc2gAU1bUT8rM0y/O4OJKTc7IV8jIhNL5XABIUE83GwAA
AA==
====
EOD
Both of those scripts produce identical output:
Code:
hi
ho
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!
 
1 members found this post helpful.
Old 12-02-2010, 09:28 PM   #15
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986

Original Poster
Rep: Reputation: 53
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.

Thanks again for everyone's expertise!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to view the source code of different commands tubu_cuteguy Linux - Newbie 2 07-24-2010 04:56 PM
Source Code of the Linux commands drimades Linux - General 15 11-16-2006 04:39 AM
How to convert Assembly code to "C" source code ssg14j Programming 2 08-01-2005 12:48 PM
Source code to commands uman Linux - Software 2 10-12-2004 11:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration