vagrant-windows-sysprep 0.0.11 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8992237b7f51500d12a334f886c8c33a32a4504b326294cefac6e1eb299a8396
4
- data.tar.gz: 764ddcf2e955a498404febb36353d9d2c32e2346c17e31aabe62d9f845349900
3
+ metadata.gz: 12257e354aa94b01e8ab8110663463b5b0b422e7a0df0bdecfd3754d65ada588
4
+ data.tar.gz: 44e5871f399dfffa4e6306e97d776e1cd3af0c08976d28cb2991b13d0b53e3e2
5
5
  SHA512:
6
- metadata.gz: c7cd8cc24d09bdee2c0f2a7b8f4920c78e8e0a3eff728a4652a76ee5dcbf5a7206f035c8dadb924b146b9d9a8a24a9cfec5a5db80f8875a48b829462e09cff37
7
- data.tar.gz: af43f8effba20ecd95cf855d23514b538a6f442031918e52e4a17f1eb7fd94a3e99f09636469c9ace964b0f78121fbbb71c2dcb63337cfa0403d196270213df1
6
+ metadata.gz: 0f9009223749455b75a6ce997945290c4836f120a174428441cb4f6afee9f2c5f1fef14fae68764a7ee2a212768b256fd5aaea44fbbec47be04c216be90c7ca4
7
+ data.tar.gz: b2df7c04e9a19b322d5d218d40692de998f1245e938ed8d1af9916cd9c948824764f92f86f75cb8616528d356369332b3782c70950df98da295e1184f69cf323
@@ -77,6 +77,8 @@ trap {
77
77
  Exit 1
78
78
  }
79
79
 
80
+ $Host.UI.RawUI.WindowTitle = 'WARNING :: vagrant-windows-sysprep :: do NOT touch anything :: WARNING'
81
+
80
82
  Write-Host @"
81
83
  WARNING WARNING WARNING WARNING WARNING WARNING
82
84
 
@@ -98,12 +100,197 @@ if ($renameAdministratorToUsername) {
98
100
  Get-LocalUser -Name Administrator | Rename-LocalUser -NewName $Username
99
101
  }
100
102
 
101
- Write-Host 'Waiting for WinRM to be running...'
102
- while ((Get-Service WinRM).Status -ne 'Running') {
103
- Start-Sleep -Seconds 5
104
- }
105
-
103
+ # disable auto logon.
104
+ # see https://learn.microsoft.com/en-us/windows/win32/secauthn/msgina-dll-features
105
+ # see http://www.pinvoke.net/default.aspx/advapi32.lsaretrieveprivatedata
106
+ # see https://attack.mitre.org/techniques/T1003/004/
106
107
  Write-Host 'Disabling auto logon...'
108
+ Add-Type @"
109
+ using System;
110
+ using System.Runtime.InteropServices;
111
+ using System.Text;
112
+
113
+ namespace PInvoke
114
+ {
115
+ public class LSAUtil
116
+ {
117
+ [StructLayout(LayoutKind.Sequential)]
118
+ private struct LSA_UNICODE_STRING
119
+ {
120
+ public UInt16 Length;
121
+ public UInt16 MaximumLength;
122
+ public IntPtr Buffer;
123
+ }
124
+
125
+ [StructLayout(LayoutKind.Sequential)]
126
+ private struct LSA_OBJECT_ATTRIBUTES
127
+ {
128
+ public int Length;
129
+ public IntPtr RootDirectory;
130
+ public LSA_UNICODE_STRING ObjectName;
131
+ public uint Attributes;
132
+ public IntPtr SecurityDescriptor;
133
+ public IntPtr SecurityQualityOfService;
134
+ }
135
+
136
+ private enum LSA_AccessPolicy : long
137
+ {
138
+ POLICY_VIEW_LOCAL_INFORMATION = 0x00000001L,
139
+ POLICY_VIEW_AUDIT_INFORMATION = 0x00000002L,
140
+ POLICY_GET_PRIVATE_INFORMATION = 0x00000004L,
141
+ POLICY_TRUST_ADMIN = 0x00000008L,
142
+ POLICY_CREATE_ACCOUNT = 0x00000010L,
143
+ POLICY_CREATE_SECRET = 0x00000020L,
144
+ POLICY_CREATE_PRIVILEGE = 0x00000040L,
145
+ POLICY_SET_DEFAULT_QUOTA_LIMITS = 0x00000080L,
146
+ POLICY_SET_AUDIT_REQUIREMENTS = 0x00000100L,
147
+ POLICY_AUDIT_LOG_ADMIN = 0x00000200L,
148
+ POLICY_SERVER_ADMIN = 0x00000400L,
149
+ POLICY_LOOKUP_NAMES = 0x00000800L,
150
+ POLICY_NOTIFICATION = 0x00001000L
151
+ }
152
+
153
+ [DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)]
154
+ private static extern uint LsaRetrievePrivateData(
155
+ IntPtr PolicyHandle,
156
+ ref LSA_UNICODE_STRING KeyName,
157
+ out IntPtr PrivateData
158
+ );
159
+
160
+ [DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)]
161
+ private static extern uint LsaStorePrivateData(
162
+ IntPtr policyHandle,
163
+ ref LSA_UNICODE_STRING KeyName,
164
+ ref LSA_UNICODE_STRING PrivateData
165
+ );
166
+
167
+ [DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)]
168
+ private static extern uint LsaOpenPolicy(
169
+ ref LSA_UNICODE_STRING SystemName,
170
+ ref LSA_OBJECT_ATTRIBUTES ObjectAttributes,
171
+ uint DesiredAccess,
172
+ out IntPtr PolicyHandle
173
+ );
174
+
175
+ [DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)]
176
+ private static extern uint LsaNtStatusToWinError(
177
+ uint status
178
+ );
179
+
180
+ [DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)]
181
+ private static extern uint LsaClose(
182
+ IntPtr policyHandle
183
+ );
184
+
185
+ [DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)]
186
+ private static extern uint LsaFreeMemory(
187
+ IntPtr buffer
188
+ );
189
+
190
+ private LSA_OBJECT_ATTRIBUTES objectAttributes;
191
+ private LSA_UNICODE_STRING localsystem;
192
+ private LSA_UNICODE_STRING secretName;
193
+
194
+ public LSAUtil(string key)
195
+ {
196
+ if (key.Length == 0)
197
+ {
198
+ throw new Exception("Key length zero");
199
+ }
200
+ objectAttributes = new LSA_OBJECT_ATTRIBUTES();
201
+ objectAttributes.Length = 0;
202
+ objectAttributes.RootDirectory = IntPtr.Zero;
203
+ objectAttributes.Attributes = 0;
204
+ objectAttributes.SecurityDescriptor = IntPtr.Zero;
205
+ objectAttributes.SecurityQualityOfService = IntPtr.Zero;
206
+ localsystem = new LSA_UNICODE_STRING();
207
+ localsystem.Buffer = IntPtr.Zero;
208
+ localsystem.Length = 0;
209
+ localsystem.MaximumLength = 0;
210
+ secretName = new LSA_UNICODE_STRING();
211
+ secretName.Buffer = Marshal.StringToHGlobalUni(key);
212
+ secretName.Length = (UInt16)(key.Length * UnicodeEncoding.CharSize);
213
+ secretName.MaximumLength = (UInt16)((key.Length + 1) * UnicodeEncoding.CharSize);
214
+ }
215
+
216
+ private IntPtr GetLsaPolicy(LSA_AccessPolicy access)
217
+ {
218
+ IntPtr LsaPolicyHandle;
219
+ uint ntsResult = LsaOpenPolicy(ref this.localsystem, ref this.objectAttributes, (uint)access, out LsaPolicyHandle);
220
+ uint winErrorCode = LsaNtStatusToWinError(ntsResult);
221
+ if (winErrorCode != 0)
222
+ {
223
+ throw new Exception("LsaOpenPolicy failed: " + winErrorCode);
224
+ }
225
+ return LsaPolicyHandle;
226
+ }
227
+
228
+ private static void ReleaseLsaPolicy(IntPtr LsaPolicyHandle)
229
+ {
230
+ uint ntsResult = LsaClose(LsaPolicyHandle);
231
+ uint winErrorCode = LsaNtStatusToWinError(ntsResult);
232
+ if (winErrorCode != 0)
233
+ {
234
+ throw new Exception("LsaClose failed: " + winErrorCode);
235
+ }
236
+ }
237
+
238
+ private static void FreeMemory(IntPtr Buffer)
239
+ {
240
+ uint ntsResult = LsaFreeMemory(Buffer);
241
+ uint winErrorCode = LsaNtStatusToWinError(ntsResult);
242
+ if (winErrorCode != 0)
243
+ {
244
+ throw new Exception("LsaFreeMemory failed: " + winErrorCode);
245
+ }
246
+ }
247
+
248
+ public void SetSecret(string value)
249
+ {
250
+ LSA_UNICODE_STRING lusSecretData = new LSA_UNICODE_STRING();
251
+ if (value.Length > 0)
252
+ {
253
+ // Create data and key.
254
+ lusSecretData.Buffer = Marshal.StringToHGlobalUni(value);
255
+ lusSecretData.Length = (UInt16)(value.Length * UnicodeEncoding.CharSize);
256
+ lusSecretData.MaximumLength = (UInt16)((value.Length + 1) * UnicodeEncoding.CharSize);
257
+ }
258
+ else
259
+ {
260
+ // Delete data and key.
261
+ lusSecretData.Buffer = IntPtr.Zero;
262
+ lusSecretData.Length = 0;
263
+ lusSecretData.MaximumLength = 0;
264
+ }
265
+ IntPtr LsaPolicyHandle = GetLsaPolicy(LSA_AccessPolicy.POLICY_CREATE_SECRET);
266
+ uint result = LsaStorePrivateData(LsaPolicyHandle, ref secretName, ref lusSecretData);
267
+ ReleaseLsaPolicy(LsaPolicyHandle);
268
+ uint winErrorCode = LsaNtStatusToWinError(result);
269
+ if (winErrorCode != 0)
270
+ {
271
+ throw new Exception("LsaStorePrivateData failed: " + winErrorCode);
272
+ }
273
+ }
274
+
275
+ public string GetSecret()
276
+ {
277
+ IntPtr PrivateData = IntPtr.Zero;
278
+ IntPtr LsaPolicyHandle = GetLsaPolicy(LSA_AccessPolicy.POLICY_GET_PRIVATE_INFORMATION);
279
+ uint ntsResult = LsaRetrievePrivateData(LsaPolicyHandle, ref secretName, out PrivateData);
280
+ ReleaseLsaPolicy(LsaPolicyHandle);
281
+ uint winErrorCode = LsaNtStatusToWinError(ntsResult);
282
+ if (winErrorCode != 0)
283
+ {
284
+ throw new Exception("LsaRetrievePrivateData failed: " + winErrorCode);
285
+ }
286
+ LSA_UNICODE_STRING lusSecretData = (LSA_UNICODE_STRING)Marshal.PtrToStructure(PrivateData, typeof(LSA_UNICODE_STRING));
287
+ string value = Marshal.PtrToStringAuto(lusSecretData.Buffer).Substring(0, lusSecretData.Length / 2);
288
+ FreeMemory(PrivateData);
289
+ return value;
290
+ }
291
+ }
292
+ }
293
+ "@
107
294
  $autoLogonKeyPath = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
108
295
  Set-ItemProperty `
109
296
  -Path $autoLogonKeyPath `
@@ -112,12 +299,31 @@ Set-ItemProperty `
112
299
  Remove-ItemProperty `
113
300
  -Path $autoLogonKeyPath `
114
301
  -Name @(
115
- 'DefaultDomainName',
116
- 'DefaultUserName',
117
- 'DefaultPassword'
302
+ ,'AutoLogonCount'
303
+ ,'AutoLogonSID'
304
+ ,'DefaultDomainName'
305
+ ,'DefaultUserName'
306
+ ,'DefaultPassword'
118
307
  ) `
119
308
  -ErrorAction SilentlyContinue
120
309
 
310
+ # enable the remote access services.
311
+ @(
312
+ 'WinRM'
313
+ 'sshd'
314
+ ) | ForEach-Object {
315
+ $service = Get-Service -Name $_ -ErrorAction SilentlyContinue
316
+ if ($service) {
317
+ Write-Host "Enabling the $_ service..."
318
+ $result = sc.exe config $_ start= delayed-auto
319
+ if ($result -ne '[SC] ChangeServiceConfig SUCCESS') {
320
+ throw "sc.exe config $_ failed with $result"
321
+ }
322
+ Write-Host "Starting the $_ service..."
323
+ Start-Service -Name $_
324
+ }
325
+ }
326
+
121
327
  Write-Host 'Logging off...'
122
328
  logoff
123
329
  '@ `
@@ -137,6 +343,7 @@ if (Test-Path $sysprepSucceededTagPath) {
137
343
  Remove-Item -Force $sysprepSucceededTagPath
138
344
  }
139
345
  C:/Windows/System32/Sysprep/sysprep.exe `
346
+ /mode:vm `
140
347
  /generalize `
141
348
  /oobe `
142
349
  /quiet `
@@ -156,5 +363,19 @@ if ($imageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') {
156
363
  throw "the windows image state $imageState is not the expected IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE."
157
364
  }
158
365
 
366
+ # disable the remote access services.
367
+ # NB they will be enabled by $unattendPs1 script (created above) after the oobe
368
+ # phase is complete.
369
+ @(
370
+ 'WinRM'
371
+ 'sshd'
372
+ ) | ForEach-Object {
373
+ $service = Get-Service -Name $_ -ErrorAction SilentlyContinue
374
+ if ($service) {
375
+ Write-Host "Disabling the $_ service..."
376
+ Set-Service -Name $_ -StartupType Disabled
377
+ }
378
+ }
379
+
159
380
  Write-Host 'Stopping computer...'
160
381
  Stop-Computer -Force
@@ -1,17 +1,16 @@
1
1
  <?xml version="1.0" encoding="utf-8"?>
2
2
  <!--
3
3
  sysprep copies this file to C:\Windows\Panther\unattend.xml
4
- the logs are stored at C:\Windows\Panther\setupact.log
4
+ the logs are stored in the following directories:
5
+ generalize phase: C:\Windows\System32\Sysprep\Panther
6
+ specialize phase: C:\Windows\Panther
7
+ oobe phase: C:\Windows\Panther\UnattendGC
5
8
  -->
6
9
  <unattend xmlns="urn:schemas-microsoft-com:unattend">
7
10
  <settings pass="generalize">
8
11
  <component name="Microsoft-Windows-Security-SPP" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
9
12
  <SkipRearm>1</SkipRearm>
10
13
  </component>
11
- <component name="Microsoft-Windows-PnpSysprep" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
12
- <PersistAllDeviceInstalls>false</PersistAllDeviceInstalls>
13
- <DoNotCleanUpNonPresentDevices>false</DoNotCleanUpNonPresentDevices>
14
- </component>
15
14
  </settings>
16
15
  <settings pass="specialize">
17
16
  <component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
@@ -52,7 +51,7 @@
52
51
  <FirstLogonCommands>
53
52
  <SynchronousCommand wcm:action="add">
54
53
  <Order>1</Order>
55
- <CommandLine>PowerShell -File @@UNATTENDPS1PATH@@</CommandLine>
54
+ <CommandLine>PowerShell -WindowStyle Maximized -File @@UNATTENDPS1PATH@@</CommandLine>
56
55
  </SynchronousCommand>
57
56
  </FirstLogonCommands>
58
57
  </component>
@@ -1,5 +1,5 @@
1
1
  module VagrantPlugins
2
2
  module WindowsSysprep
3
- VERSION = "0.0.11"
3
+ VERSION = "0.0.12"
4
4
  end
5
5
  end
@@ -71,6 +71,7 @@ module VagrantPlugins
71
71
  sleep 10
72
72
  end
73
73
 
74
+ @machine.ui.info "Starting computer..."
74
75
  options = {}
75
76
  options[:provision_ignore_sentinel] = false
76
77
  @machine.action(:up, options)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-windows-sysprep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rui Lopes