Hacker Tools
Hacker Tools
Hacker Tools
Introduction
Data Wrangling
Conclusion
2/96
Introduction Shell and Scripting Data Wrangling Conclusion
NUS Hackers
http://nushackers.org
hackerschool
Friday Hacks
Hack & Roll
NUS Hackerspace
3/96
Introduction Shell and Scripting Data Wrangling Conclusion
About Me
4/96
Introduction Shell and Scripting Data Wrangling Conclusion
5/96
Introduction Shell and Scripting Data Wrangling Conclusion
Table of Contents
Introduction
Data Wrangling
Conclusion
6/96
Introduction Shell and Scripting Data Wrangling Conclusion
Required Software
Linux1
macOS2
BSD
Other Unix-like OS’es (Minix, Solaris, AIX, HP-UX, etc.)
1
For beginners, Ubuntu is recommended. Either dual-boot or install
as virtual machine using VirtualBox
2
Open Terminal, and run xcode-select --install first
7/96
Introduction Shell and Scripting Data Wrangling Conclusion
8/96
Introduction Shell and Scripting Data Wrangling Conclusion
9/96
Introduction Shell and Scripting Data Wrangling Conclusion
Introduction
Data Wrangling
10/96
Introduction Shell and Scripting Data Wrangling Conclusion
Introduction to Shell
12/96
Introduction Shell and Scripting Data Wrangling Conclusion
Common Commands
13/96
Introduction Shell and Scripting Data Wrangling Conclusion
14/96
Introduction Shell and Scripting Data Wrangling Conclusion
15/96
Introduction Shell and Scripting Data Wrangling Conclusion
Script (1/2)
1 #!/bin/sh
2 echo something
16/96
Introduction Shell and Scripting Data Wrangling Conclusion
Script (2/2)
1 #!/bin/sh
2 echo something
Magic?
4
You can use other interpreters too, e.g.
#!/usr/bin/env python for a python script.
17/96
Introduction Shell and Scripting Data Wrangling Conclusion
Flags (1/3)
18/96
Introduction Shell and Scripting Data Wrangling Conclusion
Flags (2/3)
19/96
Introduction Shell and Scripting Data Wrangling Conclusion
Flags (3/3)
5
In Unix, by convention files whose names begin with a period is
hidden
20/96
Introduction Shell and Scripting Data Wrangling Conclusion
Introduction
Data Wrangling
21/96
Introduction Shell and Scripting Data Wrangling Conclusion
Running a command
echo Hello
22/96
Introduction Shell and Scripting Data Wrangling Conclusion
Variables (1/3)
PS1='> '
echo location
name=Julius
echo $name
23/96
Introduction Shell and Scripting Data Wrangling Conclusion
Variables (2/3)
24/96
Introduction Shell and Scripting Data Wrangling Conclusion
Variables (3/3)
1 #!/bin/sh
2 echo $0
3 echo $1
4 echo $2
5 echo $#
25/96
Introduction Shell and Scripting Data Wrangling Conclusion
Loop (1/4)
26/96
Introduction Shell and Scripting Data Wrangling Conclusion
Loop (2/4)
27/96
Introduction Shell and Scripting Data Wrangling Conclusion
Loop (3/4)
28/96
Introduction Shell and Scripting Data Wrangling Conclusion
Loop (4/4)
Conditionals (1/2)
CONDITION is a command.
If its exit code is 0 (success), then BODY is run.
Optionally, you can also hook in an else or elif
30/96
Introduction Shell and Scripting Data Wrangling Conclusion
Conditionals (2/2)
Everything Together
1 #!/bin/sh
2 for f in $(ls)
3 do
4 if test -d $f
5 then
6 echo dir $f
7 fi
8 done
32/96
Introduction Shell and Scripting Data Wrangling Conclusion
Bug!
33/96
Introduction Shell and Scripting Data Wrangling Conclusion
Argument Splitting
34/96
Introduction Shell and Scripting Data Wrangling Conclusion
Globbing (1/2)
35/96
Introduction Shell and Scripting Data Wrangling Conclusion
Globbing (2/2)
for f in a*:
36/96
Introduction Shell and Scripting Data Wrangling Conclusion
Globbing (2/2)
36/96
Introduction Shell and Scripting Data Wrangling Conclusion
Globbing (2/2)
36/96
Introduction Shell and Scripting Data Wrangling Conclusion
Globbing (2/2)
36/96
Introduction Shell and Scripting Data Wrangling Conclusion
37/96
Introduction Shell and Scripting Data Wrangling Conclusion
37/96
Introduction Shell and Scripting Data Wrangling Conclusion
37/96
Introduction Shell and Scripting Data Wrangling Conclusion
shellcheck
38/96
Introduction Shell and Scripting Data Wrangling Conclusion
Introduction
Data Wrangling
39/96
Introduction Shell and Scripting Data Wrangling Conclusion
Composability
40/96
Introduction Shell and Scripting Data Wrangling Conclusion
Pipe (1/2)
dmesg | tail
41/96
Introduction Shell and Scripting Data Wrangling Conclusion
Pipe (2/2)
42/96
Introduction Shell and Scripting Data Wrangling Conclusion
Streams
43/96
Introduction Shell and Scripting Data Wrangling Conclusion
44/96
Introduction Shell and Scripting Data Wrangling Conclusion
45/96
Introduction Shell and Scripting Data Wrangling Conclusion
45/96
Introduction Shell and Scripting Data Wrangling Conclusion
Grouping Commands
(a; b) | tac
7
tac print in reverse
46/96
Introduction Shell and Scripting Data Wrangling Conclusion
Process Substitution
b <(a)
47/96
Introduction Shell and Scripting Data Wrangling Conclusion
Introduction
Data Wrangling
48/96
Introduction Shell and Scripting Data Wrangling Conclusion
Job (1/2)
49/96
Introduction Shell and Scripting Data Wrangling Conclusion
Job (2/2)
8 Ctrl
is usually denoted as ^, thus Ctrl + z is denoted as ^Z
50/96
Introduction Shell and Scripting Data Wrangling Conclusion
9
Prefer SIGTERM over SIGKILL:
https://turnoff.us/geek/dont-sigkill/
52/96
Introduction Shell and Scripting Data Wrangling Conclusion
More Resources
10
http://mywiki.wooledge.org/BashGuide
11
http://linuxcommand.org/tlcl.php
53/96
Introduction Shell and Scripting Data Wrangling Conclusion
Introduction
Data Wrangling
54/96
Introduction Shell and Scripting Data Wrangling Conclusion
xargs
55/96
Introduction Shell and Scripting Data Wrangling Conclusion
Other Exercises
56/96
Introduction Shell and Scripting Data Wrangling Conclusion
Introduction
Data Wrangling
Introduction
sed and Regular Expression (regex)
More Advanced Data Wrangling
Exercises
Conclusion 57/96
Introduction Shell and Scripting Data Wrangling Conclusion
58/96
Introduction Shell and Scripting Data Wrangling Conclusion
Linux:
59/96
Introduction Shell and Scripting Data Wrangling Conclusion
60/96
Introduction Shell and Scripting Data Wrangling Conclusion
61/96
Introduction Shell and Scripting Data Wrangling Conclusion
We can do better!
cat log
| grep sshd
| grep "Accepted publickey for"
62/96
Introduction Shell and Scripting Data Wrangling Conclusion
Introduction
Data Wrangling
Introduction
sed and Regular Expression (regex)
More Advanced Data Wrangling
Exercises
Conclusion 63/96
Introduction Shell and Scripting Data Wrangling Conclusion
12
If you’re into lame computing jokes, here’s a joke about ed:
https://www.gnu.org/fun/jokes/ed-msg.html
64/96
Introduction Shell and Scripting Data Wrangling Conclusion
cat log
| grep sshd
| grep "Accepted publickey for"
| sed 's/.*Accepted publickey for //'
65/96
Introduction Shell and Scripting Data Wrangling Conclusion
Syntax: s/REGEX/SUBSTITUTION/
66/96
Introduction Shell and Scripting Data Wrangling Conclusion
Character Meaning
. Any single character except newline
* Zero or more of the preceding match
? One or more of the preceding match
[abc] Any one character of a, b, and c
(RX1|RX2) Either something that matches RX1 or RX2
^ The start of the line
$ The end of the line
If you are unfamiliar with regex, there is a nice tutorial at
https://regexone.com/
68/96
Introduction Shell and Scripting Data Wrangling Conclusion
69/96
Introduction Shell and Scripting Data Wrangling Conclusion
70/96
Introduction Shell and Scripting Data Wrangling Conclusion
13
https://regex101.com/r/wPc8Ii/3
71/96
Introduction Shell and Scripting Data Wrangling Conclusion
Explanation
Capture Groups
74/96
Introduction Shell and Scripting Data Wrangling Conclusion
So now we have
cat log
| grep sshd
| grep "Accepted publickey for"
| sed -E 's/.*Accepted publickey for (.*) from
,→ ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})
,→ port ([0-9]+) ssh2: RSA SHA256:.*/\1/'
77/96
Introduction Shell and Scripting Data Wrangling Conclusion
cat log
| sed -E -e '/Accepted publickey for/!d' -e
,→ 's/.*Accepted publickey for (.*) from
,→ ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})
,→ port ([0-9]+) ssh2: RSA SHA256:.*/\1/'
78/96
Introduction Shell and Scripting Data Wrangling Conclusion
Introduction
Data Wrangling
Introduction
sed and Regular Expression (regex)
More Advanced Data Wrangling
Exercises
Conclusion 79/96
Introduction Shell and Scripting Data Wrangling Conclusion
| sort | uniq -c
80/96
Introduction Shell and Scripting Data Wrangling Conclusion
23
In this particular example, sorting by the whole line wouldn’t
matter, but we’re here to learn!
81/96
Introduction Shell and Scripting Data Wrangling Conclusion
We can do better
awk
83/96
Introduction Shell and Scripting Data Wrangling Conclusion
awk Syntax
25
whitespace by default, can be changed with -F
84/96
Introduction Shell and Scripting Data Wrangling Conclusion
85/96
Introduction Shell and Scripting Data Wrangling Conclusion
BEGIN { rows = 0 }
$1 == 1 && $2 ~ /^r[^ ]*t$/ { rows += $1 }
END { print rows }
Advanced awk
88/96
Introduction Shell and Scripting Data Wrangling Conclusion
89/96
Introduction Shell and Scripting Data Wrangling Conclusion
What happened?
90/96
Introduction Shell and Scripting Data Wrangling Conclusion
ls
| grep -E 'asd.a [0-9]{2}'
| tr '\n' '\0'
| xargs -0 rm
91/96
Introduction Shell and Scripting Data Wrangling Conclusion
Introduction
Data Wrangling
Introduction
sed and Regular Expression (regex)
More Advanced Data Wrangling
Exercises
Conclusion 92/96
Introduction Shell and Scripting Data Wrangling Conclusion
Exercises (1/2)
93/96
Introduction Shell and Scripting Data Wrangling Conclusion
Exercises (2/2)
94/96
Introduction Shell and Scripting Data Wrangling Conclusion
Introduction
Data Wrangling
Conclusion
95/96
Introduction Shell and Scripting Data Wrangling Conclusion
Talk to us!
Feedback form:
https://is.gd/hs2019_hackertools_1
Upcoming hackerschool:
Hackertools Part Two
96/96