Sign in to follow this  
Followers 0
NerdFencer

Post-Op

2 posts in this topic

#1 ·  Posted (edited)

This is a quick tool that I wrote for 2 reasons.

1) to have fun parsing strings

2) because I wish the obfuscator would do this

This script is designed to be run AFTER the obfuscator has been run with the options "/cs=0 /cn=0 /cf=1 /cf=1 /sf=1 /sv=1" and has not been tested on anything else.

It does a few fun things to reduce script size.

1) finds all global const numeric values and inlines them (also works for several levels of const ex: BitOr($SomeConst,SomeNumber) )

2) renames variables to be short

3) removes some more excess white space

My test script went from 22.3 kb (after obfuscator) to 13.0 kb

I realize that this wont have a drastic effect on executable size, but its fun to mess around with.

Possibly up next...

1) Simplifying math expressions in code ex: BitOr(WhatUsedToBeAConst,SomeRandomNumbers)

2) Shortening function names

Global $file = "Your Script Here.au3"

FileDelete("out.au3")
Global $isworking = True
Global $work = FileRead($file)
While $isworking==True
    $work = PostOpConst($work)
WEnd
$work = PostOpVarNames($work)
While 1
    $work = StringReplace($work,@CR&@CR,@CR)
    If @extended==0 Then
        If StringRight($work,1)==@CR Then
            $work = StringTrimRight($work,1)
        EndIf
        ExitLoop
    EndIf
WEnd
FileWrite("out.au3",$work)

Func PostOpVarNames($text)
    Local $fvars[1], $fto[1], $state = 0, $var, $found, $last = ""
    Local $chars = StringSplit($text,"",2)
    Local $out = ""
    For $char In $chars
        Switch $state
            Case 0
                $out &= $char
                If $char=="'"Then
                    $state = 1
                    ContinueLoop
                ElseIf $char=='"' Then
                    $state = 2
                    ContinueLoop
                ElseIf $char=="$" Then
                    $state = 3
                    $var = "$"
                    ContinueLoop
                ElseIf $char==","Then
                    $state = 4
                    ContinueLoop
                EndIf
            Case 1
                $out &= $char
                If $char=="'" Then
                    $state = 0
                    ContinueLoop
                EndIf
            Case 2
                $out &= $char
                If $char=='"' Then
                    $state = 0
                    ContinueLoop
                EndIf
            Case 3
                If NotNameChar($char) Then
                    $found = False
                    For $i=0 To UBound($fvars)-2
                        If $var == $fvars[$i] Then
                            $found = True
                            $out &= $fto[$i]
                            ExitLoop
                        EndIf
                    Next
                    If Not($found) Then
                        $last = GetNext($last)
                        $fto[$i] = $last
                        $out &= $last
                        $fto[UBound($fto)-1] = $last
                        $fvars[UBound($fvars)-1] = $var
                        ReDim $fto[UBound($fto)+1]
                        ReDim $fvars[UBound($fvars)+1]
                    EndIf
                    $state = 0
                    If $char == " " Then
                        $state = 4
                        ContinueLoop
                    EndIf
                    $out &= $char
                    If $char == "," Then
                        $state = 4
                        ContinueLoop
                    EndIf
                EndIf
                $var &= $char
            Case 4
                If $char==" " Then
                    ContinueLoop
                ElseIf $char=="=" Then
                    $out &= $char
                    ContinueLoop
                ElseIf $char=="_" Or Not(NotNameChar($char)) Then
                    $out&=" "&$char
                    $state = 0
                    ContinueLoop
                EndIf
                $out &= $char
                $state = 0
                If $char=="'" Then $state = 1
                If $char=='"' Then $state = 2
                If $char=="$" Then
                    $state = 3
                    $var = "$"
                EndIf
        EndSwitch
    Next
    Return $out
EndFunc

Func NotNameChar($char)
    $char = Asc($char)
    If $char>=Asc("0") And $char<=Asc("9") Then Return False
    If $char>=Asc("A") And $char<=Asc("Z") Then Return False
    If $char>=Asc("a") And $char<=Asc("z") Then Return False
    Return True
EndFunc

Func GetNext($string)
    If $string=="" Then Return "A"
    Local $last = Asc(StringRight($string,1))
    If $last>=Asc("A") And $last<Asc("Z") Then
        Return StringTrimRight($string,1)&Chr($last+1)
    ElseIf $last==Asc("Z") Then
        Return StringTrimRight($string,1)&"0"
    ElseIf $last>=Asc("0") And $last<Asc("9") Then
        Return StringTrimRight($string,1)&Chr($last+1)
    ElseIf $last==Asc("9") Then
        Return $string&"A"
    EndIf
    Return $string&"A"
EndFunc

Func PostOpConst($text)
    Local $lines = StringSplit(StringReplace($text,@LF,""),@CR)
    Local $var, $elements, $stage, $line, $canuse
    Local $fvars[1], $flines[1], $fvals[1]
    For $i=1 To $lines[0]
        $line = $lines[$i]
        If StringLeft($line,12)=="Global Const" Then
            ; extract the variable name
            $var = ""
            $stage = False
            $line = StringTrimLeft($line,13)
            $elements = StringSplit($line,"",2)
            For $element In $elements
                If $element==" " Or $element=="=" Then
                    ExitLoop
                Else
                    $var&=$element
                EndIf
            Next
            $line = StringTrimLeft($line,3+StringLen($var))
            ; see if we can use it
            $line = strip($line)
            If Number($line)<>0 Or $line=="0" Or StringLeft($line,2)=="0x" Then
                $fvars[UBound($fvars)-1]  = $var
                $flines[UBound($flines)-1]= $i
                ; bugfix... stupid but needed
                If StringInStr($line,"+")==0 And StringInStr($line,"-")==0 Then
                    $fvals[UBound($fvals)-1] = Number($line)
                Else
                    $fvals[UBound($fvals)-1] = Execute($line)
                EndIf
                ReDim $fvars[UBound($fvars)+1]
                ReDim $flines[UBound($flines)+1]
                ReDim $fvals[UBound($fvals)+1]
            ElseIf StringLeft($line,6)=="BitOR(" And CanCondenseBitOR($line) Then
                $fvars[UBound($fvars)-1]  = $var
                $flines[UBound($flines)-1]= $i
                $fvals[UBound($fvals)-1]  = Execute($line)
                ReDim $fvars[UBound($fvars)+1]
                ReDim $flines[UBound($flines)+1]
                ReDim $fvals[UBound($fvals)+1]
            ElseIf StringLeft($line,9)=="BitShift(" And CanCondenseBitShift($line) Then
                $fvars[UBound($fvars)-1]  = $var
                $flines[UBound($flines)-1]= $i
                $fvals[UBound($fvals)-1]  = Execute($line)
                ReDim $fvars[UBound($fvars)+1]
                ReDim $flines[UBound($flines)+1]
                ReDim $fvals[UBound($fvals)+1]
            EndIf
        EndIf
    Next
    For $i=0 To UBound($fvals)-2
        $text = StringReplace($text,$fvars[$i],$fvals[$i])
    Next
    $lines = StringSplit(StringReplace($text,@LF,""),@CR)
    $text = ""
    For $i=1 To $lines[0]
        $canuse = True
        For $j=0 To UBound($fvals)-2
            If $flines[$j]==$i Then
                $canuse = False
                ExitLoop
            EndIf
        Next
        If $canuse == True Then
            $text &= $lines[$i] & @CR
        EndIf
    Next
    If UBound($fvals) <= 1 Then
        $isworking = False
    EndIf
    Return $text
EndFunc
Func CanCondenseBitOR($string)
    Local $tmp = StringTrimLeft(StringTrimRight($string,1),6)
    If StringInStr($tmp,"(")==0 And StringInStr($tmp,")")==0 And StringInStr($tmp,"$")==0 Then
        Return @error==0
    EndIf
    Return False
EndFunc
Func CanCondenseBitShift($string)
    Local $tmp = StringTrimLeft(StringTrimRight($string,1),9)
    If StringInStr($tmp,"(")==0 And StringInStr($tmp,")")==0 And StringInStr($tmp,"$")==0 Then
        Return @error==0
    EndIf
    Return False
EndFunc
Func strip($string)
    Local $out = "", $state = 0
    $string = StringSplit($string,"",2)
    For $char In $string
        Switch $state
            Case 0
                If $char=="'" Or $char=='"' Then
                    $state = 1
                    $out &= $char
                    ContinueLoop
                EndIf
                If $char==" " Or $char==@TAB Then
                    ContinueLoop
                EndIf
                If $char==";" Then
                    ExitLoop
                EndIf
                $out &= $char
            Case 1
                $out &= $char
                If $char=="'" Or $char=='"' Then
                    $state = 0
                    ContinueLoop
                EndIf
        EndSwitch
    Next
    Return $out
EndFunc

Edit: fixed something stupid that I did for debugging

Happy Scripting :mellow:

Edited by NerdFencer

_________[u]UDFs[/u]_________-Mouse UDF-Math UDF-Misc Constants-Uninstaller Shell

Share this post


Link to post
Share on other sites



Make a list of funcs that always return the same when they are given the same parameters (constant values) and does not change the environment:

* All maths except random

* Asc and Chr

* Hex and Dec

* All string functions.

* Is*

For example, this should happen:

StringTrimLeft("My String", StringLen(Chr(65) & Chr(32) & Chr(66)))

Becomes

"String"

That would be amazing :mellow:

Mat

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0