System function in c not working for me

I use this code to extract a password protected rar file. I use a system function to invoke the rar command. If I use the password in the system command, it works. But as trying to pass the password as a parameter, it is not. for example, if in this code, if I use the pwd password, it gives the error "pwd is not recognized as an internal or external command, operating program or batch file." But if I change the code and do it "system (" rar e -ppdd wingen.rar "), it works. Can someone explain to me what mistake I am making? Thanks in advance.

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char **argv)
    {
    char pword[20];
    printf("enter the pword : ");
    gets(pword);
    system(("rar e -p%s wingen.rar",pword));
    getchar();
    return 0;
    }
+5
source share
3 answers

system() printf(). , system():

char command[100];
sprintf(command, "rar e -p%s wingen.rar", pword);
system(command);

, , - , , . , , 100%:

system(pword);

, , .

+4

system() - const char *. ,

system("rar e -p%s wingen.rar",pword);

- , system(). ,

system(("rar e -p%s wingen.rar",pword));

, . , , . , :

system(pword);

:

system("pwd");

pwd ( POSIX- ... ). , , , - , sprintf:

char buff[256];
sprintf(buff, "rar e -p%s wingen.rar", pword);

, ( , , ):

char buff[256] = "rar e -p";
strcat(buff, pword);
strcat(buff, " wingen.rar");
+17

system() . :

    int system(const char *command);

. snprintf().

char buf[512];
snprintf(buf, sizeof(buf), "rar e -p%s wingen.rar", pword);
system(buf);

EDIT: all of these solutions are bad ideas, as there is a vulnerability to injection using a system with unanimated input.

Even if it uses snprintf, as with my answer, or strcat, like the other, the problem still exists, since system () (at least with / bin / sh on * nix systems) can execute several commands with a single call functions.

 system("rar e -pXX wingen.rar ; rm -rf * ; # wingen.rar")

will be obtained from pwd = "XX wingen.rar; rm -rf *; #"

+6
source

All Articles