This is a fun one to crack from the previous post which you can find here
The report was handy, but wouldn’t it be nice if you could click a red unlock button which would intern would queue an account unlock request for another script to process.
Note : I did originally go down the route of allowing the website to unlock an account in active directory, however, I quickly realize the security ramifications of completing such action so I’ve modified it so it doesn’t directly talk “direct” to ADDS - even with asp.net there are some issues with this process especially when linked to IIS websites.
How does this work?
Interesting question, you are presented with the same report as the previous post except on the right you will get red unlock button, when you click on the unlock button you will be prompted if you would like to queue a request for this account, when this is confirmed, the butter will turn green with pending on the button and it will write it to a file in the same folder the website is running from, this file will contain the date and the time of the request along with samAccountName attribute.
That is as far as the website goes from an IIS perspective, you are simply requesting the account to be unlocked.
Refresh the page button once again to unlock And you can request the same account to be unlocked - Did nothing does not reflect the state of their account only what’s in the report for lockouts - as this report runs daily, the same users should not be continually on the same report every day.
The actual unlocking of the account is done by another script back to monitors this file for requests every five seconds, When you requested added the second script will actually unlock the account and then remove it from the request file so it’s not continually being unlocked - this way once the unlock has been processed it will automatically be purged from the file after the processing of the unlock is complete.
Will this activity be logged?
Yes, when you request the account unlock that go in the first log then when the account unlock his process and completed that will go in the second log
Or request will be tracked, and on process request will remain in the original log locations whereas successful unlocks will end up in the final log location.
What does the look like?
You can see the visual below with example of clicked "Unlock" buttons and original "Unlock" buttons:
Scripting with Powershell
Create the website (with unlock button)
Now we need to create the website that will display the report this is done with a script that I have placed in a folder called "UnlockWriteFile" as this script will produce the website with the unlock button and then also give you the index.html that will be used on the IIS server later in this guide, you will also require the WriteUnlock.aspx file as well.
C:\inetpub\wwwroot
Then we want to create a folder here called Lockout as below:
Then when you start IIS manager and expand Sites>Default Website you will notice that your folder is now shown, if not press F5 and it will appear:
Then we need to right click that folder and choose Convert to Application:
You should then notice is has a global next to the folder rather that the normal folder, this means its is now an application and can run the code required.
Now we need to ensure the authentication for that virtual directory is setup correctly so when you click on the "Lockout" application to the right you should see the authentication option as below:
Double click on the Authentication option which should be set to "anonymous authentication" for this to work, this means anyone can access the website but this will be fixed later with IP address restrictions.
We now need to check the Application Pool permissions assigned to the folder from earlier, so from IIS locate the "Application Pools" as below, then find the DefaultAppPool (as assigned earlier)
Right click on the DefaultAppPool can choose Advanced Settings as below:
Finally under Advanced settings ensure that under Process model>Identity is set to "ApplicationPoolIdentity" as below, if not update this setting and confirm that update with the OK button.
One more place to update permissions then we can continue, now to need to in Explorer view the ACL (or security tab) for that folder, ensure you are in the Advanced options as below:
We need to add some permission for the account which will enable it to write files to this directory only, so the account we need to add is:
IIS APPPOOL\DefaultAppPool
Then when you click OK that will add the Application Pool to the ACL, ensure you place a tick in the "Modify" box and then confirm with the OK as below:
That now concludes the IIS configuration for the website to work as it should, now we can move right along.
Review Lockout Folder
Now when you see a lockout account on the website with the red unlock button as below:
When you click the unlock button and confirm you action with an OK, that then calls WriteUnlock.aspx that in turn records the saAccountName of the user to a text file and the website reports "Requested" as below:
Finally when this script runs you will also get a debug log file, which is highlighted below that will log every request made from the website:
Monitoring unlock_requests.txt and Unlocking Account
Unlock-ADAccount <samAccountName>
Script : ContinualLogging.ps1
This will then continually monitor the unlock requests file for updates and then will process the unlock command when a username is detected as you can see below:
The file is processed so that the raw data in the unlock request file, it ignores all the data excluding the samAccountName which is shown in bold:
We now need to restrict who can use this website to authorised IP addresses, so first we need to ensure we have the pre-requisites installed and this is not installed by default we need the IP security plugin for IIS with the command:
Add-WindowsFeature Web-IP-Security
This will then start the installation as below:
This should then install and complete, you may require a reboot at this stage as well:
You then need to navigate to your folder in IIS manager called Lockout and then on the right look for the IP Address and Domain Restrictions - if you do not see this option after installing it, then you need a reboot.
Double click that option and it will look blank as nothing has been setup we need the "Add Allow Entry" as below:
Then we need to enter either a single IP or a range of addresses:
However this will not stop the access from unauthorised addresses as the "feature settings" have not been setup yet, we now need the option for "edit feature settings" as below: