0

Like the title saying.
I have a C program and I compile it to an ELF by using gcc.
Now I want to run this ELF up and using netcat to proxy it.
Let client can netcat to service then send message and get response

I run the command below

nc -lp 8763 -e ./test

then shell tell me that natcat do not have -e option

❯ nc -e
nc: invalid option -- 'e'
usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl]
          [-m minttl] [-O length] [-P proxy_username] [-p source_port]
          [-q seconds] [-s sourceaddr] [-T keyword] [-V rtable] [-W recvlimit]
          [-w timeout] [-X proxy_protocol] [-x proxy_address[:port]]
          [destination] [port]

So I trying socat.

socat tcp4-l:8763 exec:./test

The service run, but when I connect the service from client.
Server cannot get the message sent by client.

The only way I've tried to succeed is running the command below.

socat - tcp4-l:8763 | ./test

But it can only recieve message from client, cannot response and message to client.

Does someone know why, or how can I make a netcat server like CTF.
Here is my C program

// test.c
#include <stdio.h>

int main(void){
    int n;
    while (scanf("%d", &n) != EOF){
        if (n == 1) {
             printf("Only one\n");
        } else {
             printf("%d\n", n);
        }
    }
    return 0;
}

then compile it with

gcc -o test test.c
  • How can you be sure that the problem is not related to your program? (Maybe buffering issues?) Please [edit] your question and show a [mre] to allow us to reproduce the problem. This would include the source code of `test` and what you use/do at the other end of the connection. – Bodo May 16 '23 at 17:49
  • I apologive. I just edit my question and add my code recently. Maybe my code is why it went wrong. – Miyago9267 May 17 '23 at 09:12
  • Problem 1 is that you used buffered I/O: Just try: "./test | cat", and you might no see immediate output (on pipes and sockets, versus terminal). Problem 2 is that you probably use netcat as client that does not seem to support half close. Try socat client, and you should see the buffered output when you type ^D. – dest-unreach May 17 '23 at 10:49
  • Thus, use fflush(stdout) in your code, or run the server using a pseudo terminal: "socat tcp4-l:8763 exec:./test,pty,cfmakeraw" – dest-unreach May 17 '23 at 10:56
  • As written in other comments I think it's an issue with buffering. You still did not tell us what you use to connect to the server and what data you send. If you enter something that cannot be converted as a decimal integer, e.g. `a`, your program will get into an endless loop. If you want to implement a line based protocol I suggest to read the data with `fgets` and then parse the line of input. If you use `*scanf` for parsing, check the return value. EOF is only one possible value. It will normally tell you how many items have been converted, which might be 0 if you enter e.g. `a` for `%d`. – Bodo May 17 '23 at 15:28

0 Answers0