【问题标题】:Download web content through CMD通过 CMD 下载网页内容
【发布时间】:2016-04-24 21:13:55
【问题描述】:

我想通过某种方式在命令提示符窗口 (Windows CMD) 上获取在线内容。

想象一些在线内容存储在托管服务或该托管服务提供给您的 MySql 数据库中。它可以是以任何形式存储的任何数据。我只想在世界任何地方的 CMD 窗口的帮助下远程查看它。我应该使用什么命令?

为了更清楚,假设您有一天时间准备考试。你不是在为此做准备,而是在制定一个欺骗的计划。

现在您的考试将在分配给您的计算机上进行,您不得使用浏览器或在 PC 上下载任何新应用程序。但是您可以使用命令提示符,因此您的作弊任务是将答案放在网上的某个地方。你不能安装任何新的东西。

如果你被困在上面的场景中,你会怎么做?

P.S 这仅用于教育目的。我没有这样的考试。

【问题讨论】:

  • 这完全取决于内容的存储和访问方式。 CMD 知道如何从 SMB 网络共享 (\\computername\folder) 获取内容,但您需要运行程序才能访问大多数其他内容(即:用于数据库的 sqlcmd,或用于 Web 内容的 iexplore)
  • @RyanBemrose 想象我上传了一个文本文件 www.mywebsite.com/myfile.txt 我可以在 CMD 上查看这个文件的内容,或者在记事本中打开这个文件,除了不是浏览器或第三方应用程序
  • 如果不使用浏览器或其他程序(如 UnxUtils wget)下载页面,CMD 不支持它。如果您被允许运行 PowerShell,那么使用Invoke-Webrequest 可能会有一些运气。
  • @RyanBemrose 可以通过任何方式直接使用 CMD 下载 UncUtils,也可以说我可以使用 PowerShell,我该怎么做?
  • 检查this

标签: windows batch-file cmd


【解决方案1】:

这完全取决于内容的存储和访问方式。 CMD 知道如何从 SMB 网络共享 (\computername\folder) 获取内容,但您需要运行一个程序才能访问大多数其他内容。 (例如:数据库的 sqlcmd)

CMD 不支持下载网页内容。您将需要使用其他程序连接到网站并下载页面。显然浏览器可以工作。另一种选择是从UnxUtils 下载wget.exe。或者使用另一种脚本语言,例如 PowerShellWscript

如果您有权启动 PowerShell(预装在 Windows 7-10 上),则可以使用 .NET 库下载 Web 资源。

PS> Invoke-Webrequest 'www.mywebsite.com/myfile.txt' -OutFile '.\myfile.txt'

这将使用 .NET 连接到页面并将其下载到本地目录。要下载到另一个目录或文件名,请更改 -OutFile 参数。

要从 CMD 启动它,只需在 CMD 中键入 powershell 即可进入 PowerShell 提示符,然后从那里运行 PS 命令。或者,您可以使用 powershell -c 命令从 CMD 运行 PS 命令。

powershell.exe -c "invoke-webrequest 'www.mywebsite.com/myfile.txt' -outfile .\myfile.txt"

【讨论】:

    【解决方案2】:

    You can check this question。最简单的方法是使用bitsadmin 命令:

    bitsadmin /transfer myDownloadJob /download /priority normal http://downloadsrv/10mb.zip c:\10mb.zip
    

    你也可以试试winhttpjs.bat:

    call winhhtpjs.bat https://example.com/files/some.zip -saveTo c:\somezip.zip
    

    【讨论】:

      【解决方案3】:
      Set Arg = WScript.Arguments
      set WshShell = createObject("Wscript.Shell")
      Set Inp = WScript.Stdin
      Set Outp = Wscript.Stdout
      On Error Resume Next
          Set File = WScript.CreateObject("Microsoft.XMLHTTP")
          File.Open "GET", Arg(1), False
          File.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 1.1.4322; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C; .NET4.0E; BCD2000; BCD2000)"
          File.Send
          txt=File.ResponseText
          'Putting in line endings
          Outp.write txt
          If err.number <> 0 then 
              Outp.writeline "" 
              Outp.writeline "Error getting file" 
              Outp.writeline "==================" 
              Outp.writeline "" 
              Outp.writeline "Error " & err.number & "(0x" & hex(err.number) & ") " & err.description 
              Outp.writeline "Source " & err.source 
              Outp.writeline "" 
              Outp.writeline "HTTP Error " & File.Status & " " & File.StatusText
              Outp.writeline  File.getAllResponseHeaders
              Outp.writeline LCase(Arg(1))
          End If
      

      一般用途

      过滤器用于命令提示符。 Filter.vbs 必须与 cscript.exe 一起运行。如果您只是键入 filter,它将运行一个批处理文件,该文件将自动执行此操作。

      filter subcommand [parameters]
      

      过滤器仅读取和写入标准输入和标准输出。这些仅在命令提示符下可用。

      filter <inputfile >outputfile
      filter <inputfile | other_command
      other_command | filter >outputfile
      other_command | filter | other_command
      

      网络

      filter web webaddress
      filter ip webaddress
      

      从 Web 检索文件并将其写入标准输出。

      webaddress - a web address fully specified including http://
      

      示例

      获取微软的主页

      filter web http://www.microsoft.com
      

      使用https://onedrive.live.com/redir?resid=E2F0CE17A268A4FA!121&authkey=!AAFg7j814-lJtmI&ithint=folder%2cres 提供的 19 个示例命令提示符脚本进行过滤

      【讨论】:

        【解决方案4】:

        Windows 10 build 17063 及更高版本发布 curl.exe(参考:https://techcommunity.microsoft.com/t5/containers/tar-and-curl-come-to-windows/ba-p/382409)。假设您不需要支持早期的 Windows 版本,包括早期的 Windows 10 版本,您可以像这样在批处理文件中使用它

        curl http://example.com/ --output content.txt
        notepad content.txt
        

        【讨论】:

          【解决方案5】:

          除了 VBScript 和 JScripts,在 Windows 上还有一个实用程序(与 CMD 一起存在),可以从 CMD 运行(如果您有写入权限):

          set url=https://www.nsa.org/content/hl-images/2017/02/09/NSA.jpg
          set file=file.jpg
          certutil -urlcache -split -f %url% %file%
          

          Powershell 中的 Cmdlet:

          $url = "https://www.nsa.org/content/hl-images/2017/02/09/NSA.jpg"
          $file = "file.jpg"
          $ProgressPreference = "SilentlyContinue";
          Invoke-WebRequest -Uri $url -outfile $file
          

          PowerShell下的.Net:

          $url = "https://www.nsa.org/content/hl-images/2017/02/09/NSA.jpg"
          $file = "file.jpg"
          # Add the necessary .NET assembly
          Add-Type -AssemblyName System.Net.Http
          # Create the HttpClient object
          $client = New-Object -TypeName System.Net.Http.Httpclient
          $task = $client.GetAsync($url)
          $task.wait();
          [io.file]::WriteAllBytes($file, $task.Result.Content.ReadAsByteArrayAsync().Result)
          

          使用 csc.exe 在命令行上构建的 C# 应用程序:

          https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/command-line-building-with-csc-exe

          using System;
          using System.IO;
          using System.Net.Http;
          using System.Threading.Tasks;
          
          namespace DownloadImage
          {
              class Program
              {
                  static async Task Main(string[] args)
                  {
                      using var httpClient = new HttpClient();
                      var url = "https://www.nsa.org/content/hl-images/2017/02/09/NSA.jpg";
                      byte[] imageBytes = await httpClient.GetByteArrayAsync(url);
          
                      using var fs = new FileStream("file.jpg", FileMode.Create);
                      fs.Write(imageBytes, 0, imageBytes.Length);
          
                  }
              }
          }
          

          内置于 Windows 应用程序中。无需外部下载。

          在 Win 10 上测试

          【讨论】:

          • certutil 一个给了我“拒绝访问”(并且它不是对 %file% 的写访问权限被拒绝)
          • 默认情况下,只有 TrustedInstaller 可以访问-urlcache。您可以更改用户权限、获取文件所有权或编辑管理员组策略,或禁用 Windows Defender 或防病毒监控。当您拥有访问权限时,您的回复将类似于**** Online **** 000000 ... 05193f CertUtil: -URLCache command completed successfully. 而不是Access is denied. 您还可以finger 绕过Windows Defender 干扰通过C2 服务器下载文件。 LoLBins 可以绕过 UAC 和 WDAC。
          猜你喜欢
          • 2012-02-27
          • 2015-03-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-05
          • 1970-01-01
          • 1970-01-01
          • 2018-01-15
          相关资源
          最近更新 更多