Skip to content

Start-Transcript might silently delete the output file #4065

Closed

Description

If Start-Transcript is ran without -Append and a user has write privileges on the file without write privileges on the parent folder, the file will be silently deleted without any kind of error message.

Steps to reproduce

As administrator create a folder and file with the privileges specified above. I'm assuming that a local user, "myuser" exist:

mkdir "c:\test\"
icacls.exe "c:\test" "/grant" "builtin\administrators:(CI)(OI)(F)" "/inheritance:r"
icacls.exe "c:\test" "/grant" "myuser:(CI)(OI)(RX)"
"" | Out-File -Encoding "ascii" -LiteralPath "c:\test\my-transcript.txt"
icacls.exe "c:\test\my-transcript.txt" "/grant:r" "myuser:(F)"

Run Start-Transcript as "myuser":

Start-Transcript -LiteralPath "c:\test\my-transcript.txt"
Stop-Transcript

Expected behavior

The transcript should be written to c:\test\my-transcript.txt

Actual behavior

The output states success, but the file is actually deleted.

PS C:> Start-Transcript -LiteralPath "c:\test\my-transcript.txt"
Transcript started, output file is c:\test\my-transcript.txt

PS C:> Stop-Transcript
Transcript stopped, output file is C:\test\my-transcript.txt

PS C:> Test-Path "c:\test\my-transcript.txt"
False

Environment data

Not the newest PowerShell ...

PS C:> $PSVersionTable

Name Value


PSVersion 5.1.14393.1358
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.1358
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

... but you can clearly see the error in line 230 of StartTranscriptCmdlet.cs :

// If they didn't specify -Append, delete the file
if (!_shouldAppend)
{
	System.IO.File.Delete(effectiveFilePath);
}

This part of the code should rather write some sort of zero length content to the file.

Activity

  1. added
    WG-Security
    Up-for-Grabs
    on Jun 22, 2017
  2. charlieschmidt commented on Oct 22, 2017

    @charlieschmidt

    Paal Braathen (@paalbra) are you able to reproduce this on a verison of powershell-core? That line is correct - if the file exists, and the user hasn't said -Append (after some other conditions) - then delete the file before starting any of the real logic. That wipes it out as expected.

    And fwiw, works for me on:

    Name                           Value
    ----                           -----
    PSVersion                      6.0.0-beta
    PSEdition                      Core
    GitCommitId                    v6.0.0-beta.7
    OS                             Darwin 17.2.0 Darwin Kernel Version 17.2.0: Sun Oct  1 00:46:50 PDT 2017; root:xnu-4570.20.62~10/RELEASE_X86_64
    Platform                       Unix
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
    WSManStackVersion              3.0
    
  3. added
    Resolution-Fixed
    and removed
    Up-for-Grabs
    on Oct 22, 2017
  4. SteveL-MSFT commented on Oct 22, 2017

    @SteveL-MSFT
    Member

    With beta.8, following the repro steps, it does delete the file if -Append is not used, as expected. However, the transcript file does exist including a bunch of header information as well as the stop-transcript command.

  5. paalbra commented on Oct 22, 2017

    @paalbra
    ContributorAuthor

    Steve Lee (@SteveL-MSFT) I just ran through the repro steps and the issue is still present in beta.8 (with some changes).

    Did you follow my steps all the way? What account did you use? The account I'm referring to as "myuser" should be an unprivileged account. The account doing the file access setup should be privileged (e.g. an administrator).

    Charlie Schmidt (@charlieschmidt) You should not be able to do that. I believe you might have missed some of the file ACL setup as well?

    The ACLs should become the following after setup:

    PS C:\Program Files\PowerShell\6.0.0-beta.8> icacls C:\test
    C:\test DESKTOP-IGCDSJ7\myuser:(OI)(CI)(RX)
            BUILTIN\Administrators:(OI)(CI)(F)
    
    Successfully processed 1 files; Failed processing 0 files
    PS C:\Program Files\PowerShell\6.0.0-beta.8> icacls C:\test\my-transcript.txt
    C:\test\my-transcript.txt DESKTOP-IGCDSJ7\myuser:(F)
                              DESKTOP-IGCDSJ7\myuser:(I)(RX)
                              BUILTIN\Administrators:(I)(F)
    
    Successfully processed 1 files; Failed processing 0 files
    

    When you start a transcript as "myuser" you now get the following in beta.8:

    PS C:\Program Files\PowerShell\6.0.0-beta.8> Start-Transcript -LiteralPath "c:\test\my-transcript.txt"
    Start-Transcript : Transcription cannot be started due to the error: Access to the path 'C:\test\my-transcript.txt' is
    denied.
    At line:1 char:1
    + Start-Transcript -LiteralPath "c:\test\my-transcript.txt"
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [Start-Transcript], PSInvalidOperationException
        + FullyQualifiedErrorId : CannotStartTranscription,Microsoft.PowerShell.Commands.StartTranscriptCommand
    
    PS C:\Program Files\PowerShell\6.0.0-beta.8> Get-ChildItem -Force C:\test\
    PS C:\Program Files\PowerShell\6.0.0-beta.8>
    

    There was no error before. So the silent part of my first reported issue is no longer the case.

    While it properly emits an error now there is still an issue that should be addressed. Start-Transcript should not delete the file. It should rather wipe the content of the file.

    Version

    PS C:\Program Files\PowerShell\6.0.0-beta.8> $PSVersionTable
    
    Name                           Value
    ----                           -----
    PSVersion                      6.0.0-beta.8
    PSEdition                      Core
    GitCommitId                    v6.0.0-beta.8
    OS                             Microsoft Windows 10.0.15063
    Platform                       Win32NT
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
    WSManStackVersion              3.0
    
  6. added
    Issue-Enhancement
    on Apr 13, 2018
  7. paalbra commented on Oct 26, 2018

    @paalbra
    ContributorAuthor

    You could also reproduce this as an issue on Ubuntu. Since this is a different OS/filesystem the issue isn't completely the same. What happens on Ubuntu is that you won't be able to delete the file since this requires write permissions on the parent directory. This also wouldn't be an issue if Start-Transcript didn't try to delete the file.

    myuser@ubuntu:~$ sudo mkdir /test
    myuser@ubuntu:~$ sudo touch /test/my-transcript.txt
    myuser@ubuntu:~$ sudo chmod a+w /test/my-transcript.txt
    myuser@ubuntu:~$ ls -la /test
    total 8
    drwxr-xr-x  2 root root 4096 okt.  26 22:34 .
    drwxr-xr-x 25 root root 4096 okt.  26 22:34 ..
    -rw-rw-rw-  1 root root    0 okt.  26 22:34 my-transcript.txt
    myuser@ubuntu:~$ pwsh
    PowerShell 6.1.0
    Copyright (c) Microsoft Corporation. All rights reserved.
    
    https://aka.ms/pscore6-docs
    Type 'help' to get help.
    
    PS /home/myuser> Start-Transcript -LiteralPath "/test/my-transcript.txt"
    Start-Transcript : Transcription cannot be started due to the error: Access to the path '/test/my-transcript.txt' is denied.
    At line:1 char:1
    + Start-Transcript -LiteralPath "/test/my-transcript.txt"
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Transcript], PSInvalidOperationException
    + FullyQualifiedErrorId : CannotStartTranscription,Microsoft.PowerShell.Commands.StartTranscriptCommand
     
    PS /home/myuser> Stop-Transcript
    Stop-Transcript : An error occurred stopping transcription: The host is not currently transcribing.
    At line:1 char:1
    + Stop-Transcript
    + ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Stop-Transcript], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.StopTranscriptCommand
     
    PS /home/myuser> Get-ChildItem /test/
    
    
        Directory: /test
    
    
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    ------         10/26/18  10:34 PM              0 my-transcript.txt
    
    
    PS /home/myuser> $PSVersionTable
    
    Name                           Value
    ----                           -----
    PSVersion                      6.1.0
    PSEdition                      Core
    GitCommitId                    6.1.0
    OS                             Linux 4.15.0-38-generic #41-Ubuntu SMP Wed Oct 10 10:59:38 UTC 2018
    Platform                       Unix
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
    WSManStackVersion              3.0
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-EnhancementWG-Security

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions