This will be a relatively quick article because it's a simple issue. The system
function gets a lot of flak for being slow because it calls the shell runtime to execute a command, but I rarely see the more devastating issue of security brought up.
system
is insecure in many cases. Let's use a simple example of opening Notepad on Windows:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
system("notepad.exe");
getchar();
return 0;
}
This works like a champ, so what's the problem? What if I told you that subverting this program is as simple as copying a malicious program into the same directory and calling it notepad.exe
?
#include <stdio.h>
int main(void)
{
puts("Malicious code! Arrrgh...");
return 0;
}
Now instead of that lovely Notepad interface, you're met with a dastardly console message that your code has been hacked! Worse, the malicious code could do its evil and then open Notepad, with you none the wiser that bad stuff went down in between. If your program is run with superuser privileges, then the malicious program also has those privileges and can do a lot of damage.
- Try it for yourself and see what happens.
- Does this work for built in commands like "cls"?
- Can you find a safe way to execute an external program?