T09 · Insecure Skill Coding Practices
- Location
scripts/hilink.sh:211- Finding
Arbitrary Python Code Injection Through the SMS Index Argument
- Content
View full analysis
1501000') echo "$resp" | python3 -c " import sys, xml.etree.ElementTree as ET tree = ET.parse(sys.stdin) for msg in tree.findall('.//Message'): if msg.findtext('Index') == '${index}': print(f'From: {msg.findtext(\"Phone\")}') print(f'Date: {msg.findtext(\"Date\")}') print('---') print(msg.findtext('Content', '')) break else: print('SMS ${index} not found') " 2>/dev/null } ``` ### Technical Analysis The `index` value originates from the third command-line argument and is inserted directly into source code supplied to `python3 -c`. The shell's double-quoted string permits parameter expansion, but it does not encode the expanded value as a safe Python string. An attacker-controlled index containing quotes, line breaks, comments, or other Python syntax can terminate the intended string literal and alter the generated Python program. Because the result is interpreted as source code rather than data, successful injection allows arbitrary Python execution. The value is interpolated in two places, which may require the malicious input to preserve valid Python syntax at both locations. This complicates exploitation but does not establish a security boundary; a crafted multiline value can account for both contexts. ### Attack Path 1. An attacker supplies or persuades a user or Agent to use a crafted value as an SMS index. 2. The user invokes: ```bash s ...[truncated 1019 chars]- Remediation
View remediation
&2 return 1 } ``` 2. Pass the index as a separate Python argument: ```bash echo "$resp" | python3 -c ' import sys import xml.etree.ElementTree as ET index = sys.argv[1] tree = ET.parse(sys.stdin) for msg in tree.findall(".//Message"): if msg.findtext("Index") == index: print(f"From: {msg.findtext(\"Phone\")}") print(f"Date: {msg.findtext(\"Date\")}") print("---") print(msg.findtext("Content", "")) break else: print(f"SMS {index} not found") ' "$index" ``` 3. Never interpolate untrusted values into source strings passed to interpreters such as `python3 -c`, `sh -c`, or `eval`. 4. Add regression tests using quotes, newlines, semicolons, backslashes, and Python comment characters to verify that malformed indices are rejected and never interpreted. ]]>
