[Reverse] SECCON - Powerful_Shell



Powerful_Shell
300 points
Powerful_Shell

Crack me.
powerful_shell.ps1-1fb3af91eafdbebf3b3efa3b84fcc10cfca21ab53db15c98797b500c739b0024

Windows powershell… We started by analysing the file:

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
$ECCON="";
$ECCON+=[char](3783/291);
$ECCON+=[char](6690/669);
$ECCON+=[char](776-740);
$ECCON+=[char](381-312);
$ECCON+=[char](403-289);
$ECCON+=[char](-301+415);
$ECCON+=[char](143-32);
$ECCON+=[char](93594/821);
$ECCON+=[char](626-561);
$ECCON+=[char](86427/873);
$ECCON+=[char](112752/972);
$ECCON+=[char](43680/416);
$ECCON+=[char](95127/857);
$ECCON+=[char](-682+792);
$ECCON+=[char](-230+310);
$ECCON+=[char](-732+846);
$ECCON+=[char](1027-926);
$ECCON+=[char](94044/922);
$ECCON+=[char](898-797);
$ECCON+=[char](976-862);
$ECCON+=[char](52419/519);
$ECCON+=[char](1430/13);
$ECCON+=[char](18216/184);
$ECCON+=[char](21715/215);
$ECCON+=[char](12320/385);
$ECCON+=[char]([int][Math]::sqrt([Math]::pow(61,2)));
.....
Write-Progress -Activity "Extracting Script" -status "20040" -percentComplete 99;
$ECCON+=[char](520-510);
Write-Progress -Completed -Activity "Extracting Script";.([ScriptBlock]::Create($ECCON))

As we can see we have alot of operations, it looks like something is being hidden behind those arithmetic operations, I did a python script to parse all the operation using regex:

1
2
3
4
5
6
7
8
9
10
11
12
13
import re
from math import *

script = ''
lines = open('powerful_shell','r').read().split('\n')
for line in lines:
if 'sqrt' in line:
op = line.replace('$ECCON+=[char]([int][Math]::','').replace('[Math]::','').replace(';','').replace(')','') + '))'
script += chr(int(eval(op)))
elif '$ECCON' in line and len(re.findall(r'\((-*[0-9]+.-*[0-9]+)\)', line)) > 0:
op = re.findall(r'\((-*[0-9]+.-*[0-9]+)\)', line)[0]
script += chr(int(eval(op)))
print script

After extracting it we got this:

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
... truncanted ...

$keytone=@{'a'=261.63}
$pk='a'
ForEach($k in ('w','s','e','d','f','t','g','y','h','u','j','k')){
$keytone+=@{$k=$keytone[$pk]*[math]::pow(2,1/12)};$pk=$k
}
Write-Host -b 00 -f 15 "Play the secret melody."

Write-Host -b 15 -f 00 -n ' '
Write-Host -b 00 -f 15 -n ' '
Write-Host -b 15 -f 00 -n ' '
Write-Host -b 00 -f 15 -n ' '
Write-Host -b 15 -f 00 -n ' | '
Write-Host -b 00 -f 15 -n ' '
Write-Host -b 15 -f 00 -n ' '
Write-Host -b 00 -f 15 -n ' '
Write-Host -b 15 -f 00 -n ' '
Write-Host -b 00 -f 15 -n ' '
Write-Host -b 15 -f 00 ' | '
Write-Host -b 15 -f 00 -n ' '
Write-Host -b 00 -f 15 -n ' '
Write-Host -b 15 -f 00 -n ' '
Write-Host -b 00 -f 15 -n ' '
Write-Host -b 15 -f 00 -n ' | '
Write-Host -b 00 -f 15 -n ' '
Write-Host -b 15 -f 00 -n ' '
Write-Host -b 00 -f 15 -n ' '
Write-Host -b 15 -f 00 -n ' '
Write-Host -b 00 -f 15 -n ' '
Write-Host -b 15 -f 00 ' | '
Write-Host -b 15 -f 00 -n ' '
Write-Host -b 00 -f 15 -n ' w '
Write-Host -b 15 -f 00 -n ' '
Write-Host -b 00 -f 15 -n ' e '
Write-Host -b 15 -f 00 -n ' | '
Write-Host -b 00 -f 15 -n ' t '
Write-Host -b 15 -f 00 -n ' '
Write-Host -b 00 -f 15 -n ' y '
Write-Host -b 15 -f 00 -n ' '
Write-Host -b 00 -f 15 -n ' u '
Write-Host -b 15 -f 00 ' | '
Write-Host -b 15 -f 00 -n ' |'
Write-Host -b 15 -f 00 -n ' |'
Write-Host -b 15 -f 00 -n ' |'
Write-Host -b 15 -f 00 -n ' |'
Write-Host -b 15 -f 00 -n ' |'
Write-Host -b 15 -f 00 -n ' |'
Write-Host -b 15 -f 00 -n ' |'
Write-Host -b 15 -f 00 ' '
Write-Host -b 15 -f 00 -n ' a |'
Write-Host -b 15 -f 00 -n ' s |'
Write-Host -b 15 -f 00 -n ' d |'
Write-Host -b 15 -f 00 -n ' f |'
Write-Host -b 15 -f 00 -n ' g |'
Write-Host -b 15 -f 00 -n ' h |'
Write-Host -b 15 -f 00 -n ' j |'
Write-Host -b 15 -f 00 ' k '
Write-Host -b 15 -f 00 -n ' |'
Write-Host -b 15 -f 00 -n ' |'
Write-Host -b 15 -f 00 -n ' |'
Write-Host -b 15 -f 00 -n ' |'
Write-Host -b 15 -f 00 -n ' |'
Write-Host -b 15 -f 00 -n ' |'
Write-Host -b 15 -f 00 -n ' |'
Write-Host -b 15 -f 00 ' '
Write-Host
$stage1=@();$f="";
While($stage1.length -lt 14){
$key=(Get-Host).ui.RawUI.ReadKey("NoEcho,IncludeKeyDown")
$k=[String]$key.Character
$f+=$k;
If($keytone.Contains($k)){
$stage1+=[math]::floor($keytone[$k])
[console]::beep($keytone[$k],500)
}
}
$secret=@(440,440,493,440,440,493,440,493,523,493,440,493,440,349)
If($secret.length -eq $stage1.length){
For ($i=1; $i -le $secret.length; $i++) {
If($secret[$i] -ne $stage1[$i]){
Exit
}
}
x "Correct. Move to the next stage."
}
$text=@"
YkwRUxVXQ05DQ1NOE1sVVU4TUxdTThBBFVdDTUwTURVTThMqFldDQUwdUxVRTBNEFVdAQUwRUxtT
TBEzFVdDQU8RUxdTbEwTNxVVQUNOEFEVUUwdQBVXQ0NOE1EWUUw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XQ0wRShdPEUYXTB0UFUFME0gVThBDFU4TTRVD
ThNKF0wRQBdMEUMbTBNBFUNOE0gWThNBFUITQxdME0EVQU8RShdMHUMVThNVF0wRVhdDThNEFUwR
RhVOEyoWQUwRShtME0MVTBEzF0BOE0gVQhNDF0wTQRVOEEEVQUwdShVOExQXTBFNF0NOE0QVTBFG
FU4TRBZBTBFKG0wTRBVMERQXQE4TSBVCEzUXTBMWFUFPEUoXTB1DFU4TRhdDThBIFU4TTRVMEVUV
QUwRSRdMERQbQ0wRShVOE0wWThNDFU1ME0gVTBFDF08RQxdMHTMVQUwTSBVOEEEVThNbFUwRNRVB
TBFJF0wRQxtME0EVTBFAF0BOE0gVQhNDF0wTVxVOEEEVQUwdShVOEzMXTBE2F0NOE0QVTBFBFU4T
KhZBTBFKG0wTQRVMEUMXTxFDF0NCE0oXTBNBFU4QQRVOEzsVQ04TShdMEUAXTBFDG0wTQhVDThNI
Fk4TRBVCEygXQ0wRShdPEUYXTB0zFUFME0gVThBMFU4TSBVDThNKF0wRQxdMERQbQ0wRShVOE0IW
ThNDFU1ME0gVTBFAF08RQRdDQhNKF0wTQxVOEBYVQUwdShVOE0EXTBFNF0NOE0QVTBFDFU4TKhZO
E0QVTUwTSBVMEUYXTxFAF0NCE0oXTBNCFU4QFhVBTB1KFU4TQBdMEUIXQ04TRBVMEUAVThNDFkFM
EUobTBNDFUwRFBdAThNIFUITQRdME0wVQU8RShdMHUMVThMoF0wRNhdDThNEFUwRRhVOEzEWQUwR
ShtME0EVTBFGF0BOE0gVQhNDF0wTVxVBTxFKF0wdQxVOEygXTBE2FxROE10VShZOTBFTF2E=
"@

$plain=@()
$byteString = [System.Convert]::FromBase64String($text)
$xordData = $(for ($i = 0; $i -lt $byteString.length; ) {
for ($j = 0; $j -lt $f.length; $j++) {
$plain+=$byteString[$i] -bxor $f[$j]
$i++
if ($i -ge $byteString.Length) {
$j = $f.length
}
}
})
iex([System.Text.Encoding]::ASCII.GetString($plain))

I removed some of the beginning of the script, it was useless code and was just printing some things to the screen, continuing with the rest we have more two steps to conclude, the first one was to play the piano accordantly to the key in the code 440,440,493,440,440,493,440,493,523,493,440,493,440,349.

1
2
3
4
5
6
7
8
9
10
While($stage1.length -lt 14){
$key=(Get-Host).ui.RawUI.ReadKey("NoEcho,IncludeKeyDown")
$k=[String]$key.Character
$f+=$k;
If($keytone.Contains($k)){
$stage1+=[math]::floor($keytone[$k])
Write-Host ([math]::floor($keytone[$k])).toString(),$k
[console]::beep($keytone[$k],500)
}
}

Each key of the piano corresponds to a number this is easy to discover without having to even look at the code, I just added a few prints in the loop and go through the executable and enter the correct key for the piano was hhjhhjhjkjhjhf



The last phase is to decrypt that big base64 string with the key we got from the piano, once again we don’t even need to check the code just print the variable and you will get the last powershell script of the challenge.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$plain=@()
$byteString = [System.Convert]::FromBase64String($text)
$xordData = $(for ($i = 0; $i -lt $byteString.length; ) {
for ($j = 0; $j -lt $f.length; $j++) {
$plain+=$byteString[$i] -bxor $f[$j]
$i++
if ($i -ge $byteString.Length) {
$j = $f.length
}
}
})
iex([System.Text.Encoding]::ASCII.GetString($plain))
Write-Host iex([System.Text.Encoding]::ASCII.GetString($plain))
Read-Host -Prompt "Press Enter to exit"

Now we get another powershell script but the obfuscation on this one is real asking for a password:



Well it wasn’t very hard to me to reverse it, if you look closely there are alot of semicolons separating each variable assignment, for instance I checked one bye one variable and the one who looked interesting was the variable {;}

1
2
PS C:\Users\root> ${;}
iex

iex is an invocation, for example it can be used to run a .ps1 file, if we look closely at the end of the script we can see a very big string (in green) which is being piped into iex:



if we copy that big string into the terminal we can see alot of [char]number, to easily convert them into text we can assign it into a variable and then call it and get the password!



As we can see the flag was the password! SECCON{P0wEr$H311}