1

I have two files, client.sh and server.sh, all the necessary data is on the server, which is sent to the client using netcat, the client just get these data and display it to the end user, the problem is when the server send some multiline code, the client just receive it as text and display on screen

client.sh

ip=127.0.0.1
client_port=5678
server_port=8765

while :
do
        # Request the menu from the server
        echo menu > /dev/tcp/"$ip"/"$server_port"
        # Waits the server response
        nc -l $porta_cliente
done

server.sh

ip=127.0.0.1
porta_cliente=5678
porta_servidor=8765

while :
do
        nc -vv -l $porta_servidor > logservidor
        echo "Texto recebido: "`cat logservidor` # LOG
        case `cat logservidor` in
        "splash")
                echo "dialog --stdout --msgbox 'SPLASH' 0 0" > /dev/tcp/"$ip"/"$porta_c$
        ;;
        "menu_inicial")
                nc $ip $porta_cliente <<-EOF
                        dialog --stdout --backtitle 'Bem vindo ao SEPA 0.1' --title 'Me$
                        Cadastrar 'Criar um novo usuário' \
                        Entrar 'Fazer login com sua conta' \
                        Sair 'Encerrar o SEPA'

                        # Caso o usuário selecione Cancelar, a execução do script será $
                        if [ $? -eq 0 ]; then
                                echo SUCESSO
                        else
                                rm resposta_servidor dados_digitados 2> /dev/null
                                clear
                                exit
                        fi
                EOF
        ;;
        "menu_principal")
                echo "dialog --msgbox 'MENU_PRINCIPAL' 0 0" > /dev/tcp/"$ip"/"$porta_cl$
        ;;
        *)
                dialog --msgbox 'WTF?!' 0 0 > /dev/tcp/"$ip"/"$porta_cliente"
        ;;
        esac
done

2 Answers 2

2

You can pipe the output of netcat into the shell

nc -l $porta_cliente | sh

however, on a socket with no authentication whatsoever, be extremely careful as this could allow anyone to execute arbitrary code on the client machine.

This method is extremely bad practice and you should consider different approaches.

6
  • Thank you, but do you know any safer alternative? Feb 13, 2018 at 12:53
  • I used nc -l $porta_cliente | sh as you said, but when the server receive the text, the following line if [ $? -eq 0 ]; then is automatically setting the $? to 0, so i can´t verify if the user pressed cancel on dialog, any workaround? Feb 13, 2018 at 13:00
  • You would need to replace if [ $? -eq 0 ]; then with if [ \$? -eq 0 ]; then since it's being substituted in there heredoc before it's sent over netcat.
    – Torin
    Feb 13, 2018 at 13:17
  • As for alternatives, it really depends on what your program wants to be able to do and how much variation the code may have. Have a look at how AJAX is used for a better feel of what kind of information you should be sending over. Keep in mind that with internet, possibly anyone could be seeing the information and at the same time possibly anyone could change the information. In this environment, you should not be sending code over such a channel.
    – Torin
    Feb 13, 2018 at 13:23
  • Thank you a lot! you don´t even have idea how you helped me :) Feb 13, 2018 at 13:48
0

You can also redirect your text from the netcat connection to a file, and then execute it as a command, using source

Example:

nc -l 8765 > command
source command

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .