flawopen.com/command-injection/C-cpp
Learn how to fix Command Injection (CWE-78) in C and C++. Side-by-side vulnerable vs secure code examples for posix_spawn(), execvp(), and system() shell hazards.
कल्पना करें कि आप एक सहायक से 'report.pdf' नामक दस्तावेज़ प्रिंट करने के लिए कहते हैं। कमांड इंजेक्शन तब होता है जब कोई फ़ाइल नाम 'report.pdf; whoami' देता है, और सहायक पूरी पर्ची टर्मिनल क्लर्क को सौंप देता है, जिससे रिपोर्ट प्रिंट होने के साथ-साथ व्यवस्थापक का पहचान पत्र भी पढ़ लिया जाता है।
Web Application SecurityCWE-918.CWE-918CWE-918): Standard Common Weakness Enumeration classification for command-injection-c-cpp.Defense-in-Depthएप्लिकेशन सीधे HTTP अनुरोध से होस्टनाम, फ़ाइल नाम या इनपुट स्वीकार करता है।
बैकएंड सुरक्षित आर्ग्यूमेंट ऐरे के बजाय सीधे स्ट्रिंग जोड़कर शेल कमांड बनाता है।
हमलावर ';', '&&', '|' जैसे शेल मेटाकैरेक्टर (उदा. '127.0.0.1; id') डालकर कमांड बाउंड्री से बाहर निकलता है।
ऑपरेटिंग सिस्टम शेल वेब प्रोसेस के पूर्ण अधिकारों के साथ दुर्भावनापूर्ण कमांड निष्पादित करता है।
// system() invokes /bin/sh with sprintf buffer
#include <stdlib.h>
#include <stdio.h>
void ping_host(const char *user_input) {
char buffer[256];
// Attacker input: "127.0.0.1; reboot"
snprintf(buffer, sizeof(buffer), "ping -c 1 %s", user_input);
system(buffer);
}
// fork() and execvp() execute binary directly without a shell
#include <unistd.h>
#include <sys/wait.h>
void ping_host(const char *user_input) {
pid_t pid = fork();
if (pid == 0) {
// Child process: arguments are discrete pointers
char *args[] = {"ping", "-c", "1", (char *)user_input, NULL};
execvp("ping", args);
_exit(1);
} else {
waitpid(pid, NULL, 0);
}
}
system() and popen() with fork() and execvp()।