Install PowerShell on Kali Linux

I tried to install PowerShell on Kali Linux Rolling by following instructions on the GitHub page as well as other articles I found online and none of them worked. I’m going to tell you what worked for me.

In the past I’ve stuck to Bash and Python for all of my scripting needs because they work cross platform. My work issued laptop runs Windows 10 and I use Git Bash to run my simple shell scripts that I use mainly to slice, dice, and reformat data, and Python for everything else. I’m a big fan of using one cross platform scripting language when possible.

Lately I’ve found a need to dive into PowerShell to be able to understand a complex script that I took over from a departing coworker. I was really surprised at how easy it is to work with XML using PowerShell after struggling to read XML with Python and xmlstarlet. Add in some Unicode and dependency problems while switching back and forth between Python 2.7 and 3.5 and I knew is was time to give PowerShell a chance. This had me thinking about starting a personal project to create a cross platform script in PowerShell to manage pentests and reporting.

Let’s get started installing PowerShell on Kali.

First, open a terminal and run “apt-get install libunwind8 libicu55”. Next you’ll need to download libssl1.0.0 from Debian and install with the command “dpkg -i libssl1.0.0_1.0.1t-1+deb8u6_amd64.deb”. Now you can download the PowerShell Ubuntu 16.04 deb package and install using “dpkg -i powershell_6.0.0-alpha.18-1ubuntu1.16.04.1_amd64.deb”. Now you should be able to enter “powershell” in your terminal to run it.

Using Python for IP addresses

Part of the day to day tasks of any Information Security professional may include transforming system input and output text and calculating IP addresses. In this article I demonstrate how use Python to work with IP addresses and transform system input/output.

A system that I manage at work doesn’t allow you to enter networks in CIDR notation like 192.168.0.0/24, it requires starting and ending IP addresses in a range like 192.168.0.1-192.168.0.254. In the past I’ve used ipcalc on Linux to calculate IP addresses when I have something other than a simple netmask to calculate. Today I had a list of IP subnets of various netmasks that I needed to transform into a comma separated list in the format of 192.168.0.1-192.168.0.254, 10.128.0.1-10.128.0.254…

I’m going to use the ipcalc module for Python to demonstrate how to do this. You can install it from an administrator cmd prompt or using root or sudo in a Linux terminal with the following command:

pip install ipcalc

Pretty simple to install. Now lets move on to a demonstration of how to use it. After importing the ipcalc module, I created a new object and attempted to output the first and last host on the network but it didn’t work quite as expected until I cast the output to string.

Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import ipcalc
>>> subnet = ipcalc.Network('192.168.0.0/24')
>>> print subnet.host_first
<bound method Network.host_first of Network('192.168.0.0/24')>
>>> print str(subnet.host_first)
<bound method Network.host_first of Network('192.168.0.0/24')>
>>> print(str(subnet.host_first()))
192.168.0.1
>>> print str(subnet.host_first())
192.168.0.1
>>> print str(subnet.host_last())
192.168.0.254
>>> print str(subnet.broadcast())
192.168.0.255
>>> print str(subnet.info())
PRIVATE
>>> print str(subnet.to_ipv6())
2002:c0a8:0000:0000:0000:0000:0000:0000
>>> print str(subnet.size())
256
>>> ipaddress = ipcalc.IP('192.168.0.128/24')
>>> print str(ipaddress.subnet())
24
>>> print str(ipaddress.info())
PRIVATE
>>> print str(ipaddress.guess_network())
192.168.0.0/24
>>>

Now I’m going to take an input file with a network name followed by network address in CIDR format on each line, cut out just the subnet, and output the networks in firsthost-lasthost,firsthost-lasthost,…” format.

>>> with open('ips.txt') as f:
    content = f.readlines()

    
>>> content = [x.strip() for x in content]

>>> import sys
>>> for x in content:
    subnet = x.split(' ')[-1]
    networkobj = ipcalc.Network(subnet)
    sys.stdout.write("%s-%s," % (str(networkobj.host_first()),str(networkobj.host_last())))

In the code above, I loop through each line of the file and get the last word in the string which is the subnet address. It was necessary to use sys.stdout.write() to get rid of the space between each network start and end addresses using Python 2.7. I’m not going to show you the output. If you want to know my public IP addresses then you’ll have to work to find them yourself. 🙂

I’m sure that there are more than one way to write the code for this exercise. If you have any constructive feedback please leave a comment. Thanks for visiting!