Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

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

  1. comment changed to:
    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 cmd /c echo {test&whoami} 
    <b style="color:green">  "test&whoami" </b>
      % exec cmd /c echo {test"&whoami}
    <b style="color:red">  "test\"
      my_domain\sebres</b>
    </pre></code>
    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(["cmd", "/c", "echo", 'test"&whoami'])
    <b style="color:red">  "test\"
      my_domain\sebres</b>
    </pre></code>
    
    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>
    
    And following tcl-script (<code>test-dump-inv.tcl</code>):
    <code><pre style="padding-left:10pt">
    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}
    } {
      incr tn
      puts [string repeat - 20]
      foreach cmd {test-dump.exe test-dump.CMD} {
        puts [format "*%3d) \u2018%s\u2019" $tn [join [list $cmd $a] "\u2019 \u2018"]]
        set r [exec $cmd $a]
        puts [regsub -all -line {^} $r "  "]
      }
    }
    </pre></code>
    
    The results:
    <code><pre style="padding-left:10pt">
    % source test-dump-inv.tcl
    --------------------
    <b>*  1) ‘test-dump.exe’ ‘test&whoami’</b>
         0: [test-dump.exe]
         1: [test&whoami]
    <b>*  1) ‘test-dump.CMD’ ‘test&whoami’</b>
         0: [test-dump.exe]
         1: [test&whoami]
    --------------------
    <b>*  2) ‘test-dump.exe’ ‘test|whoami’</b>
         0: [test-dump.exe]
         1: [test|whoami]
    <b>*  2) ‘test-dump.CMD’ ‘test|whoami’</b>
    <b style="color:red">  my_domain\sebres</b>
    --------------------
    <b>*  3) ‘test-dump.exe’ ‘"test&whoami’</b>
         0: [test-dump.exe]
         1: ["test&whoami]
    <b>*  3) ‘test-dump.CMD’ ‘"test&whoami’</b>
    <b style="color:red">     0: [test-dump.exe]
         1: ["test]
      my_domain\sebres</b>
    --------------------
    <b>*  4) ‘test-dump.exe’ ‘"test|whoami’</b>
         0: [test-dump.exe]
         1: ["test|whoami]
    <b>*  4) ‘test-dump.CMD’ ‘"test|whoami’</b>
         0: [test-dump.exe]
         1: ["test|whoami]
    --------------------
    <b>*  5) ‘test-dump.exe’ ‘test"&whoami’</b>
         0: [test-dump.exe]
         1: [test"&whoami]
    <b>*  5) ‘test-dump.CMD’ ‘test"&whoami’</b>
    <b style="color:red">     0: [test-dump.exe]
         1: [test"]
      my_domain\sebres</b>
    --------------------
    <b>*  6) ‘test-dump.exe’ ‘test"|whoami’</b>
         0: [test-dump.exe]
         1: [test"|whoami]
    <b>*  6) ‘test-dump.CMD’ ‘test"|whoami’</b>
         0: [test-dump.exe]
         1: [test"|whoami]
    --------------------
    <b>*  7) ‘test-dump.exe’ ‘"test"&whoami’</b>
         0: [test-dump.exe]
         1: ["test"&whoami]
    <b>*  7) ‘test-dump.CMD’ ‘"test"&whoami’</b>
         0: [test-dump.exe]
         1: ["test"&whoami]
    --------------------
    <b>*  8) ‘test-dump.exe’ ‘"test"|whoami’</b>
         0: [test-dump.exe]
         1: ["test"|whoami]
    <b>*  8) ‘test-dump.CMD’ ‘"test"|whoami’</b>
    <b style="color:red">  my_domain\sebres</b>
    --------------------
    <b>*  9) ‘test-dump.exe’ ‘""test"&whoami’</b>
         0: [test-dump.exe]
         1: [""test"&whoami]
    <b>*  9) ‘test-dump.CMD’ ‘""test"&whoami’</b>
    <b style="color:red">     0: [test-dump.exe]
         1: [""test"]
      my_domain\sebres</b>
    --------------------
    <b>* 10) ‘test-dump.exe’ ‘""test"|whoami’</b>
         0: [test-dump.exe]
         1: [""test"|whoami]
    <b>* 10) ‘test-dump.CMD’ ‘""test"|whoami’</b>
         0: [test-dump.exe]
         1: [""test"|whoami]
    %
    </pre></code>
    
    The major injury is happened by processing of the arguments containing quotes, but some tests show that Tcl seems to have here additionally insufficient escaping by some special characters like pipe (at least looks like vulnerable arguments-handling, also by execution of some tests without unpaired quotes, see for example test 2 and completely another behavior compared to ampersand-character, see tests 9/10, where args of test 10 seems to be not affected in opposite to test 9 with the same quote-chars state inside argument).
    
    WiP. I have almost fixed this for Tcl >= 8.5, working on test-cases and review, so coming soon.
    
  2. login: "sebres"
  3. mimetype: "text/plain"