未加星标

PowerShell for Pentesters Part 4: Modules with PowerShell

字体大小 | |
[系统(windows) 所属分类 系统(windows) | 发布者 店小二03 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏
Introduction

The more we advance in our lab series, the more we notice the power of PowerShell. And it will only become more noticeable.

In this lab we will discover how to manipulate modules with PowerShell. This is a very interesting and important part of learning PowerShell for us, because it will help us a lot in automating many tasks and operations.

Modules with PowerShell

As we have seen in the previous articles, modules represent a very powerful concept on PowerShell. Now we will not learn how to use them (we saw this before), but how to create and exploit them for our pentesting purposes, especially for automating routine tasks.

First, let’s remember the definition of a module. Microsoft describe modules as the following:

“A module is a package that contains PowerShell commands, such as cmdlets, providers, functions, workflows, variables, and aliases.

“People who write commands can use modules to organize their commands and share them with others. People who receive modules can add the commands in the modules to their PowerShell sessions and use them just like the built-in commands.” ( Source )

We can get the list of available modules by using the Get-Module command:


PowerShell for Pentesters Part 4: Modules with PowerShell
Figure 1: Get the list of available modules on PowerShell

The Get-Module command helps us get the modules that have been imported into the current session. And in our example, we used this command with the -ListAvailable option to retrieve the modules that are installed on the computer and can be imported in our session. Adding the option -All makes us able to get all exported files for all available modules in our current session.

We can also import a module very simply by using the Import-Module command:


PowerShell for Pentesters Part 4: Modules with PowerShell
Figure 2: Import module on PowerShell

And then we can remove it using the Remove-Module command like the following:


PowerShell for Pentesters Part 4: Modules with PowerShell
Figure 3: Remove module on PowerShell Writing Modules With PowerShell

Now let’s talk about how to create modules with PowerShell.

The modules on PowerShell have the “.psm1” extension.

We’ll start by creating a sample module. First, we will place a function in a psm1 file to make our module.

There is nothing special in this kind of file (other than the file extension), so we can use a normal PowerShell script containing our functions and rename it to make the psm1 file. Simple, isn’t it?

Here’s a simple function that we will use.

This function simply tries to see if a file exists:


PowerShell for Pentesters Part 4: Modules with PowerShell
Figure 4: Test-File

And there is an important note: We have to add a module manifest to call it really a module.

Manifests simply adds metadata about the concerned module. It includes information like:

Author information Versioning Auto-load of the module

The module manifest is just a hash table saved as psd1file, which will be loaded when we will import our data.

The New-ModuleManifestcmdlet will help us by creating the manifest for us.

So let’s generate our first manifest:


PowerShell for Pentesters Part 4: Modules with PowerShell
PowerShell for Pentesters Part 4: Modules with PowerShell
Figure 5: Creation of manifest on PowerShell

And here’s what our generated manifest will look like:


PowerShell for Pentesters Part 4: Modules with PowerShell

One of the most interesting properties in the manifest is the FunctionsToExport property. It has a default value of *, exporting all functions, but we can also choose specific functions using the following syntax: FunctionsToExport = “GetFunction” .

And finally, let’s talk about RootModule. The root module p arameter simply indicates what PowerShell module file should be run.

Don’t forget to put all files in the same folder, like so:

Modules

└───if_exist

If_exist.psd1

If_exist.psm1

And now let’s import this:


PowerShell for Pentesters Part 4: Modules with PowerShell

The Import-Module command will automatically find our psm1 file in this case.

That’s it.

Congratulations, now you now how to manipulate modules with PowerShell. And you will see, it will be very very, very, useful!

Conclusion

Modules will represent a very useful concept that will help us a lot.

In the next articles, we will begin exploiting real use cases with PowerShell and begin to talk about remoting with PowerShell.

Let’s go to the next level. See you soon, my friends!

Brace yourself: the best is coming! Prev: PowerShell for Pentesters Part 3: Functions and Scripting with PowerShell

本文系统(windows)相关术语:三级网络技术 计算机三级网络技术 网络技术基础 计算机网络技术

tags: PowerShell,will,modules,module,our,And
分页:12
转载请注明
本文标题:PowerShell for Pentesters Part 4: Modules with PowerShell
本站链接:https://www.codesec.net/view/597035.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(windows) | 评论(0) | 阅读(11)