3

How can I execute each command pre-appended with another one?

Example when I run:

nmap -p 80 host

I want it to run

proxychains nmap -p 80 host

even when I do not add proxychains intentionally.

In other words: can I alias all commands at once with proxychains pre-appended?

Bonus if this is something I can switch on/off.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
AK_
  • 3,622
  • 2
  • 8
  • 10

2 Answers2

7

Would it work to just run a full shell under proxychains? Assuming it can deal with processes started by the shell properly.

You could do with just

$ proxychains bash

and exit the shell at will.


But if you really want to, you can abuse the DEBUG trap (with extdebug set) to mangle the commands the shell runs. This would run every command with time:

$ shopt -s extdebug
$ d() { eval "time $BASH_COMMAND"; return 1;  }
$ trap d DEBUG
$ sleep 2

real    0m2.010s
user    0m0.000s
sys     0m0.000s
$ trap - DEBUG         # turn it off, this still prints the 'time' output

But the tricky part here is that it will also affect builtins, like trap or shopt themselves, so you'd probably want to add some exceptions for those... Also, stuff like cd somedir would turn into proxychains cd somedir, which probably will not work. This would also affect everything started from within functions etc. Maybe it's better to just have the function use proxychains only for those commands known to need it.

ilkkachu
  • 133,243
  • 15
  • 236
  • 397
  • this is genius solution! I wish I can give you 100 points for it. Thank you! – AK_ Dec 17 '17 at 10:29
  • bonus of bonus: is there a way I can temporarily ask bash to escape one command to run it without `proxychains` ? example I don't want `ls` to be slow and wait for `proxychains` – AK_ Dec 17 '17 at 10:31
  • 1
    @AK_, with the shell under proxychains, I don't think so. With the DEBUG trap you could of course do anything, but listing the programs you want to use proxychains with, or the ones without it might get annoying. Which leads me to think if it would be easier to just `alias p=proxychains` and then go with `p nmap... ` when you need it... – ilkkachu Dec 17 '17 at 11:03
1

You can just create an alias for your current sesion.

alias nmap="proxychains nmap"

Everytime you write nmap, it would be as writting "proxychains nmap". You can turn it off with:

unalias nmap

You could also create a shell script to manage this. Just add it to your bashrc

function prefix {
if [ $2 != "with" ]; then
    echo "You have to prefix something with something"
    return
fi
echo "alias $1 = $3 $1"

alias $1="$3 $1"
}
function unprefix {
    alias $(alias $1 | sed "s/=.\b.*\b /='/g")
}

So you can do:

prefix nmap with proxychains
unprefix nmap
Blasco
  • 264
  • 1
  • 2
  • 11