ht.
ht.

Reputation: 25

Analysing a shell script

This would be part of a reverse-engineering project.

To determine and document what a shell script (ksh, bash, sh) does, it is comfortable, if you have information about what other programs/scripts it calls.

How could one automate this task? Do you know any program or framework that can parse a shell script? This way for instance, I could recognize external command calls -- a step to the right direction.

Upvotes: 0

Views: 1070

Answers (5)

Shiva
Shiva

Reputation: 116

How about:

  1. Get a list of distinct words in that script

  2. Search $PATH to find a hit for each

?

Upvotes: 1

bta
bta

Reputation: 45057

If you can't actually run the script, try loading it into a text editor that supports syntax highlighting for Bash. It will color-code all of the text and should help indicate what is a reserved word, variable, external command, etc.

Upvotes: 0

Tobu
Tobu

Reputation: 25416

Bash's xtrace is your friend.

You can invoke it with:

  • set -x at the top of your script,
  • by calling your script with bash -x (or even bash --debugger -x),
  • or recursively by doing (set -x; export SHELLOPTS; your-script; )

Upvotes: 0

tomala
tomala

Reputation: 1

bash -v script.sh ?

Upvotes: 0

Francis
Francis

Reputation: 12008

For bash/sh/ksh, I think you can easily modify their source to log what has been executed. That would be a solution.

Upvotes: 1

Related Questions