Skip to content

powershell execute encodedcommand instead of ps1 file #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 28 additions & 29 deletions toast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package toast

import (
"bytes"
"encoding/base64"
"errors"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"

"github.com/nu7hatch/gouuid"
"syscall"

"golang.org/x/text/encoding/unicode"
)

var toastTemplate *template.Template
Expand Down Expand Up @@ -62,18 +61,18 @@ const (
func init() {
toastTemplate = template.New("toast")
toastTemplate.Parse(`
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null;
[Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null;
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null;

$APP_ID = '{{if .AppID}}{{.AppID}}{{else}}Windows App{{end}}'
$APP_ID = "{{if .AppID}}{{.AppID}}{{else}}Windows App{{end}}";

$template = @"
<toast activationType="{{.ActivationType}}" launch="{{.ActivationArguments}}" duration="{{.Duration}}">
$template = "
<toast activationType=""{{.ActivationType}}"" launch=""{{.ActivationArguments}}"" duration=""{{.Duration}}"">
<visual>
<binding template="ToastGeneric">
<binding template=""ToastGeneric"">
{{if .Icon}}
<image placement="appLogoOverride" src="{{.Icon}}" />
<image placement=""appLogoOverride"" src=""{{.Icon}}"" />
{{end}}
{{if .Title}}
<text><![CDATA[{{.Title}}]]></text>
Expand All @@ -84,24 +83,24 @@ $template = @"
</binding>
</visual>
{{if ne .Audio "silent"}}
<audio src="{{.Audio}}" loop="{{.Loop}}" />
<audio src=""{{.Audio}}"" loop=""{{.Loop}}"" />
{{else}}
<audio silent="true" />
<audio silent=""true"" />
{{end}}
{{if .Actions}}
<actions>
{{range .Actions}}
<action activationType="{{.Type}}" content="{{.Label}}" arguments="{{.Arguments}}" />
<action activationType=""{{.Type}}"" content=""{{.Label}}"" arguments=""{{.Arguments}}"" />
{{end}}
</actions>
{{end}}
</toast>
"@
";

$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($template)
$toast = New-Object Windows.UI.Notifications.ToastNotification $xml
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($APP_ID).Show($toast)
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument;
$xml.LoadXml($template);
$toast = New-Object Windows.UI.Notifications.ToastNotification $xml;
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($APP_ID).Show($toast);
`)
}

Expand Down Expand Up @@ -341,19 +340,19 @@ func Duration(name string) (toastDuration, error) {
}

func invokeTemporaryScript(content string) error {
id, _ := uuid.NewV4()
file := filepath.Join(os.TempDir(), id.String()+".ps1")
defer os.Remove(file)
bomUtf8 := []byte{0xEF, 0xBB, 0xBF}
out := append(bomUtf8, []byte(content)...)
err := ioutil.WriteFile(file, out, 0600)
content = strings.TrimSpace(strings.ReplaceAll(content, "\n", ""))

encoder := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewEncoder()
utf16, err := encoder.String(content)
if err != nil {
return err
}
cmd := exec.Command("PowerShell", "-ExecutionPolicy", "Bypass", "-File", file)
encContent := base64.StdEncoding.EncodeToString([]byte(utf16))

cmd := exec.Command("PowerShell", "-EncodedCommand", encContent)
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
if err = cmd.Run(); err != nil {
if err := cmd.Run(); err != nil {
return err
}
return nil
}
}