-2

I have a executable linux file called "Fyserver". I want to open it with this arg

./Fyserver --pass 2849

If I don't enter this --pass arg to ELF, then it should exit itself without even running. Is there a way to do this?

NOTE: I don't have the source code of this ELF. I want to do it in bash.

Erikli
  • 115
  • 5
  • 1
    Your question is unclear. Are you asking for how to create an [alias](https://www.google.com/search?q=bash+alias) so you can avoid typing out the entire command each time? Or did you want to create an icon you can click on in your file manager? – Dúthomhas Oct 11 '22 at 11:29
  • Does this answer your question? [An example of how to use getopts in bash](https://stackoverflow.com/questions/16483119/an-example-of-how-to-use-getopts-in-bash) – m19v Oct 11 '22 at 11:32
  • @Dúthomhas No. I want to add an password argument for some executable linux file. If the user runs the `./Fyserver` the executable linux file should exit itself. If the user runs the `./Fyserver --pass string`, it should run. – Erikli Oct 11 '22 at 11:43
  • @m19v No It doesn't. I want to add `--pass` argument for some executable linux file (not bash script, and not mine). – Erikli Oct 11 '22 at 11:44
  • _**Your question is unclear.**_ I have no idea what Fyserver is, or what `--pass NNNN` does, or what you expect to happen. If you are trying to modify an executable to do something it doesn’t do, then... that’s not how things work. – Dúthomhas Oct 11 '22 at 11:48
  • Is the new command argument supposed to modify the behavior of the program? – Dúthomhas Oct 11 '22 at 11:51
  • @Dúthomhas It's something like that. If the user runs this ELF without `--pass NNNN` argument, It shouldn't run. – Erikli Oct 11 '22 at 11:52

1 Answers1

6

There is no sane way to do what you are asking. You can't add new behavior to a binary without access to the source code or some serious reverse engineering skills.

The usual solution is to create a simple wrapper, i.e. move ./Fyserver to ./Fyserver.real and create a script like

#!/bin/sh
[ "$1" = "--pass" ] || { echo "Syntax: $0 --pass <pass>" >&2; exit 127; }
exec ./Fyserver.real "$@"

The argument checking could arguably be more sophisticated, but this should at least give you an idea of how this is usually handled.

If you really wanted to, I suppose you could write this logic in a compiled language, and embed the original binary within it somehow.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • This answer only works when `./Fyserver.real` is in the current directory. The answer can be _trivially_ made better by using `exec "$0.real"` instead. – Employed Russian Oct 13 '22 at 03:22
  • I originally wrote an instruction to move it somewhere like `/usr/local/bin/Fyserver.real` but the OP is specifically asking about running something in the current directory. But thanks, your suggestion is an improvement. – tripleee Oct 13 '22 at 04:21