简体   繁体   中英

Running powershell scripts with python under one session

I try to create a python program which will deobfuscate powershell malware, which uses IEX. My python program is actually hooking the IEX function and instead of running the desired string, it will print the string. Now my problem is that I have some .ps1 scripts (for examples 1.ps1, 2.ps1, etc..) and I want to run all of them under the same session so that by this, all the local variables created by 1.ps1 script, the 2.ps1 script will be able to use...

Now I tried so many ways, First I tried with subprocess but it always creates a new session for every time I enter a command (which is the path of the .ps1 file). Then I found this project at GitHub: https://gist.github.com/MarkBaggett/a7c10195b2626c78009bf73bcdb6db20 Which is really awesome and did work but still, it seems that when I run the command ./1.ps1 it still does not store the local variables at the session (Maybe it opens a new one when running a script). I tried to do also "Get-Content 1.ps1 | iex" but then it crashes since I have functions there for example:

function Invoke-Expression()
{
    param(
        [Parameter( `
            Mandatory=$True, `
            Valuefrompipeline = $True)]
        [String]$Command
    )
    Write-Host $Command
}

taken from PSDecode project: https://github.com/R3MRUM/PSDecode/blob/master/PSDecode.psm1#L28

Anyway, any ideas about how I can do this? I have those scripts on my desktop but no idea how to run them at the same session so they will use the same local variables...

Two things that I did though but they really suck: 1. Convert all the scripts to 1 script and run it, but in next run that I will use this program I might have 100 scripts or more and I don't really want to do this. 2. I can save the local variables from each script and load it to another yet I want to use it in the worst case scenario and still didn't get there.

Thank you so much for helping me and sorry for my grammar my English is not my mother language as you can see :)

Maybe you're looking for dot sourcing :

Runs a script in the current scope so that any functions, aliases, and variables that the script creates are added to the current scope. PowerShell

. c:\\scripts\\sample.ps1

If so dot-source your ps1 files, and call the functions inside them.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM