Ps2Ducky.psm1

function Invoke-Ps2Ducky {
<#
.SYNOPSIS
   Powershell to DuckyScript Converter
.DESCRIPTION
   This script will take a powershell script and convert it to ducky script ready to run on Hak5 devices
.PARAMETER Script
   The powershell script you want to convert
.PARAMETER Output
   The file path you want to save your payload to
.PARAMETER Payload
   Name of your payload, will populate in the description
.PARAMETER Author
   Payload Author, will populate in the description
.PARAMETER TargetOS
   Operating System being targeted, will populate in the description
.PARAMETER Description
   Description of payload, will populate in the description
.PARAMETER b64
   Switch paramter to indicate whether you want to encode your script in base64 before converting to duckyscript
.EXAMPLE
   Example of how to use this cmdlet
.EXAMPLE
   Another example of how to use this cmdlet
.LINK
   Related links
#>

    param(
        [Parameter(Mandatory=$False)]
        [string]$Script,
        [Parameter(Mandatory=$False)]
        [string]$Output,
        [Parameter(Mandatory=$False)]
        [string]$Payload,
        [Parameter(Mandatory=$False)]
        [string]$Author,
        [Parameter(Mandatory=$False)]
        [string]$TargetOS,
        [Parameter(Mandatory=$False)]
        [string]$Description,
        [Parameter(Mandatory=$False)]
        [switch]$b64
    )

    if ($args.Count -eq 0) {displayMainScreen -Display $banner}

    $content = Get-Content $Script -Raw

    # Remove multi-line comments
    $content = [regex]::Replace($content, '<#.*?#>', '', [System.Text.RegularExpressions.RegexOptions]::Singleline)

    # Remove single-line comments
    $content = [regex]::Replace($content, '#.*', '')

    # Minify the Script and offer Base64 option
    if($b64){

        $content = Get-Content -Path $Script | Where-Object { $_ -match '\S' }
        $contentBytes = [System.Text.Encoding]::UTF8.GetBytes($content)
        $content = [System.Convert]::ToBase64String($contentBytes)

        # Initialize an empty array to hold the lines
    $lines = @()

    # Loop to break the string into lines of at most 260 characters
    while ($content.Length -gt 0) {
        # Determine the length for this line
        $length = [Math]::Min(260, $content.Length)

        # Extract the line
        $line = $content.Substring(0, $length)

        # Add the line to the array
        $lines += $line

        # Remove the line from the content
        $content = $content.Substring($length)
    }

    # Now $lines is an array of strings, each string is at most 260 characters long
    echo $lines > $Output
    }
    else {echo $content > $Output}

    $Delay = 250
    $file = Get-Content -Path $Output | Where-Object { $_ -match '\S' } 
    $file = $file.Trim()
    $FilePath = $Output

    echo "" > $FilePath
    echo "REM This payload was generated using I am Jakoby's Powershell to DuckyScript Converter." >> $FilePath
    echo "REM See how you can do the same here: https://github.com/I-Am-Jakoby/PowerShell-for-Hackers `n" >> $FilePath

    if ($Payload)      { echo "REM Title: $Payload"        >> $FilePath}
    if ($Author)       { echo "REM Author: $Author"         >> $FilePath}
    if ($TargetOS)     { echo "REM Target OS: $TargetOS"       >> $FilePath}
    if ($Description)  { echo "REM Description: $Description"    >> $FilePath}

    $runPowershell = @"
    DELAY 2000
    GUI r
    DELAY 2000
    STRING powershell
    DELAY $Delay
    ENTER
    DELAY 2000
"@


echo $runPowershell >> $FilePath

    foreach ($line in $file) {
        $line.trim()
        echo "STRING $line `nDELAY 250" >> $FilePath
    }

    echo "ENTER" >> $FilePath
    notepad $FilePath
}



$version = "1.7"
$version = $version.PadRight(13, " ")+'║'

$banner = @"
╔═════════════════════════════════════════════════════════════════════════╦════════════════════════╗
║+@@@@@@@@@@+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ║ Powershell to ║
║+++@@@@@@@@@@++++++++++++++++++++++++++++++,-.++++++++++++++++++++++++ ║ Ducky Converter ║
║+++++@@@@@@@@@@++++++++++++++++++++++++,--' -.).++++++++++++++++++++++ ║ ║
║+++++++@@@@@@@@@@++++++++++++++++++++,' .+++++++++++++++++++++ ║ Created By: ║
║+++++++++@@@@@@@@@@+++++++++++++++++; (((__ __)))+++++++++++++++++++ ║ I am Jakoby ║
║+++++++++++@@@@@@@@@@+++++++++++++++; ( (#)~( (#)~~~~~~~~~~~~~~~~~~~~ ║────────────────────────║
║+++++++++++++@@@@@@@@@@+++++++++++++| \_/___\_/|++++++++++++++++++++ ║ [P]ayload Generator ║
║+++++++++++++++@@@@@@@@@@++++++++++," ,-' __".++++++++++++++++++++ ║ ║
║+++++++++++++++++@@@@@@@@@++++++++( ( ._ ____.)--._++++++++_++++++ ║ [O]pen Demo (coming) ║
║+++++++++++++++@@@@@@@@@+++++++++++._ '-.'-' \('-' _ -.',_,-'', ,-+ ║ ║
║+++++++++++++@@@@@@@@@++++++++++++++,') '.'._)) ,' ._ . ,','; ;+ ║ [S]upport ║
║+++++++++++@@@@@@@@@++++++++++++++.' . '--' / ). . ;+ ║ ║
║+++++++++@@@@@@@@@+++++++++++++++; - / ' ) ;++ ║ [H]elp ║
║+++++++@@@@@@@@++@@@@@@@@@@@@@@++\ ') ,'++ ║ ║
║++++++@@@@@@@@+++@@@@@@@@@@@@@@+++\ ,' ;++++ ║ [X] - exit ║
║++++@@@@@@@@+++++@@@@@@@@@@@@@@++++\ ---' ,'++++++ ║ ║
║++@@@@@@@@++++++++++++++++++++++++++. _,'++++++++ ║────────────────────────║
║@@@@@@@@++++++++++++++++++++++++++++++. ,--'+++++++++++ ║ Version: $version
╚═════════════════════════════════════════════════════════════════════════╩════════════════════════╝
"@



$displayHelp = @"
╔═════════════════════════════════════════════════════════════════════════╦════════════════════════╗
║ ║ Powershell to ║
║ invoke-ps2Ducky -Script -Output -Payload ║ Ducky Converter ║
║ -Author -TargetOS -Description -b64 ║ ║
║ ║ Created By: ║
║ ║ I am Jakoby ║
║ Title : Enumerate ║────────────────────────║
║ Author : Jakoby ║ ║
║ Target OS : Windows ║ ║
║ Description: Enumerates something ║ [M]ore Help ║
║ ║ (Open GitHub) ║
║ Script = Powershell script to wish to convert ║ ║
║ Output = Path of text file ducky script is saved to ║ ║
║ Payload = The Name of your payload ║ ║
║ Author = Your name ║ ║
║ TargetOS = The Name of your payload ║ ║
║ Descripton = Brief description of your payload ║ ║
║ ║ Press Enter to Return ║
║ b64 = Switch to encode output in Base 64 ║ ║
║ ║────────────────────────║
║ ║ Version: $version
╚═════════════════════════════════════════════════════════════════════════╩════════════════════════╝
"@



$displaySupport = @"
╔═════════════════════════════════════════════════════════════════════════╦════════════════════════╗
║ ║ Powershell to ║
║ invoke-ps2Ducky -Script -Output -Payload ║ Ducky Converter ║
║ -Author -TargetOS -Description -b64 ║ ║
║ ║ Created By: ║
║ ║ I am Jakoby ║
║ Title : Enumerate ║────────────────────────║
║ Author : Jakoby ║ ║
║ Target OS : Windows ║ ║
║ Description: Enumerates something ║ [G]itHub ║
║ ║ ║
║ Script = Powershell script to wish to convert ║ [D]iscord ║
║ Output = Path of text file ducky script is saved to ║ ║
║ Payload = The Name of your payload ║ [Y]outube ║
║ Author = Your name ║ ║
║ TargetOS = The Name of your payload ║ [T]witter ║
║ Descripton = Brief description of your payload ║ ║
║ ║ Press Enter to Return ║
║ b64 = Switch to encode output in Base 64 ║ ║
║ ║────────────────────────║
║ ║ Version: $version
╚═════════════════════════════════════════════════════════════════════════╩════════════════════════╝
"@


function displayMainScreen {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$False)]
        [string]$Display,
        [Parameter(Mandatory=$False)]
        [string]$red
    )    
    Clear-Host
    foreach ($char in $Display.ToCharArray()) {
        if ($red -and $char -match '#') {
            Write-Host $char -NoNewline -ForegroundColor Red
        }        
        elseif ($char -match '[A-Z]') {
            Write-Host $char -NoNewline -ForegroundColor White
        }
        else {
            switch ($char) {
                '+' { Write-Host $char -NoNewline -ForegroundColor DarkBlue }
                '@' { Write-Host $char -NoNewline -ForegroundColor Cyan }
                '╔' { Write-Host $char -NoNewline -ForegroundColor Red }
                '╗' { Write-Host $char -NoNewline -ForegroundColor Red }
                '╚' { Write-Host $char -NoNewline -ForegroundColor Red }
                '╝' { Write-Host $char -NoNewline -ForegroundColor Red }
                '═' { Write-Host $char -NoNewline -ForegroundColor Red }
                '║' { Write-Host $char -NoNewline -ForegroundColor Red }
                '─' { Write-Host $char -NoNewline -ForegroundColor Red } 
                '~' { Write-Host $char -NoNewline -ForegroundColor Red }  
                '╩' { Write-Host $char -NoNewline -ForegroundColor Red } 
                '╦' { Write-Host $char -NoNewline -ForegroundColor Red }                   
                default { Write-Host $char -NoNewline -ForegroundColor DarkYellow }
            }
        }
    }



$decision = Read-Host -Prompt "`nWhat would you like to do?"


        switch ($decision) {
            'P' { invoke-ps2Ducky }
            'O' { invoke-ps2Ducky }
            'S' { Clear-Host; displayMainScreen -Display $displaySupport }  
            'H' { Clear-Host; displayMainScreen -Display $displayHelp } 
            'X' { exit } 
            'M' { Start-Process "https://github.com/I-Am-Jakoby/Powershell-to-Ducky-Converter"; displayMainScreen -Display $displayHelp}
            'G' { Start-Process "https://github.com/sponsors/I-Am-Jakoby"; displayMainScreen -Display $displaySupport} 
            'D' { Start-Process "https://discord.com/servers/i-am-jakoby-495265922135621632"; displayMainScreen -Display $displaySupport} 
            'Y' { Start-Process "https://youtube.com/@iamjakoby?sub_confirmation=1"; displayMainScreen -Display $displaySupport} 
            'T' { Start-Process "https://twitter.com/I_Am_Jakoby"; displayMainScreen -Display $displaySupport}              
            default { Invoke-Ps2Ducky }
        }

}