You are not logged in.

#1 2008-11-08 15:10:40

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,000
Website

Parsing sh/bash scripts: Get all executable/function calls out of it

I'm doing some analysing of some code written in bash.
Now I must know all the "things" that are executed (or can be executed) from inside the script, without executing the script.
So basically the script must be parsed and all the occurrences of executions of programs and sh functions must be listed.

Does anyone know if there is a tool/script for this somewhere?

Thanks.

Last edited by Dieter@be (2008-11-08 15:12:07)


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#2 2008-11-08 15:33:44

skymt
Member
Registered: 2006-11-27
Posts: 443

Re: Parsing sh/bash scripts: Get all executable/function calls out of it

First you should know that it's impossible to get a guaranteed-correct answer without executing the script. Scripts can call programs/functions whose names are found/generated at runtime. The most common example is opening a text file using the program whose name is stored in $EDITOR.

That said, you can get an approximation by writing a parser. Parsers are hard. Smart programmers use a parser generator like bison or ANTLR to abstract away much of the complexity, but it's still sort of a pain in the butt the first time or two.

If you don't want to write a parser, a very approximate answer can be found by splitting each line by whitespace and returning the first word. Then finding other places commands can hide, like after semicolons, backticks, "$(", etc, but not when quoted. At this point, you will have written a very shoddy parser and taken longer at it than you would have if you'd bothered to learn bison or ANTLR.

Last edited by skymt (2008-11-08 15:34:36)

Offline

#3 2008-11-08 18:36:03

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: Parsing sh/bash scripts: Get all executable/function calls out of it

Yeah, this is totally impossible to do for most programming languages. If you're just trying to find out dependencies you can do something workable, but for security purposes, for example, it's totally inadequate. Especially in a language that can produce entirely new programs to run.

Offline

#4 2008-11-08 18:39:59

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,000
Website

Re: Parsing sh/bash scripts: Get all executable/function calls out of it

Well, in this specific case we can luckily ignore the fact that the script can dynamically generate executable/function names.  I know the code a bit, and I know I have enough with the executables/functions that are hard coded smile


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#5 2008-11-09 13:11:57

carlocci
Member
From: Padova - Italy
Registered: 2008-02-12
Posts: 368

Re: Parsing sh/bash scripts: Get all executable/function calls out of it

run the script, see what processes are started with ps?

Offline

#6 2008-11-09 13:17:28

moljac024
Member
From: Serbia
Registered: 2008-01-29
Posts: 2,676

Re: Parsing sh/bash scripts: Get all executable/function calls out of it

Run the script in an isolated environment ? A virtual machine ?
I mean, if you're concerned about whether it is safe to execute...

Last edited by moljac024 (2008-11-09 13:18:20)


The day Microsoft makes a product that doesn't suck, is the day they make a vacuum cleaner.
--------------------------------------------------------------------------------------------------------------
But if they tell you that I've lost my mind, maybe it's not gone just a little hard to find...

Offline

#7 2008-11-09 13:19:39

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,000
Website

Re: Parsing sh/bash scripts: Get all executable/function calls out of it

carlocci wrote:

run the script, see what processes are started with ps?

moljac024 wrote:

Run the script in an isolated environment ? A virtual machine ?

carlocci & moljac024:
Not an option.  The script can take many different routes according to a variety of parameters.  Checking the processes is not good either because a process can finish really fast, some things are just sh *functions* etc.  I need to do this because I'm refactoring some code ;-)

Last edited by Dieter@be (2008-11-09 13:20:07)


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#8 2008-11-09 13:41:57

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Parsing sh/bash scripts: Get all executable/function calls out of it

Try set -x

Offline

#9 2008-11-10 02:33:26

carlocci
Member
From: Padova - Italy
Registered: 2008-02-12
Posts: 368

Re: Parsing sh/bash scripts: Get all executable/function calls out of it

for word in $(cat filename); do
[ -f $word -o -f /bin/$word and so on ] && echo $word
done

you might want to sanitize a little bit removing ",(,`,; and so on from word
you might want to check for execution instead of existance (-f)

Offline

Board footer

Powered by FluxBB