r/PowerShell • • Mar 10 '21

And today in "Making IT security professionals cry"

So background: This month my team are having a bit of a tidy-up/purge of our Azure Automation account. There's hundreds of runbooks in there assigned to dozens of different schedules, tons of duplicates and stuff people made then left sitting "in edit". Very nasty.

I was browsing down the list and I found one made by a former colleague who has now left the business. It's a powershell runbook, so no problem, I think to myself.

I was wrong. (Names have been changed to protect the guilty)

$cred = Get-AutomationPSCredential -Name 'domainadmin'
New-PSDrive -name 'Q' -Root '\\UNCpath\touserfolder\that\is\open\to\all\users\'
Set-Location Q:\
$process = Start-Process -FilePath C:\Python37\python.exe -ArgumentList .\pythonscript.py -NoNewWindow -Wait -Credential $cred
$errorcode = $process.ExitCode
if($errorcode -ne 0){
Write-Error -Message "Error: $errorcode" -Category NotSpecified
   throw "Python script returned an error"
} else {
   Write-Output "Exit Code: $errorcode"
}

I can't. I just can't. This is just too disgusting.

This is now top of my list of things to replace with something that won't make the head of security cry bitter, salty tears.

129 Upvotes

58 comments sorted by

27

u/computerbob Mar 10 '21

I just got rid of a similar process in our environment. The PowerShell enumerated any *.ps1 files in a folder and ran them all. The intent was to process any scripts that were put in that folder. The problem is that there is no auditing or version control on the folder and the share and NTFS permissions for the folder were set to 'everyone'. I showed it to my boss and she didn't understand the problem until I showed her a 3 line script that would have deleted every AD account in our system. I showed her how all I had to do was copy that to the shared folder and wait for the automation to run it for me (which already runs as an account that has access to create/modify/delete AD users).

Once she figured out how a 3 line powershell script could basically take down our company of over 10k people, she realized why I was concerned.

15

u/bee_administrator Mar 10 '21

Hey that actually sounds really efficient. A script that just says "Run all the things!", I wonder why more people don't do that? /s

6

u/[deleted] Mar 11 '21

Bro how can anyone blindly run a script without explicit instruction? It blows my mind.

3

u/become_taintless Mar 11 '21

a lot of people simply don't think past "will this meet my current need?" and security is just not on the list of concerns

72

u/davidnait Mar 10 '21

There is almost always a reason why there is shitty code. Most common in my experience is money, or the lack of it. “Please make a spaceship for $0 in 5min.”

Edit: The Python script would be interested to see. I mean the Powershell script in itself isn’t that bad IMO.

13

u/bee_administrator Mar 10 '21

The python itself isn't so bad, despite it being, y'know Python (not a fan). It just pulls some data from an API on one of our internal services, formats it and emails a report. No idea why he couldn't just use Powershell for that rather than this...abomination.

The Powershell itself? It's fair, it does exactly what it says on the tin. It's just the complete lack of any common sense in implementing that way that triggered me. You've got domain admin creds blindly running a script that any disgruntled scrub with access to the network share (ie everyone) could modify. It's just asking for some arsehole to make it do something horrible...

2

u/32178932123 Mar 10 '21

Out of curiosity, would it be frowned upon to set up a similar script but make it so it runs it from a Read-Only share?

In the past I've asked users to click on a Batch File which then just triggers a powershell script on a share that they have Read-Only access to... That way I could update the script without them knowing.

8

u/bee_administrator Mar 10 '21

I mean, you could do that. It would solve the issue of the script being an attack vector. But thing is Azure Automations can run Python runbooks natively, there's literally no need to have a Powershell wrapper calling a .py file on a local file share no matter how secure you make the folder.

4

u/32178932123 Mar 10 '21

Ah sorry yeah that's my lack of Azure knowledge :) I'd not heard of a runbook until I saw your post and googled it.

20

u/da_chicken Mar 10 '21

It just pulls some data from an API on one of our internal services, formats it and emails a report. No idea why he couldn't just use Powershell for that rather than this...abomination.

APIs can be a pain in the ass with Powershell. Firstly because every doc API ever assumes you're using Python, Javascript, or curl so you have to translate the API headers to something Invoke-WebRequest understands.

Secondly, the default JSON parser in .Net, and therefore in ConvertTo-Json and ConvertFrom-Json, uses Microsoft specific formats for some elements like datetimes. This is why the JSON.NET library is so damn popular in .Net applications. It's not only faster, it also formats things correctly so that you can, you know, actually serialize data to a foreign goddamn system, Microsoft.

6

u/jantari Mar 11 '21

All of your JSON complaints are adressed in PowerShell 6/7

3

u/BlackV Mar 11 '21

that's why you use invoke-rest instead

2

u/JiveWithIt Mar 11 '21

I have found one of the most useful skills within system automation/integration is being able to translate logic from one language to another. It gets easy with practice.

I often find myself translating C# to PowerShell, but also JavaScript.

5

u/ApricotPenguin Mar 10 '21

Maybe it's the other way around?

Both scripts were created then either folder became Writable to everyone, or it was moved to this new location and someone simply modified the path?

5

u/davidnait Mar 10 '21

I get that. It is bad. Don’t get me wrong. :) I’m just saying that there is almost always a reason till why bad scripts exist. Trust I’ve seen far worse out there. 😰

3

u/HalfysReddit Mar 11 '21

there is almost always a reason till why bad scripts exist

IME it largely comes down to ignorance. Some tech gets excited over being able to flex their creative muscles but doesn't recognize the importance of following best practices while building their toy.

1

u/not_rholliday Mar 11 '21

What is the use case for a shared folder that all users have read AND write to?

77

u/weebsnore Mar 10 '21

The bad thing is that any user on the domain can write the contents of the Python script which is then run as Domain Admin!

19

u/sys_unknown Mar 10 '21 edited Mar 10 '21

the folder share may be open to all users but may not be writable to all users. but then he should have avoided putting the python file on a shared folder and running it as admin.

12

u/wrexinite Mar 11 '21

Privilege escalation like a mfer

16

u/PMental Mar 10 '21

I have a hard time coming up with a need to run a script as domain admin from a share anyone has access too though, no matter the situation. Tons of better ways run run whatever was in the .py and not many worse that I can think of.

5

u/HalfysReddit Mar 11 '21

Shit I rarely need to do anything as a domain admin. I work at an MSP and once we set up some standardized security groups there's very little we'd need to use domain admin for other than actually standing up/replacing domain controllers.

1

u/DestroyerOfIphone Mar 11 '21

I don't use the account Administrator but i have to elevate my privilege's frequently in Windows networks.

3

u/HalfysReddit Mar 11 '21

But that's just being a local admin, which is entirely reasonable. Domain admin is effectively given full access to everything (the only exception being if your environment has multiple domains), and it's not very common that you actually need that level of access.

1

u/DestroyerOfIphone Mar 11 '21

My admin account is domain admins rights. I suppose if you're large enough company you could segregate your admins into OUs. But realistically I've never worked anywhere that does that.

3

u/mustang__1 Mar 12 '21

I don't want to invoke my domain admin account just so I can, for example, run rufus on my computer. Thus I have a local admin account to invoke instead.

4

u/jtmott Mar 10 '21

I got a guy that can do it faster and cheaper 😂

1

u/Pauley0 Mar 12 '21
  • Good

  • Fast

  • Cheap

Pick 2 of the above. :)

2

u/budlight2k Mar 11 '21

The space ship can run on this open source software. We don't have the money for a license.

2

u/davidnait Mar 11 '21

Trial licenses works fine as well!

1

u/budlight2k Mar 11 '21

Yeah we only need one user anyway they can all share.

1

u/[deleted] Mar 11 '21

Well yes, I think the most common reason for bad code is someone high up on the corporate ladder saying "please make a spaceship for $0 in 5 minutes."

But I think a close second would be an inexperienced IT person going, "I just learned some PowerShell, and I have a really clever idea!" and failing to anticipate all the ways that their idea could go wrong.

0

u/[deleted] Mar 11 '21

[deleted]

0

u/WorkJeff Mar 11 '21

which would take time and money

12

u/networkgod Mar 10 '21

Sounds like a similar issue I ran into with a scheduled task deployed via GPO to run a batch file.

The setting was setup to create the Scheduled Task with THE root administrator account for the domain...and placed into the Default Domain GPO.

The best part? That meant that account touched every.single.computer.in.the.domain, and then the cherry on top? The password was stored in a easily reversible hash, which because of how the GPO is read and deployed, meant that hash was also on every single computer in the domain.

This is not quite the same thing, but this article talks about the same method the creds got "decrypted" in my scenario: https://blog.compass-security.com/2012/04/exploit-credentials-stored-in-windows-group-policy-preferences/

The sad part is that doing this exact process, AD\GPO warns you about storing the password since it's essentially clear text, at least in 2012 or later. This was done in...oh, around 2017, so no way could that have been something the person who deployed that ignored /s.

That person is now a higher level manager, so you know...don't have to be good, just proclaim you're good long and loud enough and people will believe you. Fix a small problem here, create a larger one there...hope nothing happens, you're good.

3

u/bee_administrator Mar 10 '21

Wait so you end up with domain admin creds cached on every machine on the domain? I...I just have no words for this. I hope this GPO is called something pithy like TIMEBOMB

2

u/networkgod Mar 10 '21

Yup! No, they didn't have the awareness of what they were doing to call it anything good, unfortunately. I'm still cleaning up the problems this guy created and continued to create. He's still here, and still making messes.

10

u/billr1965 Mar 10 '21

The goal should be running scripts (Powershell, Python, whatever) with the LEAST permissions necessary to do the job. I wrote a Ps script that polls vcenter and older netapps and emails report. It needs credentials to connect to vsphere and Netapps, but those ID's essentially have read-only permissions in both environments. Is it a potential point of entry? YES, but even if it were hacked they could only query the environments, not make any changes.

3

u/bee_administrator Mar 10 '21

Exactly that. If your runbook doesn't need to make changes to anything, it doesn't need more than read permissions on whatever data source you're interacting with.

2

u/WorkJeff Mar 11 '21

The goal should be running scripts (Powershell, Python, whatever) with the LEAST permissions necessary to do the job.

And yet we put tons of trust into network management and security software. "We need 'Local Admin' on the domain controllers to ensure the domain controllers are safe"

1

u/billr1965 Mar 13 '21

I agree that most vendors and people are lazy and don't put the time in to determine what permissions they need so give them all to me and things will work.

2

u/6T9Burner Mar 10 '21

Holy Jeebus, Hooserdaddy, and Marybell on a fuckin donkey... I had to reread to make sure I read that correctly... but couldn't because my eye was already twitching in countersync with my cheek.

Who?! Why?! How did this pass a sec audit or at least a buddy check?

1

u/bee_administrator Mar 10 '21

It didn't at the time, we had no idea this was a thing until today. I've binned the whole thing so I can replace it with something sensible tomorrow.

3

u/davidnait Mar 10 '21

Grab the working .py-file and run in as a Python run book and it sounds like you’re golden. The Powershell code is just overhead.

3

u/bee_administrator Mar 10 '21

I could do, but for my own peace of mind I'm just going to rewrite it in PS.

Completely besides the security bit, the maintenance overhead (since the person who wrote it was the only team member who can write Python), etc etc...the report the current version outputs is bare text, I have too much self-respect not to embetter it and put a nice bit of HTML/CSS round it, just to save wear and tear on peoples' eyes.

0

u/[deleted] Mar 10 '21

[deleted]

-1

u/bee_administrator Mar 10 '21

Lol no, I'm fine thanks. :)

0

u/[deleted] Mar 10 '21

[deleted]

4

u/Garegin16 Mar 10 '21

PS is basically Python with an interactive shell. I mean honestly they could have just built a Python for .NET.

I do laugh at people who say that PS is a glorified Bash/CMD, though. It’s really not. You can’t “improve” on shells without introducing a type system. I never understood why the Unix Hater’s handbook called shells “bad programming languages” until recently.

0

u/[deleted] Mar 10 '21

[deleted]

2

u/bee_administrator Mar 11 '21

" I just don't see a role for PS for anything other Windows administration."

See if you'd said that 5-10 years ago you would have been close to the mark. There's all sorts of ridiculous things people have done with PS recently though.

Case in point, I made an interactive dashboard for my teammates at work, it's made with Powershell Universal Dashboard and runs as an Azure web app. All the version control and CI/CD for the thing is also handled exclusively through PS (well, Azure DevOps, but I interact with that using PS too).

Yeah the primary use for PS is Windows administration, but there's so much more you can do with it these days :)

→ More replies (0)

1

u/Garegin16 Mar 11 '21

Why not. It’s basically another dynamic scripting language. How different is Python from Powershell, really?

22

u/mrb4gm4n Mar 10 '21

I think there may have been a posting error, I hear r/ShittySysadmin is calling /s

7

u/Mechanical_Monk Mar 11 '21

Reminds me of a previous job where our CIO insisted we stopped using batch scripts, and replace them all with PowerShell scripts. Thing is, none of us knew PS at the time, and the company wouldn't provide any training. So my coworker just "ported" everything to PS by just calling "cmd /c" from ps1 files.

5

u/PM_ME_UR_CEPHALOPODS Mar 11 '21

This sub is full of breathless admins. This is bush league on the oh shit scale.

2

u/Garegin16 Mar 10 '21

Most ITs think security is strong passwords. I used to work in an MSP with atrocious security practices. The admin was a “30 year veteran” that half assed everything. I don’t think she ever read any IT books, but learned all with googling.

They had client servers that were unpatched for years.

2

u/Disorderly_Chaos Mar 11 '21

I’ve slowly been retooling all of my scripts from years ago (when I cut/pasted code only and had only a little idea of what PS could do).

Now I remember I need to make debug scripts for when stuff breaks.

1

u/serendrewpity Mar 10 '21

Colin Robinson: MMMMMmmmmmm, I love the tears of IT Professionals. Yummy!

1

u/thatto Mar 10 '21

You don’t have to worry about code signing if you use python!

1

u/Raymich Mar 10 '21

Fantastic twist lol, but question bit off topic if you don’t mind.

Is it true that Automation Run As account can only work in subscription scope and I can only limit it from there using NotActions?

I was hoping I could just clone contributor role and reduce scope to a single resource group, but it didn’t work.

1

u/elevul Mar 11 '21

This executes in the cloud or on a machine running the hybrid worker?

1

u/user01401 Mar 11 '21

Truthfully if a user is interactive in that shell they can get all credentials anyway, not just that one.