PowerShell 技能连载 - 元宇宙虚拟资产自动化管理系统

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function Manage-MetaverseAssets {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$AssetType,

[ValidateSet('Create','Update')]
[string]$Operation = 'Create'
)

$assetReport = [PSCustomObject]@{
Timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
TotalAssets = 0
OperationLogs = @()
PermissionChanges = @()
}

try {
# 元数据模板配置
$metadataTemplate = @{
NFT = @{ Properties = @('Owner','CID','Royalties') }
Avatar = @{ Properties = @('ModelID','Inventory','Permissions') }
Land = @{ Properties = @('Coordinates','Terrain','BuildHeight') }
}

# 执行资产操作
switch ($Operation) {
'Create' {
$newAsset = [PSCustomObject]@{
Type = $AssetType
Metadata = $metadataTemplate[$AssetType]
Created = Get-Date
}
$assetReport.OperationLogs += $newAsset
}
'Update' {
$updatedAsset = [PSCustomObject]@{
Type = $AssetType
Modified = Get-Date
PermissionUpdates = (Get-Random -Minimum 1 -Maximum 5)
}
$assetReport.PermissionChanges += $updatedAsset
}
}

# 统计资产总量
$assetReport.TotalAssets = (Get-ChildItem "HKLM:\SOFTWARE\MetaverseAssets\$AssetType" -Recurse).Count
}
catch {
Write-Error "资产管理操作失败: $_"
}

# 生成XRSF格式报告
$assetReport | ConvertTo-Json | Out-File -Path "$env:TEMP/MetaverseReport_$(Get-Date -Format yyyyMMdd).json"
return $assetReport
}

核心功能

  1. 多类型数字资产模板管理
  2. 元数据版本控制系统
  3. 权限变更追踪审计
  4. XRSF格式交互报告

应用场景

  • 虚拟经济系统构建
  • NFT资产批量发行
  • 元宇宙土地资源分配
  • 跨平台资产迁移管理

PowerShell实现供应链安全自动化扫描

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function Invoke-ModuleVulnerabilityScan {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$ModuleName
)

# 获取模块版本信息
$module = Get-InstalledModule -Name $ModuleName -ErrorAction Stop

# 调用漏洞数据库API
$response = Invoke-RestMethod -Uri "https://vulndb.example.com/api/modules/$($module.Name)/$($module.Version)"

# 生成安全报告
[PSCustomObject]@{
ModuleName = $module.Name
Version = $module.Version
Vulnerabilities = $response.vulns.Count
Critical = $response.vulns | Where-Object { $_.severity -eq 'Critical' } | Measure-Object | Select-Object -Expand Count
LastUpdated = $module.PublishedDate
} | Export-Csv -Path "$env:TEMP\ModuleSecurityScan_$(Get-Date -Format yyyyMMdd).csv" -Append
}

# 扫描常用模块
'PSReadLine', 'Pester', 'Az' | ForEach-Object {
Invoke-ModuleVulnerabilityScan -ModuleName $_ -Verbose
}

核心功能:

  1. 自动化检测已安装PowerShell模块版本
  2. 对接漏洞数据库API进行安全检查
  3. 生成包含严重性等级的安全报告

扩展方向:

  1. 集成软件物料清单(SBOM)生成
  2. 添加自动补丁更新功能
  3. 与CI/CD流水线集成实现预发布扫描

用 PowerShell 重新打包 0day appz

从 0day 服务器下载下来的 appz 文件夹是这样的形态:

每个文件夹代表一个 appz 软件,打开是这个样子的:

里面是一系列 .zip 文件以及说明文件。这些 .zip 文件却不是使用 zip 的分卷压缩出来的,它们的内容如下:

要把这些 .zip 文件全部解压到同一个目录下,才可以得到一系列 rar 的分卷压缩文件。我们打开一个 .rar 文件,这才看到真正的内容:

软件数量大的时候,人工重复进行上述操作就不合适了。机械的劳动应该交给程序。我们可以设计一个 PowerShell 脚本,完成一系列功能:

  • 遍历 0day appz 的下载目录。
  • 解压所有 .zip 文件。
  • 解压 .rar 文件。
  • 将说明文件复制到一起。
  • 将最终的文件重打包为 .zip 文件。
  • 如果上述的解压有问题,则不打包,并输出错误日志。
  • 清理临时文件。
  • 清理成功的原始文件夹,保留失败的原始文件夹。

按照这个需求,我们可以编写如下 PowerShell 脚本:

$DebugPreference = 'Continue'

$incoming = 'd:\0day\incoming'
$temp1 = 'd:\0day\temp1'
$temp2 = 'd:\0day\temp2'
$output = 'd:\0day\output'

if (Test-Path $temp1) { del $temp1 -r }
if (Test-Path $temp2) { del $temp2 -r }

$apps = dir $incoming -Directory
$count = 0
$hasFailed = $false
$apps | foreach {
    $name = $_.Name
    Write-Progress -Activity 'Repacking apps' -PercentComplete ($count / $apps.Length * 100) -CurrentOperation $name
    echo "Repacking $name"

    md $temp1 | Out-Null
    md $temp2 | Out-Null

    # d:\0day\util\7z x -o"d:\0day\temp1" "d:\0day\incoming\VanDyke.SecureCRT.v7.2.2.491.Incl.Patch.And.Keymaker-ZWT\*.zip"
    $arguments = 'x', "-o""$temp1""", '-y', (Join-Path $_.FullName *.zip)
    .\7z $arguments | Out-Null

    if (!$?) {
        Write-Warning "Repacking $name failed."
        echo "$name" >> "$output\fail.log"

        del $temp1 -r
        del $temp2 -r

        $count++
        $hasFailed = $true
        return
    }

    # d:\0day\util\7z x -o"d:\0day\temp2" "d:\0day\temp1\*.rar" -y
    $arguments = 'x', "-o""$temp2""", '-y', "$temp1\*.rar"
    .\7z $arguments | Out-Null
    if (!$?) {
        Write-Warning "Repacking $name failed."
        echo "$name" >> "$output\fail.log"

        del $temp1 -r
        del $temp2 -r

        $count++
        $hasFailed = $true
        return
    }

    # copy d:\0day\temp1\*.diz d:\0day\temp2
    # copy d:\0day\temp1\*.nfo d:\0day\temp2

    dir $temp1 | where {
        $_.Extension -notmatch 'rar|r\d*'
    } | copy -Destination $temp2

    #d:\0day\util\7z a "d:\0day\output\VanDyke.SecureCRT.v7.2.2.491.Incl.Patch.And.Keymaker-ZWT.zip" "d:\0day\temp2\*.*" -r
    $arguments = 'a', "$output\$name.zip", "$temp2\*.*", '-r'
    .\7z $arguments | Out-Null
    if (!$?) {
        Write-Warning "Repacking $name failed."
        echo "$name" >> "$output\fail.log"

        del $temp1 -r
        del $temp2 -r

        $count++
        $hasFailed = $true
        return
    }

    del $temp1 -r
    del $temp2 -r

    Remove-Item -LiteralPath $_.FullName -r

    $count++
}

if ($hasFailed) {
    echo '' >> "$output\fail.log"
}

echo 'Press any key to continue...'
[Console]::ReadKey() | Out-Null

# del 'd:\0day\output\*.*' -r

您也可以在这里下载写好的脚本,包括完整的目录结构和 7z 软件包。请解压到 d:\ 中使用,或者自行调整脚本头部的路径。

用脚本批量下载www.cheat-sheets.org中的所有pdf文件

流水不腐,户枢不蠹。虽然批量下载有很多工具能做到,但是为了提高,我们尽量动手编写脚本吧。
http://www.cheat-sheets.org 里有很多好东西,我们把它批量下载下来。

下载的PDF截图

PowerShell代码:

Add-Type -AssemblyName System.Web
$baseUrl = 'http://www.cheat-sheets.org'
$result = Invoke-WebRequest $baseUrl
$result.Links |
    ? {$_.href -Like '*.pdf'} |
    select -ExpandProperty href |
    sort |
    % {
        if ($_ -like '/*')
        {
            $baseUrl + $_
        } else {
            $_
        }
      } |
     % {
        echo "Downloading $_"
        $fileName = $_.Substring($_.LastIndexOf("/") + 1)
        $localFileName = [System.Web.HttpUtility]::UrlDecode($fileName)

        if (Test-Path $localFileName) {
            return
        }
        Invoke-WebRequest -Uri $_ -OutFile $localFileName
        if (Test-Path $localFileName) {
            Unblock-File $localFileName
        }
     }

批量更改csdn下载的文件名(UrlDecode)

例如csdn下载的一个文件名字为 %5B大家网%5DWindows.PowerShell应用手册%5Bwww.TopSage.com%5D.pdf,我们通过两行PowerShell脚本把它转化为正常的 [大家网]Windows.PowerShell应用手册[www.TopSage.com].pdf。量大的时候特别好用。

方法如下:

Add-Type -AssemblyName System.Web
dir | % { ren -LiteralPath $_ ([System.Web.HttpUtility]::UrlDecode($_)) }
PowerShell 技术 QQ 群