NetSPI / PowerUpSQL

PowerUpSQL: A PowerShell Toolkit for Attacking SQL Server
Other
2.47k stars 462 forks source link

PowerShell v2 compatibility #40

Closed BaffledJimmy closed 4 years ago

BaffledJimmy commented 5 years ago

Hi there,

When trying to import PowerUpSQL into a powershell -version 2 session, the user receives the following message:

ipmo 'E:\Pentest Tools\PowerUpSQL-master\PowerUpSQL.ps1'
- : You must provide a value expression on the right-hand side of the '-' operator.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedValueExpression

I'll have a look at working out the exact error line, but first inspection seems to be PS interpreting a '-' as a mathematical symbol rather than a hyphen.

Thanks :)

BaffledJimmy commented 5 years ago

Update - I've worked out that it is due to an error within the Test-Subnet function, I'll investigate that now :).

Update 2 - looks to be because PS v2 doesn't support bitshift / shift left functions etc.

nullbind commented 5 years ago

Thanks for figuring that out @BaffledJimmy, ill have to take a look at alternative options.

OJ commented 5 years ago

I just bashed my head against this as well. My fix:

function BitShift {
    param(
        [Parameter(Mandatory=$True,Position=0)]
        [int]$x,

        [Parameter(ParameterSetName='Left')]
        [int]$Left,

        [Parameter(ParameterSetName='Right')]
        [int]$Right
    )
    $shift = If($PSCmdlet.ParameterSetname -eq 'Left')
    {
        $Left
    }
    else
    {
        -$Right
    }
    return [Math]::Floor($x * [Math]::Pow(2, $shift))
}

# Source: http://www.padisetty.com/2014/05/powershell-bit-manipulation-and-network.html
# Notes: Changed name from checkSubnet to Test-Subnet (Approved Verbs)
function Test-Subnet ([string]$cidr, [string]$ip)
{
    $network, [int]$subnetlen = $cidr.Split('/')
    $a = [uint32[]]$network.split('.')
    [uint32] $unetwork = (BitShift $a[0] -Left 24) + (BitShift $a[1] -Left 16) + (BitShfit $a[2] -Left 8) + $a[3]

    $mask = BitShift (-bnot [uint32]0) -Left (32 - $subnetlen)

    $a = [uint32[]]$ip.split('.')
    [uint32] $uip = (BitShift $a[0] -Left 24) + (BitShift $a[1] -Left 16) + (BitShift $a[2] -Left 8) + $a[3]

    $unetwork -eq ($mask -band $uip)
}

Sorry for the lack of PR, feel free to shove it in at some point :)

nullbind commented 5 years ago

No worries, thanks so much for the help!

aph3rson commented 5 years ago

Just an FYI, this appears to be a clone of #32. There's an open PR (#33) that was filed to fix this issue.

BaffledJimmy commented 4 years ago

I think the latest version is now CLR v2 compliant so this can be closed :)