peass-ng/PEASS-ng · error · ArgumentException

Network path is not allowed for directory junction: [{0}]

Error message

Network path is not allowed for directory junction: [{0}]

What it means

CreateJunctionCore rejects junction targets (directoryPath) that are UNC/network paths. Junctions (NTFS reparse points of type mount point) cannot point to network shares, so AlphaFS pre-checks new DriveInfo(directoryPath).IsUnc and throws ArgumentException with Resources.Network_Path_NotAllowed before making any native call.

Source

Thrown at winPEAS/winPEASexe/winPEAS/3rdParty/AlphaFS/Filesystem/Directory Class/Directory Core Methods/Directory.CreateJunctionCore.cs:78

      {
         if (pathFormat != PathFormat.LongFullPath)
         {
            Path.CheckSupportedPathFormat(directoryPath, true, true);
            Path.CheckSupportedPathFormat(junctionPath, true, true);

            directoryPath = Path.GetExtendedLengthPathCore(transaction, directoryPath, pathFormat, GetFullPathOptions.RemoveTrailingDirectorySeparator);
            junctionPath = Path.GetExtendedLengthPathCore(transaction, junctionPath, pathFormat, GetFullPathOptions.RemoveTrailingDirectorySeparator);

            pathFormat = PathFormat.LongFullPath;
         }


         // Directory Junction logic.


         // Check if drive letter is a mapped network drive.
         if (new DriveInfo(directoryPath).IsUnc)
            throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.Network_Path_Not_Allowed, directoryPath), "directoryPath");

         if (new DriveInfo(junctionPath).IsUnc)
            throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.Network_Path_Not_Allowed, junctionPath), "junctionPath");


         // Check for existing file.
         File.ThrowIOExceptionIfFsoExist(transaction, false, directoryPath, pathFormat);
         File.ThrowIOExceptionIfFsoExist(transaction, false, junctionPath, pathFormat);


         // Check for existing directory junction folder.
         if (File.ExistsCore(transaction, true, junctionPath, pathFormat))
         {
            if (overwrite)
            {
               DeleteDirectoryCore(transaction, null, junctionPath, true, true, true, pathFormat);

               CreateDirectoryCore(true, transaction, junctionPath, null, null, false, pathFormat);

View on GitHub (pinned to 53fb989abc)

Solutions

  1. Use a symbolic link instead of a junction: symlinks can target UNC paths (Directory.CreateSymbolicLink / mklink /D), keeping in mind they need admin or Developer Mode on older Windows.
  2. Junction to a local staging copy: copy or mount the share to a local path (e.g. via `net use` mapped drive is NOT enough — it's still UNC-backed) and junction to a genuine local directory.
  3. Validate the target before calling: new DriveInfo(target).IsUnc or target.StartsWith(@"\\") and fail with your own message.
  4. If the share must appear at a fixed path, use DFS or SMB mapping at the OS level rather than a junction.

Example fix

// before
Directory.CreateJunction(@"C:\links\\data", @"\\\\server\\share\\data");
// after
// Symlinks may point to UNC paths; junctions may not.
Directory.CreateSymbolicLink(@"C:\links\\data", @"\\\\server\\share\\data", true);
Defensive patterns

Strategy: validation

Validate before calling

static void EnsureLocalJunctionTarget(string target)
{
    bool isUnc = target.StartsWith(@"\\")
        || (target.Length >= 2 && new DriveInfo(target.Substring(0, 1) + @":\\").DriveType == DriveType.Network);
    if (isUnc)
        throw new ArgumentException($"Junction target must be a local path: {target}");
}
EnsureLocalJunctionTarget(targetPath);
Directory.CreateJunction(linkPath, targetPath);

Type guard

static bool IsLocalFileSystemPath(string p) =>
    !p.StartsWith(@"\\") &&
    new DriveInfo(Path.GetPathRoot(Path.GetFullPath(p))).DriveType != DriveType.Network;

Try / catch

try
{
    Directory.CreateJunction(linkPath, targetPath);
}
catch (ArgumentException ex) when (ex.Message.Contains("Network path is not allowed"))
{
    // fall back to symlink, which supports UNC targets
    Directory.CreateSymbolicLink(linkPath, targetPath, true);
}

Prevention

When it happens

Trigger: Calling Directory.CreateJunction (or CreateJunctionTransacted) with a junctionWanted/target path starting with "\\\\" (UNC, e.g. \\\\server\\share\\dir) or a mapped drive whose underlying provider is UNC.

Common situations: Trying to junction to a network share to 'link' remote storage locally; a drive letter that is actually a mapped network drive (DriveInfo.IsUnc true after provider resolution); build scripts linking to \\\\server\\projects; misconfigured deploy targets.

Related errors


AI-assisted analysis of peass-ng/PEASS-ng@53fb989abc (2026-09-02). Data as JSON: /api/errors/6a4bb19edbadb97d. Report an issue: GitHub.