9

In the long-term care hospital where I volunteer, on the first day of every month, the password for the Guest Wi-Fi connection changes. This causes a lot of work for the staff and a lot of frustration for the patients, many of whom have very limited mobility.

(Yes, the real solution is to get the IT team to keep the same password, but that's not going to happen).

Most of the patients connect to the outside world through Windows laptops. I'd like to create a batch script that we can install on their computers that will automatically retrieve the password for the coming month and apply it as soon as it is needed.

I can put next month's Guest password in a file on the hospital's internal network, where it can only be accessed by someone who currently has this month's password, and I can use bitsadmin inside a batch script to retrieve the password to a local file (see below). I can set up a task on each patient's computer to run this script just before the end of the month.

My question is: when last month's password fails at the beginning of the new month, how can I change the password for that network connection from a batch script?

I know that I can use...

netsh wlan show profile name=“INSERT_SSID_HERE” key=clear

... to find the current password, but how can I set it?


EDIT: I have found that in Windows Vista and up, the Wi-Fi passphrase is stored in an XML file at C:\ProgramData\Microsoft\Wlansvc\Profiles\Interfaces[Interface Guid].xml. It appears in the format:

- <sharedKey>
    <keyType>passPhrase</keyType>
    <protected>true</protected>
    <keyMaterial> ** 400+ hexit number ** </keyMaterial>
  </sharedKey>

I am guessing that to change the password, I need to encrypt the new password using the appropriate algorithm and update this XML file. Is there a command that I can use to do this automatically? If not, what encryption algorithm should I use?

A simpler alternative might be to remove the encryption:

    <protected>false</protected>
    <keyMaterial>plainTextPassword</keyMaterial>

However, when I try to relaunch the Wi-Fi connection after rebooting the computer, using an XML file that has been modified this way, the connection fails.


A solution which does not require a reboot is preferable.


Batch script to retrieve password:

@echo off
setlocal
set file=%~dp0result.txt
bitsadmin /reset
bitsadmin /create /download job
bitsadmin /addfile job http://example.com/password.html %file%
bitsadmin /resume job

timeout 5

bitsadmin /getstate job | find /i "TRANSFERRED" && goto :done
bitsadmin /cancel job
exit /b 1

:done
bitsadmin /complete job
:: results.txt now holds the new password
exit /b 0
10
  • High security hospital this one! A simpler solution that meets all your current security concerns is to hand the password to patients on a slip of paper.
    – John3136
    Commented Jan 21, 2016 at 22:50
  • 1
    That would be so nice. But many of them cannot move their hands. For some of them their laptops (and their eye-movement detectors and single-switch scanners) are their only connection with the outside world. Commented Jan 21, 2016 at 23:18
  • I have an idea of how to do this, "I can put next month's Guest password in a file on the hospital's internal network", how would it need to be accessed? UNC path, FTP or something alike?
    – Will Ryan
    Commented Jan 29, 2016 at 17:13
  • Just need to know how to access the file so i can write something accordingly
    – Will Ryan
    Commented Jan 29, 2016 at 17:13
  • The batch script I give in the question allows me to create a results.txt file that contains the password, anywhere on the end-user's computer. You can consider that such a file exists, and that it is located wherever you want it to be. Commented Jan 29, 2016 at 18:15

4 Answers 4

5
+50

Did you try removing and re-adding the profile instead of just changing the content of the XML to use plain password?

I had a similar situation in the past and this is what worked for me:

This answer assumes that you want to keep only the WiFi password on the network instead of a full XML and the SSID of the WiFi is "My Network".

  1. Disconnect from the network: netsh wlan disconnect
  2. Export the "My Network" profile to somewhere else, e.g. C:\: netsh wlan export profile folder="C:\" name="My Network" key=clear --> this should create C:\Wi-Fi-My Network.xml. The resulting xml file should be similar to the one you see in C:\ProgramData\Microsoft\Wlansvc\Profiles\Interfaces, but with the <sharedKey> part unprotected (this allows us to only do one replacement on the next step). Obviously, you can also do this by copying the file from the Wlansvc's profile directory, but doing so requires the knowledge of the interface's GUID. Exporting is easier as you only need to know your SSID.
  3. In the copied profile, make sure that protected is false, and fill the keyMaterial with the plain password (simple text replacement, should be easy to do with VB or C#, but if you need to do it purely in batch script, see Changing tag data in an XML file using windows batch file)
  4. Remove the currently stored profile: netsh wlan delete profile name="My Network"
  5. Add back the profile: netsh wlan add profile filename="C:\Wi-Fi-My Network.xml".
    (This will recreate the appropriate file in C:\ProgramData\Microsoft\Wlansvc\Profiles\Interfaces, with encrypted password.)
  6. Connect to the network: netsh wlan connect name="My Network"

If you are okay with storing the full XML, you can also export the profile unencrypted (step 2) and store it in your network drive, then you simply need to do step 1, 4, 5 and 6.

I hope this helps.

4
  • Could you explain what the command C:\MyProfile.xml: netsh wlan export profile folder="C:\" name="My Network" key=clear does? I'm assuming that C:\MyProfile.xml should be replaced with C:\ProgramData\Microsoft\Wlansvc\Profiles\Interfaces\{blablabla}\{profilename}.xml. When I do this, an Internet Explorer window opens, showing the contents of the XML file. I imagined I would see a copy of the file appearing at the root of C:\ , but I don't see that happening. Commented Jan 29, 2016 at 18:26
  • "In the copied profile, change protected to false, and fill the keyMaterial with the plain password" >>> I'm guessing that I'm going to need to use a regular expression to make this change from a batch script. It would be great to see a complete batch script that treats all these steps. Commented Jan 29, 2016 at 18:29
  • The command should start from the netsh. Just forget about the MyProfile.xml thing, it's a leftover from my personal note. I have clarified the step in an edit. I hope it's clearer now? Basically that step is just to make a copy of the current profile of your Wifi profile My Network with the key in cleartext.
    – roberto
    Commented Jan 29, 2016 at 22:34
  • As for the second comment: yes. See this question for an example: stackoverflow.com/questions/17054275/… I only use Windows at work so I can't write and test a complete batch script at the moment. If you can't get it to work I'll see if I can create one on Monday.
    – roberto
    Commented Jan 29, 2016 at 22:39
1

The best logical way i would notify the password is by keeping the password as a number and keeping the wifi name as +or- "from the previous password" every month by this way you could notify the patients for once that what next months password is gonna be! :-) ex: suppose your current password is 134768 and your next months password is 134778 then at the beggining of the next month you could rename the wifi as +ten .

1

You can also write a batch that utilizes

netsh wlan set profileparameter name="SSID" keyMaterial="password"

to update the XML document. The keyMaterial parameter could be passed in through a variable set from another .bat or other file.

I crafted this method when my local coffee shop decided to change the password everyday and I grew tired of telling Windows 10 to forget the network and reconnect.

0

It seems like "FooBar" asked a forume how to en/de crypt the keymaterial but there is no need for that from what im getting, cause he mentions "I successfully just copied and pasted a test one of these between computers, and it connected just fine, so the encryption is definitely reversible." so i would guess that all you have to do is:

  1. Type in the wireless password on a staff computer
  2. Check if the xml file updats(If it does then next step)
  3. Then put it on the network where the multiple computer can reach
  4. C&P the .xml file and overwrite
  5. Pray that you dont need a restart for it else i would guess you could disable the internet driver and re-enable it from batch

Thats my take on it, but i can't really test it at this time.

4
  • The encryption is reversible, but typically, only a user with the same logon credential as the user who encrypted the data can decrypt the data, which means it's not really possible to just copy and paste the xml file if the keyMaterial is still encrypted. (keyMaterial is just encrypted using CryptProtectData and can be decrypted using CryptUnprotectData; see: msdn.microsoft.com/en-us/library/windows/desktop/… It's fairly straight forward to do this with e.g. VB or C#, but for batch file I think invoking netsh is probably the easiest in this case)
    – roberto
    Commented Feb 1, 2016 at 10:20
  • 1
    Well, another way of doing it is nirsoft.net/utils/wireless_key.html it has command lines to exports to a .txt file that the program can import with a command line. but that just looks like a last resort to me. And CryptProtectData is C++ while only has a batch tag.
    – NizonRox
    Commented Feb 1, 2016 at 10:24
  • Yes, this whole thing is pretty simple if it wasn't for the batch restriction. The nice thing about netsh is that it's built-in so it will just work with batch.
    – roberto
    Commented Feb 1, 2016 at 10:40
  • Thats very true but i should mean that you can run the wireless_key program from the network, makes it easier but yeah netsh is quite the thing. There has been many restrictions for batch but alot of people have found clever ways to deal with it without changing to vbs,c,java ect.
    – NizonRox
    Commented Feb 1, 2016 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.