Cheatsheet: Difference between revisions

 
(26 intermediate revisions by the same user not shown)
Line 312:
 
= VPN Messages =
<div style="column-count:3;-moz-column-count:3;-webkit-column-count:3">
 
*;Phase 1 - Main Mode
Line 326 ⟶ 325:
ID,Accepted Proposal,DH Key,Nonce,ID Hash
ID Hash
 
*;Phase 2 - Quick Mode
Ph1 Hash,Message ID,Proposal List,Nonce, DH Key,Proxy-ID
Ph1 Hash,Message ID,Accepted Proposal,Nonce,DH Key,Proxy-ID
Ph1 Hash,Message ID,Nonce
</div>
 
 
= HTTP =
Line 380 ⟶ 369:
* Supports OPTIONS, PUT, DELETE, TRACE, CONNECT request methods
<br />
 
;HTTP/1.1 vs HTTP/2
 
* HTTP/2 Supports Page load speed improvements through:
'''Compression of request headers'''
'''Binary protocol'''
'''HTTP/2 Server Push''': capability allows the server to send additional cacheable information to the client that isn’t requested but is anticipated in future requests.
'''Request multiplexing over a single TCP connection'''
'''Request pipelining'''
'''HOL blocking (Head-of-line) — Package blocking'''
 
;HTTP Request Methods
Line 408 ⟶ 407:
Other uses
*Zombie cookie
 
== HTTP Headers ==
{|class="wikitable"
|-
! Header !! Uses
|-
|Set-cookie ||
|-
|Location ||1) Used to ask a web browser to load a different web page<br/>Client request:<br/>''GET /index.html HTTP/1.1''<br/>''Host: www.example.com''<br/>Server response:<br/>''HTTP/1.1 302 Found''<br/>''Location: http://www.example.org/index.php.''<br/>
2) To provide info about location of a newly created resource, the Location header should be sent with an HTTP status code of 201 or 202.
|-
|Host||The host Header tells the webserver which virtual host to use if same virtual host is using several aliases
|-
|Accept||
|-
|User-Agent||
|-
|content-type||
|-
|content-length||
|-
|date||
|-
|expires||
|-
|Authorization||Basic access authentication is used to provide a user name and password when making a request.<br/>Credentials are the base64 encoding of id and password joined by a single colon<br/>Base64-encoding of 'Aladdin:OpenSesame' is 'QWxhZGRpbjpPcGVuU2VzYW1l'<br/>''Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l''<br/>''https://Aladdin:OpenSesame@www.example.com/index.html'' ==> This will not ask for the credentials; deprecated now
|-
|Referrer||When a user clicks a hyperlink in a web browser, the browser sends a request to the server holding the destination webpage.<br/>
The request may include the referer field, which indicates the last page the user was on (the one where they clicked the link).
|-
|X-Forwarded-For (XFF)||Used for identifying the originating IP address of a client connecting to a Web Server through an HTTP Proxy or Load Balancer.
|}
 
= FTP =
Line 429 ⟶ 460:
#Client sends the second half of the Diffie-Hellman exchange, Computes the session keys; Switches to encrypted communication
#Server computes the session keys; Switches to encrypted communication.
<br>
 
; SSLv1 vs TLS 1.0 vs TLS1.3
SSL 2.0 - Deprecated
SSL 3.0 - Deprecated
TLS 1.0 - Deprecated
TLS 1.1 - Deprecated
TLS 1.2 -
TLS 1.3 -
 
= NetScaler =
Line 498 ⟶ 538:
[[File:OSFF LSA 2.png|center]]
 
* OSPF path selection: O > O*IA > O*E1 > O*E2 > N1 > N2.
* “area range” summarize type 3 LSA’.
* “summary-address” summarize type 5 & 7 LSA’s.
Line 505 ⟶ 545:
= BGP =
 
* Route Selection Criteria
<div style="column-count:2;-moz-column-count:2;-webkit-column-count:2">
 
*Route Selection Criteria
<center>
{| class="wikitable"
|-
! Attribute !! Which is better !! DirectionType
|-
|Next Hop reachable || Route cannot be used if next hop is unreachable || Well-known Mandatory
|-
|Weight || Bigger; value local to the router; Cisco proprietary; default is 0 for all routes not originated by local router ||
|-
|Local Preference || Bigger; used within AS and exchanged bw iBGP routers; default is 100 || Well-known discretionary
|-
|Locally Injected (Originate) || Prefer path local router originated; Locally injected > iBGP/eBGP learned; In BGP table it will hv next hop 0.0.0.0 ||
|Next Hop reachable || Route cannot be used if next hop is unreachable ||
|-
|AS Path Length || Smaller; e.g: AS path 1 2 3 is preferred over AS path 1 2 3 4 5 || Well-known Mandatory
|Weight || Bigger ||
|-
|Origin || Prefer IGP(advertised by network cmd - i) > EGP > INCOMPLETE - '?'(reditributed) || Well-known Mandatory
|Local Preference || Bigger ||
|-
|MED(Metric) || Smaller; used to advertise to neighbors how they should enter your AS; propagated to all routers within the neighbor AS but not passed along any other AS || Optional non-transitive
|Locally Injected || Locally injected is better than iBGP/eBGP learned ||
|-
|Neighbor Type || Prefer eBGP over iBGP ||
|AS Path Length || Smaller ||
|-
|IGP Metric to Next Hop || Smaller; Prefer the path within the AS with the lowest IGP metric to the BGP next hop ||
|Origin || Prefer I over E & E over Unknown ||
|-
|Oldest path || Prefer the path that we received first ||
|MED || Smaller ||
|-
|Router ID || Prefer the path with the lowest BGP neighbor router ID (Manually conf > Highest Loopback IP address > Highest Interface IP address) ||
|Neighbor Type || Prefer eBGP over iBGP ||
|-
|Neighbor IP address || Prefer the path with the lowest neighbor IP address ||
|IGP Metric to Next Hop || Smaller ||
|}
<br /center>
<br />
 
Line 546 ⟶ 593:
Notification Always indicate something is wrong
<br />
</div>
 
* Directions
Line 726 ⟶ 772:
#ARP Reply will reach PC1, it will add entry to its ARP Table
#Then send a packet destined to PC2 with destintion MAC address as Router's Interface's MAC address received in ARP reply.
 
 
= Linux =
 
== Linux Booting ==
 
#BIOS(Basic Input/Output System) - POST, Loads and executes the MBR boot loader.
#MBR (Master Boot Record) - Loads and executes the GRUB boot loader.
#GRUB (Grand Unified Bootloader) - Loads and executes Kernel and Initrd images.
#Kernel - Heart of OS; Memory, Process mgmt; Executes INIT process.
#Init (initialization) - Decides the Linux run level; default run level to either 3 or 5.
#Runlevel programs - Executes programs like sendmail, etc from the run level directory as defined by the run level.
 
== Manually Boot using Grub ==
 
*Locate where the vmlinuz and initrd.* files are located:
grub> ls
(hd0) (hd0,msdos5) (hd1) (hd1,msdos0)
 
*Boot the system:
grub> linux (hd1,msdos1)/install/vmlinuz root=/dev/sdb1
grub> initrd (hd1,msdos1)/install/initrd.gz
grub> boot
 
== File system layout ==
 
/ – The Root Directory
/bin – Essential command binaries
/boot – Boot loader files
/dev – Device Files
/etc – Configuration Files
/home – Home Directory
/lib – Essential Libraries
/lost+found – Recovering Files
/media – Removable Media Devices
/mnt – Temporarily mounted filesystems
/opt – Optional software packages
/proc – Kernel & Process Information
/root – Root Home Directory
/sbin – System binaries
/selinux – Security-Enhanced Linux
/srv – Service Data
/sys – virtual filesystem
/tmp – Temporary files
/usr – binaries, documentation, source code, libraries
/var – Variable Files
 
 
=== CURL ===
curl -I http://domain.com Get HTTP header information
curl -i http://domain.com Get HTTP header + Body information
curl -L http://domain.com Handle URL redirects
curl -v http://domain.com Debug level details
curl -x proxy.sr.com:3128 http://domain.com Using proxy to download a file
curl -k https://domain.com Ignoring the ssl certificate warning
curl -A "Mozilla/5.0" http://domain.com Spoofing user agent:
curl -L -H "user-agent: Mozilla/5.0" https://aman.info.tm Custom Headers
curl smtp://example.com:2525
curl ftp://example.com
curl example.com:21
curl example.com:7822 Troubleshooting SSH: SSH-2.0-OpenSSH_5.3
time curl google.com
curl -i https://site1.lab.com --cert /root/ca/domains/ubnsrv01-cert.pem --key /root/ca/domains/ubnsrv01-key.pem
curl -v -X OPTIONS https://site3.lab.com
curl -v -X TRACE https://site3.lab.com
curl --sslv2 https://yoururl.com
curl --tlsv1 https://yoururl.com
curl -H 'X-My-Custom-Header: 123' https://httpbin.org/get Using httpbin tool; shows header info
curl -e google.com yoururl.com Referrer
curl --data "name=bool&last=word" https://httpbin.org/post Post data
curl -X POST https://httpbin.org/post Empty Post Request
curl -H 'Host: aman.info.tm' 128.199.139.216 If Server using Virtual Hosting
 
 
Post Json Data
curl --data '{"email":"test@example.com", "name": ["Boolean", "World"]}' -H 'Content-Type: application/json' https://httpbin.org/post
 
Time Breakdown
curl https://www.booleanworld.com/ -sSo /dev/null -w 'namelookup:\t%{time_namelookup}\nconnect:\t%{time_connect}\nappconnect:\t%{time_appconnect}\npretransfer:\t%{time_pretransfer}\nredirect:\t%{time_redirect}\nstarttransfer:\t%{time_starttransfer}\ntotal:\t\t%{time_total}\n'
 
=== IPtables ===
 
iptables -L ==> List rules
iptables -F ==> Stop iptables
iptables -nvL ==> Check Stats
iptables --flush MYCHAIN ==> Flush Chain
iptables -X MYCHAIN ==> Delete Empty Chain
iptables -A INPUT -p tcp --dport ssh -j ACCEPT ==> Allow SSH
iptables -A INPUT -p tcp --dport 80 -j ACCEPT ==> Allow incoming web traffic
iptables -A INPUT -j DROP ==> Blocking Traffic
iptables -A INPUT -i ens160 -s 10.140.198.7 -j DROP ==> Blocking Traffic
iptables -I INPUT 1 -i lo -j ACCEPT ==> Allow loopback
iptables -I INPUT 5 -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7 ==> Logging
 
 
=== TCPDump ===
 
sudo tcpdump -s 0 -i ens160 host 10.1.1.1 -v -w /tmp/packet_capture.cap
sudo tcpdump -s 0 -i ens160 host 10.1.1.1 and port 22 -v -w /tmp/packet_capture.cap
sudo tcpdump -s 0 -i ens160 host 10.1.1.1 and port not 22 and port not 80 -v -w /tmp/packet_capture.cap
sudo tcpdump -s 0 -i ens160 host 10.1.1.1 and tcp port not 22 and tcp port not 80 -v -w /tmp/packet_capture.cap
 
for i in `find . -type f | egrep "All.pcap"`; do echo $i; tcpdump -r $i '((host 1.1.1.1 or host 2.2.2.2) and host 3.3.3.3) and port 445' ; echo -e "\n"; done
 
 
=== MTR ===
 
Provides the functionality of both the ping and traceroute commands.
Prints information about the entire route.
 
mtr google.com
mtr -g google.com Display Numeric IP addresses
mtr -b google.com Both hostnames and numeric IP addresses
mtr --tcp google.com Use TCP SYN packets
mtr --udp google.com UDP datagrams
 
=== Traceroute ===
 
traceroute 4.2.2.2 ==> Uses UDP
traceroute -n 4.2.2.2 ==> Do not resolve hostnames
sudo traceroute -nI 4.2.2.2 ==> Use ICMP Packets
sudo traceroute -nT 4.2.2.2 ==> Use TCP Syn (Port 80)
 
=== Netstat ===
 
netstat -s
netstat -a Listing all ports (both TCP and UDP)
netstat -at Listing TCP Ports connections
netstat -au Listing UDP Ports connections
netstat -l Listing all LISTENING Connections
netstat -lt Listing all TCP Listening Ports
netstat -s Showing Statistics by Protocol
netstat -st Showing Statistics by TCP Protocol
netstat -tp Displaying Service name with PID
netstat -r Displaying Kernel IP routing
netstat -anp
netstat -ant
 
=== PS ===
ps -aux Display all processes in BSD format
ps -eo pid,ppid,user,cmd
ps -e --forest Print Process Tree
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head
 
=== LS ===
 
Append a character to each file name indicating the file type:
ls -F or ls --classify
 
* Executable files
/ Directories
@ Symbolic links
<nowiki>|</nowiki> FIFOs
= Sockets
> Doors
Nothing for Regular Files
 
List Symoblic Links:
 
ls -la
<pre>
lrwxrwxrwx 1 root root 11 Sep 13 14:57 mounts -> self/mounts
dr-xr-xr-x 3 root root 0 Sep 13 14:57 mpt
-rw-r--r-- 1 root root 0 Sep 13 14:57 mtrr
</pre>
 
=== Redirect Stderr ===
 
0 stdin – Use to get input (keyboard)
1 stdout – Use to write information (screen) 1> >
2 stderr – Use to write error message (screen) 2>
 
Redirect Stderr into Stdout:
2>&1
ls > file.log 2>&1 OR ls &> file.log
ls > file.log 2> /dev/null
 
=== System Calls ===
{{UC}}
 
= Sorting Algorithms =
 
 
'''* Quicksort''' is a good default choice.
It is a good default choice.
It tends to be fast in practice
It tends to be fast in practice with some small tweaks its dreaded O(n2)O(n^2)O(n2) worst-case time complexity becomes very unlikely.
A tried and true favorite.
 
'''Heapsort''' is a good choice if you can't tolerate a worst-case time complexity of O(n2)O(n^2)O(n2) or need low space costs.
* Heapsort
The Linux kernel uses heapsort instead of quicksort for both of those reasons.
'''Merge sort'''It is a good choice if you wantcan't tolerate a stableworst-case sortingtime algorithmcomplexity of O(n2)O(n^2)O(n2) or need low space costs.
The Linux kernel uses heapsort instead of quicksort for both of those reasons.
can easily be extended to handle data sets that can't fit in RAM
 
where the bottleneck cost is reading and writing the input on disk, not comparing and swapping individual items.
* Merge sort
'''Radix sort''' looks fast, with its O(n)O(n)O(n) worst-case time complexity.
It is a good choice if you want a stable sorting algorithm.
if you're using it to sort binary numbers, then there's a hidden constant factor that's usually 32 or 64 (depending on how many bits your numbers are).
It can easily be extended to handle data sets that can't fit in RAM where the bottleneck cost is reading and writing the input on disk, not comparing and swapping individual items.
That's often way bigger than O(lg⁡(n))O(\lg(n))O(lg(n)), meaning radix sort tends to be slow in practice.
 
'''Counting sort''' is a good choice in scenarios where there are small number of distinct values to be sorted.
* Radix sort
This is pretty rare in practice, and counting sort doesn't get much use.
It looks fast, with its O(n)O(n)O(n) worst-case time complexity.
If you're using it to sort binary numbers, then there's a hidden constant factor that's usually 32 or 64 (depending on how many bits your numbers are).
That's often way bigger than O(lg⁡(n))O(\lg(n))O(lg(n)), meaning radix sort tends to be slow in practice.
* Counting sort
It is a good choice in scenarios where there are small number of distinct values to be sorted.
This is pretty rare in practice, and counting sort doesn't get much use.
 
* Which sorting algorithm has best asymptotic run time complexity?
 
= Python =
 
* Regex
re.match() => Matches Beginning
re.search() => Matches Anywhere
re.findall() => All Matching Objects
re.sub('[ES]', 'a', s) => Substitute
 
* Lists
 
* Dictionary
 
* File operations
Using Open:
f = open('/etc/passwd')
f.read(5)
f.close()
 
Using With Open(better, auto closes the file):
with open('/etc/passwd') as f:
for line in f:
print(line)
 
* Class
 
* OS Interaction:
 
import os
os.system("date")
 
import os
f = os.popen('date')
now = f.read()
print("Today is ", now)
 
import subprocess
subprocess.call(["ls", "-l", "/etc/resolv.conf"])
 
import subprocess
p = subprocess.Popen("date", stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
print("Today is", output)
 
= SMTP =
 
HELO or EHLO (Hello)
MAIL FROM
250 OK reply code
RCPT TO (Recipient To)
250 OK reply code
DATA
345 reply code
250 OK code
QUIT
221 code
 
RSET (Reset)
 
SMTP errors:
4.X.X Persistent Transient Failure
5.X.X Permanent Error: