# Hacking with Powershell

This room can found [here](https://tryhackme.com/room/powershell). It covers: what is Powershell , how it works, basic Powershell commands, windows enumeration with Powershell, and Powershell scripting

Machine credentials:

* Username: Administrator
* Password: BHN2UVw0Q

## Task 2

This also allows Powershell to execute .NET functions directly from its shell. Most Powershell commands, called *cmdlets,* are written in .NET.&#x20;

Unlike other scripting languages and shell environments, the output of these *cmdlets* are objects, making Powershell somewhat object oriented. This also means that running cmdlets allows us to perform actions on the output object, which makes it convenient to pass output from one *cmdlet* to another. The normal format of a *cmdlet* is represented using **Verb-Noun.**

Common verbs to use include:

* Get
* Start
* Stop&#x20;
* Read
* Write
* New
* Out

Full list of approved verbs are [here](https://docs.microsoft.com/en-us/powershell/scripting/developer/cmdlet/approved-verbs-for-windows-powershell-commands?view=powershell-7).

### Question 1: What is the command to get help about a particular cmdlet(without any parameters)?

`Get-help`

## Task 3

`Get-Command` and `Get-Help` are our best friends!

**Using Get-Help**

Get-Help displays information about a *cmdlet.* To get help about a particular command, run `Get-Help Command-Name`

We can also understand how exactly to use the command by passing in the `-examples` flag.

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2Fi0kR3U1QybjwwgH5DyQg%2Fimage.png?alt=media\&token=64e983d9-ed0b-47c3-859d-1ed34ae8ac92)

#### Using Get-Command

Get-Command gets all the *cmdlets* installed on the current Computer. This *cmdlet* allows for pattern matching such as `Get-Command Verb-*` or `Get-Command *-Noun`

Running `Get-Command New-*` to view all the *cmdlets* for the verb new displays:

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FcntdDTHxXF9P8uTPqCs6%2Fimage.png?alt=media\&token=352c9ff3-a1a5-4fdb-99e2-4b118fca3e92)

#### Object Manipulation

If we want to actually manipulate the output, we need to figure out a few things:

* passing output to other *cmdlets*
* using specific object *cmdlets* to extract information

The Pipe ( | ) is used to pass output from one *cmdlet* to another. A major difference compared to other shells is that instead of passing text or string to the command after the pipe, powershell passes an object to the next cmdlet. Like every object in object oriented frameworks, an object will contain methods and properties.&#x20;

We can think of methods as functions that can be applied to output from the *cmdlet* and we can think of properties as variables in the output from a cmdlet. To view these details, pass the output of a *cmdlet* to the Get-Member *cmdlet* `Verb-Noun | Get-Member`&#x20;

An example of running this to view the members for Get-Command is:

`Get-Command | Get-Member -MemberType Method`

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2F2ssRoIyNejSHYlXUNpl8%2Fimage.png?alt=media\&token=39c1e9b3-54e5-4e43-b9cd-7d0696ad028c)

#### Creating Objects From Previous *cmdlets*

One way of manipulating objects is pulling out the properties from the output of a cmdlet and creating a new object. This is done using the `Select-Object` *cmdlet.*&#x20;

An example of listing the directories and just selecting the mode and the name:

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FQc5OfHZcuyD633cmUL4s%2Fimage.png?alt=media\&token=37d640cd-ce57-4931-988b-b71930149bd6)

We can also use the following flags to select particular information:

* first - gets the first x object
* last - gets the last x object
* unique - shows the unique objects
* skip - skips x objects

#### Filtering Objects

When retrieving output objects, we *may* want to select objects that match a very specific value. We can do this using the `Where-Object` to filter based on the value of properties.&#x20;

The general format of the using this *cmdlet* is:

`Verb-Noun | Where-Object -Property PropertyName -operator Value`

`Verb-Noun | Where-Object {$_.PropertyName -operator Value}`

The second version uses the $\_ operator to iterate through every object passed to the Where-Object cmdlet.

**Powershell is sensitive so make sure we don't put quotes around the command**

Where `-operator` is a list of the following operators:

* -Contains: if any item in the property value is an exact match for the specified value
* -EQ: if the property value is the same as the specified value
* -GT: if the property value is greater than the specified value

Full list of operators can be found [here](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/where-object?view=powershell-6).

An example of checking the stopped processes:

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2F7dFGqwQGLoFc8maTsZEj%2Fimage.png?alt=media\&token=bd23a725-7e91-4778-912b-b14838ed2b56)

#### Sort Object

When a *cmdlet* outputs a lot of information, we may need to sort it to extract the information more efficiently. We do this by pipe lining the output of a *cmdlet* to the `Sort-Object` *cmdlet*.

The format of the command would be

`Verb-Noun | Sort-Object`

An example of sort the list of directories:

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FRrTBIviWchL2bAC3DE5s%2Fimage.png?alt=media\&token=bf3ee3ac-f232-48d6-a830-2eb7fd4d3bd1)

### Question 1: What is the location of the file "interesting-file.txt"

`C:\Program Files` - Running `Get-ChildItem -Path C:\ -Recurse -File interesting*.txt -ErrorAction SilentlyContinue` will give us our answer.

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FaIyW3b6Veik1pmH38dIL%2Fimage.png?alt=media\&token=2795d8a6-1811-44b9-b761-efd0ddd3088f)

### Question 2: Specify the contents of this file

`notsointerestingcontent` - Running `Get-Content 'C:\Program Files\interesting-File.txt.txt'` will give us our answer

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FI4741BasosUDOU1p9ESq%2Fimage.png?alt=media\&token=d6f0db1c-a42d-4502-be67-2e353a0dbdab)

### Question 3: How many cmdlets are installed on the system(only cmdlets, not functions and aliases)?

`6638` - Running `Get-Command -CommandType Cmdlet | measure` will give us our answer. Using Measure calculates the numeric properties of objects, and the characters, words, and lines in string objects, such as files of text. Helpful when parsing or searching for larger amount(s) of information.

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FM30wal7YV4rLOOchVnPA%2Fimage.png?alt=media\&token=28b05220-4409-40d1-af3c-dfa4383adbb0)

### Question 4: Get the MD5 hash of interesting-file.txt

`49A586A2A9456226F8A1B4CEC6FAB329` - Running `Get-FileHash 'C:\Program Files\interesting-file.txt.txt' -Algorithm MD5` will give us our answer.

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FPZCI3MfZ8uIyEVUUg4sr%2Fimage.png?alt=media\&token=e59a07f6-e61a-4e60-b234-7b0030c4f1d9)

### Question 5: What is the command to get the current working directory?

`Get-Location`

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FZ8VcxhkdXFjEnijZDI0r%2Fimage.png?alt=media\&token=21a46cb9-d47d-4c17-84b1-d2a7def6d392)

### Question 6: Does the path "C:\Users\Administrator\Documents\Passwords" Exist(Y/N)?

`N` - Running `Get-Location -Path 'C:\Users\Administrator\Documents\Passwords'` will give us our answer

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2F50wMUBhOvHcQ3VgJPxBn%2Fimage.png?alt=media\&token=e026546a-2dd8-4181-9690-dc04fd590fd0)

### Question 7: What command would you use to make a request to a web server?

`Invoke-WebRequest`

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FWdcI6k0DaidLdGfPGUnk%2Fimage.png?alt=media\&token=b6510ce0-2043-44a5-b6c8-9b5926d1f035)

### Question 8: Base64 decode the file b64.txt on Windows

ihopeyoudidthisonwindows- Find the file first, `Get-ChildItem -Path C:\ -Recurse -File b64.txt -ErrorAction SilentlyContinue`

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FfG0HLKJkH1WZSpiRyoOP%2Fimage.png?alt=media\&token=a35b8ca3-317e-4912-94e5-e2ece19d4bed)

Now we can decode the base64 and output to a file, then get the contents of the new file.

`certutil -decode 'C:\Users\Administrator\Desktop\b64.txt' decoded.txt`

`Get-Content 'C:\Users\Administrator\Desktop\decoded.txt'`

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FwR0iZvUM8hhawjr29xl6%2Fimage.png?alt=media\&token=dfb1639d-28c0-4425-95b4-1835a8ee65ea)

## Task 4

The first step when you have gained initial access to any machine would be to enumerate. We'll be enumerating the following:

* users
* basic networking information
* file permissions
* registry permissions
* scheduled and running tasks
* insecure files

Your task will be to answer the following questions to enumerate the machine using Powershell commands!&#x20;

### Question 1: How many users are there on the machine?

`5` - Running `Get-LocalUser` will give us our answer

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FYPe5D7c5rarSyic6zeOg%2Fimage.png?alt=media\&token=0e4892cb-37bb-4a2f-83b4-ee65f5f80f76)

### Question 2: Which local user does this SID(S-1-5-21-1394777289-3961777894-1791813945-501) belong to?

`Guest` - Running `Get-LocalUser -SID "S-1-5-21-1394777289-3961777894-1791813945-501"` gives us our answer

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2F7qZY2sPraeh0Rux1qWKN%2Fimage.png?alt=media\&token=adbe22c4-677b-4871-978b-8234b05e7519)

### Question 3: How many users have their password required values set to False?

`4` - Run `Get-LocalUser | Where-Object -Property PasswordRequired -Match false` to find the answer.

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2F64rxHDEFZkUElH9bbBSQ%2Fimage.png?alt=media\&token=446e4365-900e-4abd-a9ec-bd63288e55b7)

### Question 4: How many local groups exist?

`24` - Run `Get-LocalGroup | Measure` to find the answer

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FIcgbqpo6r916VvmqOxF5%2Fimage.png?alt=media\&token=4fa46791-0451-4ad2-913f-94802f0698ef)

### Question 5: What command did you use to get the IP address info?

`Get-NetIPAddress`

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2Fp4pQl8zKl3vbjf7jHJVd%2Fimage.png?alt=media\&token=49c996b5-d1d1-4a9c-8eb3-aff2a3e2d38f)

### Question 6: How many ports are listed as listening?

`20` - Run `Get-NetTCPConnection | Where-Object -Property State -Match Listen | Measure` to find our answer.

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FfkKP3HmhWOBpsm7PGaMw%2Fimage.png?alt=media\&token=7700dc55-111e-4fe8-abf0-ae3270c22e0e)

### Question 7: What is the remote address of the local port listening on port 445?

`::` - Running `Get-NetTCPConnection | Where-Object -Property State -Match Listen | findstr "445"` will show us our answer.

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FuSLnBoJHK8nlqkCIZnZG%2Fimage.png?alt=media\&token=b35d6cc8-e208-438a-8f9f-86bb01ef96a1)

### Question 8: How many patches have been applied?

`20` - Run `Get-Hotfix` and count OR `Get-Hotfix | Measure`

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FumcXqHunQJtRPBS152KF%2Fimage.png?alt=media\&token=beb52ba3-35e4-492b-88fa-3df8e9a8f43a)

### Question 9: When was the patch with ID KB4023834 installed?

`6/15/2017 12:00:00 AM` - In the above screenshot we can find the answer, BUT we can always run `Get-Hotfix | findstr "KB4023834"` OR `Get-Hotfix -Id KB4023834`

### Question 10: Find the contents of a backup file.

`backpassflag` - First we find the backup file `Get-ChildItem -Path C:\ -include *.bak* -File -Recurse -ErrorAction SilentlyContinue`

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FeebVlLhVDmXFy5t3syGg%2Fimage.png?alt=media\&token=53ce46a0-8a99-4567-9098-4e4e34c61556)

Now we get the contents of that file `Get-Content 'C:\Program Files (x86)\Internet Explorer\passwords.bak.txt'`

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2F6umhQcZDqaFoXV3wS6sF%2Fimage.png?alt=media\&token=7d540515-bac5-481d-b82e-141e69857e6b)

### Question 11: Search for all files containing API\_KEY

`fakekey123` - We can run `Get-ChildItem C:* -Recurse | Select-String -pattern API_KEY` to find the answer. After a while we see an error code.

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FT0XrpoTbf2LKscJzSfkp%2Fimage.png?alt=media\&token=cf3da386-8305-49a9-b456-00af8c6fd810)

### Question 12: What command do you do to list all the running processes?

`Get-Process`

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FUnbY3YaOvFXf146e7KTN%2Fimage.png?alt=media\&token=06afa253-46d3-4b6b-8afa-7560a72c63c5)

### Question 13: What is the path of the scheduled task called new-sched-task?

`/` - We can run `Get-ScheduledTask -TaskName new-sched-task` and obtain our answer

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FgXmQmtSTglRKulugwmv1%2Fimage.png?alt=media\&token=1597e5ed-1d42-4d61-a4ae-f38f3a64ad14)

### Question 14: Who is the owner of the C:\\?

`NT SERVICE\TrustedInstaller` - Running `Get-Acl c:/` will show us the owner.

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2F3oVWOZtDuMsLcNOYfOq9%2Fimage.png?alt=media\&token=e3d2b06b-0f6e-4f26-a02f-cde237c83242)

## Task 5

We'll be using PowerShell ISE(which is the Powershell Text Editor). To show an example of this script, let's use a particular scenario. Given a list of port numbers, we want to use this list to see if the local port is listening. Open the listening-ports.ps1 script on the Desktop using Powershell ISE. Powershell scripts usually have the *.ps1* file extension.&#x20;

```
$system_ports = Get-NetTCPConnection -State Listen
$text_port = Get-Content -Path C:\Users\Administrator\Desktop\ports.txt
foreach($port in $text_port){
    if($port -in $system_ports.LocalPort){
        echo $port
     }
}
```

On the first line, we want to get a list of all the ports on the system that are listening. We do this using the Get-NetTCPConnection *cmdlet*. We are then saving the output of this *cmdlet* into a variable. The convention to create variables is used as:

```
$variable_name = value
```

On the next line, we want to read a list of ports from the file. We do this using the Get-Content *cmdlet.* Again, we store this output in the variables. The simplest next step is iterate through all the ports in the file to see if the ports are listening. To iterate through the ports in the file, we use the following

```powershell
foreach($new_var in $existing_var){}
```

This particular code block is used to loop through a set of object. Once we have each individual port, we want to check if this port occurs in the listening local ports. Instead of doing another for loop, we just use an if statement with the `-in` operator to check if the port exists the LocalPort property of any object. A full list of if statement comparison operators can be found [here](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-6). To run script, just call the script path using Powershell or click the green button on Powershell ISE:

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FcN0Ix7LRzmzHDEPzUjeS%2Fimage.png?alt=media\&token=6693d1b4-e57e-4cb0-a970-e4fece404e11)

Now that we've seen what a basic script looks like - it's time to write one of your own. The emails folder on the Desktop contains copies of the emails John, Martha and Mary have been sending to each other(and themselves). Answer the following questions with regards to these emails(try not to open the files and use a script to answer the questions). <br>

Scripting can be a bit difficult, but [here](https://learnxinyminutes.com/docs/powershell/) is a good resource to use.

### Question 1: What file contains the password?

`Doc3m` - So we can essentially make a script that will run powershell commands from a file by storing them into variables and calling them throughout the script.

```
$path = "C:\Users\Administrator\Desktop\emails*"
$string_pattern = "password"
$command = Get-ChildItem -Path $path -Recurse | Select-String -Pattern $String_patterne
cho $command
```

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2FUExqNh2JCy5nzKvIdWy5%2Fimage.png?alt=media\&token=1cc6142f-2101-4fc7-b743-6e5ab02e11bb)

### Question 2: What is the password?

`johnisaleggend99` - From the script/command we wrote above, it has our answer as well.

### Question 3: What files contains an HTTPS link?

`Doc2Mary` - We can edit our script above and change the string from `password` to `https://`

```
$path = "C:\Users\Administrator\Desktop\emails*"
$string_pattern = "https://"
$command = Get-ChildItem -Path $path -Recurse | Select-String -Pattern $String_patterne
cho $command
```

![](https://667808901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTdW22AGCceN8oUXfdlKI%2Fuploads%2F7prKnYVvGIO9b5mLjifP%2Fimage.png?alt=media\&token=4e727595-59f2-4741-b6ed-c4adf142ca42)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cybersec.th4ntis.com/tryhackme/hacking-with-powershell.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
