-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_powersehll_command.py
More file actions
68 lines (58 loc) · 2.11 KB
/
Copy pathgenerate_powersehll_command.py
File metadata and controls
68 lines (58 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
###################
# This tool encodes PowerShell script to execute it in single command line (in memory).
# Copyright (C) 2023 Maurice Lambert
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
###################
from sys import argv, stdin, stderr, exit, stdout
from argparse import ArgumentParser, FileType
from base64 import b64encode
parser = ArgumentParser(
description="This script generates a powershell command to execute script with only one command line (in memory)."
)
parser.add_argument(
"-f",
"--file",
default=stdin,
const=stdin,
type=FileType(),
nargs="?",
help="Filename of the script to encode or stdin",
)
parser.add_argument(
"-i",
"--inputs",
nargs="+",
action="extend",
help="Inputs to read in your powershell.",
)
parser.add_argument(
"-p",
"--as-process",
action="store_true",
help="Start this command as single process.",
)
arguments = parser.parse_args()
script = arguments.file.read()
arguments.file.close()
if arguments.as_process:
stdout.write(r'C:\Windows\System32\cmd.exe /c "')
if arguments.inputs:
inputs = "(echo " + " & echo ".join(arguments.inputs) + ") | "
stdout.write(inputs)
print(
r"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -EncodedCommand",
b64encode(script.encode("utf-16-le")).decode()
+ ('"' if arguments.as_process else ""),
)
exit(0)