-
Notifications
You must be signed in to change notification settings - Fork 0
/
shaDOS_main.ps1
251 lines (185 loc) · 5.01 KB
/
shaDOS_main.ps1
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
Function Read-ShaDOS{
param(
[String] $metafile = $env:ShaDOS_Home,
[parameter(Mandatory=$true)]
[String] $filename
)
if((Get-Content $metafile -Stream "passwd") -eq 1){
$passwd = Read-host "Enter metafile password" -AsSecureString
try{
$meta = Get-Content $metafile -Stream "ShaDOS"
}
catch [System.Exception] {
Write-Error "Error reading metafile."
return 0
}
$meta = Decrypt-String $meta (Decrypt-Passwd $passwd)
}
else{
try{
$meta = Get-Content $metafile -Stream "ShaDOS"
}
catch [System.Exception] {
Write-Error "Error reading metafile."
return 0
}
}
foreach($line in $meta){
[Array]$record = $line.Split("*")
if ($record[0] -eq $filename){
$host = $record[1]
$ads = $record[2]
$password = $record[4]
$filedata = Get-Content $host -Stream $ads
if ($password -eq $true){
$pwd = Read-Host -Prompt "Enter password" -AsSecureString
Decrypt-String $filedata (Decrypt-passwd $pwd)
}
Write-Output $filedata
}
}
}
function Get-ShaDOS{
[CmdletBinding()]
param(
[String] $metafile = $env:ShaDOS_Home
)
if((Get-Content $metafile -Stream "passwd") -eq 1){
$passwd = Read-Host "Metafile is password protected. Enter password" -AsSecureString
try{
[string]$encmeta = Get-Content $metafile -Stream "ShaDOS"
}
catch [System.Exception]{
Write-Error "Error reading metafile"
}
$meta = Decrypt-String $encmeta (Decrypt-Passwd $passwd)
}
else{
try{
$meta = Get-Content $metafile -Stream "ShaDOS"
}
catch [System.Exception]{
Write-Error "Error reading metafile"
}
}
foreach($line in $meta){
[Array]$record = $line.Split("*")
$filename = $record[0]
$moddate = $record[3]
$password = $record[4]
Write-Output $filename $moddate
}
}
function Write-ShaDOS{
[CmdletBinding()]
param(
[parameter(Mandatory=$true)]
[String]$filename,
[parameter(Mandatory=$true,ValueFromPipeline=$true)]
[String[]]$destinfo,
[String] $hostfile,
[boolean]$password = $FALSE,
$metahome = $env:ShaDOS_Home
)
begin{
$hostfile = Get-RandomHost;
if ($password){
$enckey = Read-Host -Prompt "Enter password to encrypt data:" -AsSecureString
$check = Read-Host -Prompt "confirm password:" -AsSecureString
if((Decrypt-Passwd $enckey) -eq (Decrypt-Passwd $check)){
$destinfo = Encrypt-String $destinfo (Decrypt-Passwd $enckey)
}
else{
Write-Error "Passwords do not match"
}
}
#Generate random string for data stream
$randstr = Get-RandStr
#build initial metadata string
$meta = $filename+"*"+$hostfile+"*"+$randstr+"*"+(Get-Date)+"*"+$password
#check for metafile
if($metahome -eq $NULL){
$input = Read-Host -prompt "ShaDOS metafile is not defined. Enter a desired location, or type RANDOM for a randomized location."
if($input -eq "RANDOM"){
$input = Get-RandomHost
}
if(Test-Path $input){
Write-host "Metafile location set to: $input"
$metahome = $input
[environment]::SetEnvironmentVariable("ShaDOS_Home",$input,"User")
}
else{
Write-Error "File not found: $input"
}
$inputenc = Read-Host -prompt "Encrypt Metafile (y/n)? (recommended)"
if($inputenc -ieq "y"){
$encpasswd = Read-Host -Prompt "Enter password to encrypt metafile:" -AsSecureString
$enccheck = Read-Host -Prompt "confirm password:" -AsSecureString
if((Decrypt-Passwd $encpasswd) -eq (Decrypt-Passwd $enccheck)){
$meta = Encrypt-String $meta (Decrypt-Passwd $encpasswd)
Add-Content -Path $metahome -Value "1" -Stream "passwd"
}
else{
Write-Error "Passwords do not match"
}
}
} #end NULL metahome condition
elseif (Get-Content $metahome -Stream "shaDOS") {
#check if encrypted
if((Get-Content $metahome -Stream "passwd") -eq 1){
$pass = Read-Host "Enter metafile password" -AsSecureString
$encdata = Get-Content $metahome -Stream "shaDOS"
$decdata = Decrypt-String $encdata (Decrypt-Passwd $pass)
$meta += $decdata
$meta = Encrypt-String $meta (Decrypt-Passwd $pass)
}
#metafile is plaintext
else{
$plainmeta = Get-Content $metahome -Stream "shaDOS"
$meta += $plainmeta
}
}
else {
Write-Error "metafile error"
}
}
process{
Add-Content -Path $hostfile -Value $destinfo -Stream $randstr
}
end{
Add-Content -Path $metahome -Value $meta -Stream "shaDOS"
#reset TrustedInstaller as file owner after writing ADS
icacls $hostfile /setowner "NT Service\TrustedInstaller"
}
}
#Randomizing Functions
#-----------------------------
function Get-RandStr {
$streamLength = Get-Random -minimum 6 -Maximum 13
$builder = New-Object System.Text.StringBuilder
$chars = @();
$i = 0;
for($i=48;$i -le 57;$i++){
$chars += $i;
}
for($i=65;$i -le 90;$i++){
$chars += $i;
}
for($i=97;$i -lt 123;$i++){
$chars += $i;
}
for ($i=0;$i -le $streamLength; $i++){
[Void]$builder.Append([char]$chars[(Get-Random -Minimum 0 -Maximum $chars.length )])
}
return $builder.toString()
}
function Get-RandomHost{
$hostarray = @();
ForEach($x in (Get-ChildItem C:\Windows\SysWOW64 -Filter *.dll)){
$hostarray += $x;
}
$randhost = $hostarray[(Get-Random -Minimum 0 -Maximum $hostarray.Length)].fullName
takeown /a /f $randhost > $null
icacls $randhost /grant Administrators:F > $null
return $randhost
}