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,
        [Parameter(Mandatory=$False)]
        [Int]$defaultDelay='250'
    )

    if ($PSBoundParameters.Count -eq 0) {displayMainScreen -Display $banner; break}

    $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}

    $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}

echo "" >> $FilePath

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


echo $runPowershell >> $FilePath

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

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



$version = "0.0.2"
$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 ║
║ Links where you can show support for Jakoby's work: ║ Ducky Converter ║
║ ║ ║
║ You can become a monthly sponsor on GitHub ║ Created By: ║
║ ║ I am Jakoby ║
║ ║────────────────────────║
║ You can become a monthly sponsor on Discord ,d88b.d88b, ║ ║
║ 88888888888 ║ ║
║ 'Y8888888Y' ║ [G]itHub ║
║ Subscribe to my YouTube channel 'Y888Y' ║ ║
║ 'Y' ║ [D]iscord ║
║ /\/|_ __/\\ ║ ║
║ Follow me on Twitter / -\ /- ~\ ║ [Y]outube ║
║ \ = Y =T_ = / ║ ║
║ Luther )==*(' ') ~ \ Hobo ║ [T]witter ║
║ Luther and Hobo / \ / \ ║ ║
║ would appreciate you! | | ) ~ ( ║ Press Enter to Return ║
║ / \ / ~ \ ║ ║
║ \ / \~ ~/ ║────────────────────────║
║ \__ _/_/\_/\__~__/ ║ 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' { $pi = Get-payloadInfo 
            $script = Read-Host -Prompt "`nPath to your PS script: "
            $output = Read-Host -Prompt "`nPath where you want to save ducky script: "
            
            $Payload      = $pi.Payload
            $Author       = $pi.Author 
            $TargetOS     = $pi.TargetOS
            $Description  = $pi.Description
            $defaultDelay = $pi.defaultDelay
            
            if(!$pi.b64){
                Invoke-Ps2Ducky -Script "$script" -Output "$output" -Payload "$Payload" -Author "$Author" -TargetOS "$TargetOS" -Description "$Description" -defaultDelay "$defaultDelay"
                }
                
            elseif($pi.b64){
                Invoke-Ps2Ducky -Script "$script" -Output "$output" -Payload "$Payload" -Author "$Author" -TargetOS "$TargetOS" -Description "$Description" -defaultDelay "$defaultDelay"
                }                
                
            }
            '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 }
        }

}



function Get-payloadInfo {
Clear-Host
return (New-Object -TypeName PSObject -Property ($host.ui.Prompt("PAYLOAD INFO","These are optional:",@("Payload","Author","TargetOS","Description","b64","defaultDelay"))))
}