PowerShell for Pentesters

The more we advance in our articles, the more we notice the power of PowerShell, and that impression will only increase as we move forward.

In this article, we will try to focus on Scripting and Functions with PowerShell.

As we’ve seen for all concepts with PowerShell so far, functions are also very simple to use.

To use them, all you have to do is to use the following syntax:

function [<scope:>]<name> [([type]$parameter1[,[type]$parameter2])]


param([type]$parameter1 [,[type]$parameter2])

dynamicparam {<statement list>}

begin {<statement list>}

process {<statement list>}

end {<statement list>}


It can be very simple or very complex, depending on the context.

Now let’s discover how to use functions with PowerShell. We’ll begin with a simple function: multiplying 4 by 3.

Figure 1: Simple function with PowerShell

It can also be used with arguments by using the object $args as an array, and the positions will simply represent the order of arguments that we will get from the user. We can see this in the following screenshot:

Figure 2: Exploiting functions with arguments

We can also use named parameters as following:

Figure 3: Exploiting named parameters in functions with PowerShell

There is also a very interesting feature with parameters. We can monitor the type of parameters that are parsed.

In the following example, we will try to filter and permit only integer parameters to be parsed. Anything else will launch an exception.

Figure 4: Manipulating type with functions

Don’t forget: I’m only presenting to you the basics needed, but there’s a lot of things you can discover for yourself, especially when using advanced functions like parameters and attributes or working with parameter validation. We’ll discuss some examples later.

Now, let’s talk a little bit about scripting with PowerShell. But before we began to (Read more...)

*** This is a Security Bloggers Network syndicated blog from InfoSec Resources authored by Kondah Hamza. Read the original post at: http://feedproxy.google.com/~r/infosecResources/~3/cKpur3BUd5s/