Board logo

标题: [文件操作] 提权删除用户文件时,如何获取当前域用户名称 [打印本页]

作者: sl28001214    时间: 2018-3-8 11:43     标题: 提权删除用户文件时,如何获取当前域用户名称

本帖最后由 pcl_test 于 2018-3-9 20:56 编辑

提权删除用户文件时,遇到的一个无奈的问题
具体描述一下:
1、C:\user文件夹下有诸多文件,我只需要保留当前域用户文件夹(%username%)、本地管理员(adminad)
2、需要用户环境执行批处理,所以想到了提权。
3、runas提权过后,%username%的值变成了adminad,而不是用户的域账户名称了。所以会出错。

新菜求解释,如何处理。
  1. @echo off
  2. ::for /d %%i in (C:\Users\*) do if /i "%%~ni" neq "%username%" if /i "%%~ni" neq "adminad"  rd /s /q "%%i"
  3. pause
复制代码

作者: ivor    时间: 2018-3-8 17:52

for 循环里面要开启变量延迟
作者: ivor    时间: 2018-3-8 18:05

本帖最后由 ivor 于 2018-3-8 20:46 编辑

1.following registry key should be remove:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList


2.Use My Computer or Windows Explorer to remove the appropriate Windows\Profiles\User name folders. To remove all profiles, remove the Windows\Profiles folder.


所以说注册表你也要操作,否则会出先下次登陆无法准备桌面
作者: sl28001214    时间: 2018-3-8 18:12

回复 3# ivor
对,注册表得同步删除,否则就会临时文件登录或者无法加载配置文件。
我要通过runas调用,for开启变量延迟可以实现么?
作者: ivor    时间: 2018-3-8 19:10

你可以通过传递参数%1 获取标准用户名
作者: ivor    时间: 2018-3-8 22:06

本帖最后由 ivor 于 2018-3-9 09:30 编辑
  1. using System;
  2. using System.Diagnostics;
  3. using System.Security;
  4. using System.IO;
  5. using Microsoft.Win32;
  6. /**
  7.     author:ivor
  8.     提权调用自己然后按条件删除C:\\Users子文件夹 和 ProfileList 子项
  9.     */
  10. namespace UpPrivilege
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             if (args.Length == 0)
  17.             {
  18.                 Lanch(Environment.UserName, Environment.CommandLine);
  19.             }
  20.             else
  21.             {
  22.                 DeleteUserProfile(args[0]);
  23.                 Console.Write("press any key to exit....");
  24.                 Console.ReadKey();
  25.             }
  26.         }
  27.         public static void Lanch(String domainUser, String file)
  28.         {
  29.             /**
  30.                 提权运行
  31.                 测试域local.domain用户test密码test
  32.             */
  33.             ProcessStartInfo psi = new ProcessStartInfo();
  34.             psi.Domain = "local.domain";
  35.             psi.UserName = "test";
  36.             psi.Password = ConvertToSecuretString("test");
  37.             psi.FileName = file;
  38.             psi.Arguments = domainUser;
  39.             psi.UseShellExecute = false;
  40.             Process p = new Process();
  41.             p.StartInfo = psi;
  42.             p.Start();
  43.         }
  44.         public static SecureString ConvertToSecuretString(String passWord)
  45.         {
  46.             /**
  47.                 加密密码字符串
  48.             */
  49.             if (passWord == null)
  50.                 throw new ArgumentNullException("passWord");
  51.             unsafe
  52.             {
  53.                 fixed (char* passwordChars = passWord)
  54.                 {
  55.                     SecureString securePassword = new SecureString(passwordChars, passWord.Length);
  56.                     securePassword.MakeReadOnly();
  57.                     return securePassword;
  58.                 }
  59.             }
  60.         }
  61.         public static void DeleteUserProfile(String domainUser)
  62.         {
  63.             /**
  64.                 删除用户文件夹
  65.             */
  66.             const String REG_PATH = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList";
  67.             const String USERS_FOLDER = "C:\\Users";
  68.             DirectoryInfo dir = new DirectoryInfo(USERS_FOLDER);
  69.             DirectoryInfo[] dirs = dir.GetDirectories();
  70.             foreach (DirectoryInfo d in dirs)
  71.             {
  72.                 if (d.Name != domainUser & d.Name != "adminad")
  73.                 {
  74.                     Console.WriteLine("{0} is deleting!", d.Name);
  75.                     Console.WriteLine(d.FullName);
  76.                     Console.ReadKey();
  77.                     d.Delete(true);
  78.                     DeleteProfileList(REG_PATH, d.FullName);
  79.                 }
  80.             }
  81.         }
  82.         public static bool DeleteProfileList(String profileList, String domainUser)
  83.         {
  84.             /**
  85.                 清理注册表
  86.             */
  87.             String subkey;
  88.             RegistryKey subReg;
  89.             RegistryKey key = Registry.LocalMachine;
  90.             RegistryKey myreg = key.OpenSubKey(profileList, true);
  91.             Console.WriteLine(profileList);
  92.             foreach (var k in myreg.GetSubKeyNames())
  93.             {
  94.                 Console.WriteLine("=========items=========");
  95.                 Console.WriteLine(k);
  96.                 Console.WriteLine("=========items=========");
  97.                 subkey = String.Format("{0}\\{1}", profileList, k);
  98.                 subReg = key.OpenSubKey(subkey);
  99.                 foreach (var sk in subReg.GetValueNames())
  100.                 {
  101.                     if (sk == "ProfileImagePath")
  102.                     {
  103.                         String ProfileImagePath = subReg.GetValue(sk).ToString();
  104.                         if (ProfileImagePath == domainUser)
  105.                         {
  106.                             Console.WriteLine(ProfileImagePath + "\n");
  107.                             myreg.DeleteSubKeyTree(k);
  108.                             return true;
  109.                         }
  110.                     }
  111.                 }
  112.             }
  113.             return false;
  114.         }
  115.     }
  116. }
复制代码

作者: sl28001214    时间: 2018-3-9 09:50

回复 6# ivor 老铁你简直6的飞起。批处理估计实现这个功能比较费劲。C/C++也是个不错的选择。谢谢老铁。




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2