File tree Expand file tree Collapse file tree 1 file changed +26
-6
lines changed Expand file tree Collapse file tree 1 file changed +26
-6
lines changed Original file line number Diff line number Diff line change @@ -18,9 +18,29 @@ def func_calls():
18
18
prep = req .prepare ()
19
19
session .rebuild_proxies (prep , proxies )
20
20
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 )
You can’t perform that action at this time.
0 commit comments