Skip to content

Commit ca26292

Browse files
author
patched.codes[bot]
committed
Patched main.py
1 parent 2441ace commit ca26292

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

main.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,29 @@ def func_calls():
1818
prep = req.prepare()
1919
session.rebuild_proxies(prep, proxies)
2020

21-
# Introduce a command injection vulnerability
22-
user_input = input("Enter a command to execute: ")
23-
command = "ping " + user_input
24-
subprocess.call(command, shell=True)
25-
26-
print("Command executed!")
21+
# Execute ping command securely
22+
try:
23+
user_input = input("Enter a hostname to ping: ").strip()
24+
25+
# Basic hostname validation
26+
if not user_input or ' ' in user_input or ';' in user_input or '&' in user_input or '|' in user_input:
27+
print("Error: Invalid hostname. Please provide a valid hostname without special characters.")
28+
exit(1)
29+
30+
# Use subprocess.run with argument list and shell=False
31+
result = subprocess.run(['ping', user_input],
32+
shell=False,
33+
check=False,
34+
capture_output=True,
35+
text=True)
36+
37+
if result.returncode == 0:
38+
print("Command executed successfully!")
39+
print(result.stdout)
40+
else:
41+
print(f"Error executing command. Return code: {result.returncode}")
42+
print(f"Error output: {result.stderr}")
43+
44+
except Exception as e:
45+
print(f"An error occurred: {str(e)}")
46+
exit(1)

0 commit comments

Comments
 (0)