Wednesday, April 7, 2021
Basic RAID Levels
Tuesday, April 6, 2021
Setup NIC Bonding in Linux in Centos 6
# vi /etc/sysconfig/network-scripts/ifcfg-bond0--- START EDIT ---DEVICE=bond0IPADDR=10.10.10.11NETWORK=10.10.10.0NETMASK=255.255.255.0USRCTL=noBOOTPROTO=noneONBOOT=yes
--- END EDIT ---
# vi /etc/sysconfig/network-scripts/ifcfg-eth0--- START EDIT ---DEVICE=eth0MASTER=bond0SLAVE=yesHWADDR=00:0C:29:8D:FB:EFTYPE=EthernetUUID=5606b424-2186-410d-9dbc-dcb65b330bd9ONBOOT=yesNM_CONTROLLED=yesBOOTPROTO=none--- END EDIT ---
--- START EDIT ---DEVICE=eth1MASTER=bond0SLAVE=yesHWADDR=00:25:B5:0B:B1:00TYPE=EthernetUUID=6353d563-0a85-47d2-b566-37f6c91c8973ONBOOT=yesNM_CONTROLLED=yesBOOTPROTO=none--- END EDIT ---
# vi /etc/modprobe.d/modprobe.conf--- START EDIT ---alias bond0 bondingoptions bond0 mode=balance-alb miimon=100--- END EDIT ---
# service network restart
Monday, April 5, 2021
RPM and Debian Package Managers
yum list # lists ALL installed packages and shows you if there is a newer version for each of those
yum list available <package name> # lists all available packages for update
yum --disablerepo=* --eablerepo=<repo ID> list <pacakge name> # list package on a particular repo
apt-get install <package name> -s # doesn't install, simulates a dry-run
Sunday, April 4, 2021
Setup NGINX Proxy
(ports above 1024). What can you do to protect its identity from attacks? Aha!
Use HTTPS.. But how? We can setup another host in front of it (proxy server) to
accept incoming requests via encrypted channel (HTTPS, port 443/tcp) and
redirect that to the backend server (or proxied host) via an uncrypted channel.
In this post, we will use Nginx to have that setup.
1. First, let's say we have VM01 that runs an application on port 8081.
2. Now, let's spin up another host, VM02 (Centos 7.3 w/ SELinux in enforcing
mode), and install Nginx. First, be sure that the nginx repo is enabled.
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/x86_64/
gpgcheck=0
enabled=1
[root@vm02 ~]#
3. Install nginx package.
4. Start and enable nginx at boot
[root@vm02 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@vm02 ~]#
5. Create selfsigned certificates. In this part, you may use `genkey` or
`openssl`. I always wanted the openssl way because its faster. BTW, don't
memorize the command below. Just be familiar with it because you can always see
it inside `/etc/pki/tls/certs/make-dummy-cert`
Generating a 2048 bit RSA private key
.........+++
..........................................................................................+++
writing new private key to 'vm02.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:US
State or Province Name (full name) []:California
Locality Name (eg, city) [Default City]:Los Angeles
Organization Name (eg, company) [Default Company Ltd]:dummy
Organizational Unit Name (eg, section) []:dummy
Common Name (eg, your name or your server's hostname) []:vm02
Email Address []:dummy@nxdomain.com
[root@vm02 ~]#
6. Move the certificates to the correct paths and run `restorecon` to make sure
SELinux contexts are correct.
[root@vm02 ~]# mv *key /etc/pki/tls/private/ [root@vm02 ~]# restorecon -Rv /etc/pki/tls/
restorecon reset /etc/pki/tls/certs/vm02.crt context unconfined_u:object_r:admin_home_t:s0->unconfined_u:object_r:cert_t:s0
restorecon reset /etc/pki/tls/private/vm02.key context unconfined_u:object_r:admin_home_t:s0->unconfined_u:object_r:cert_t:s0
[root@vm02 ~]#
7. Update nginx configuration to use SSL, point to the correct certificates
(ssl_certificate_*), and activate reverse proxy (proxy_pass). We will just use
the default config and use the minimal directives needed for simplicity.
server {
listen 443;
server_name vm02;
ssl on;
ssl_certificate /etc/pki/tls/certs/vm02.crt;
ssl_certificate_key /etc/pki/tls/private/vm02.key;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location / {
proxy_pass http://vm01:8081;
}
}
[root@vm02 ~]#
8. Open up port 443/tcp on the firewall to allow incoming connections. You may
use firewall-cmd's "--add-service" or "--add-port". Let's use "--add-service"
since there is already an existing service defined for HTTPS.
success
[root@vm02 ~]# firewall-cmd --reload
success
[root@vm02 ~]#
9. Activate this SELinux boolean to allow HTTP to forward requests to our
upstream server (VM01).
[root@vm02 ~]#
10. Now, our proxy server is ready. Let's try connecting. It must display the
data from the upstream like in #1.
So that are the basics steps in hiding your application via a proxy server.
This is very important if your application accepts user details like username
and passwords. You never want your credentials to be sent in cleartext!
Hope you learned something from this post :)
Saturday, April 3, 2021
Building an RPM package
In this post, I will teach you how to create an RPM package from a source code. This tutorial assumes that
you already have the tarballed source code ready to be unpacked.
As an example in our previous post about mrepo, we have installed it from source and not by using RPM
so that is a perfect way to demomstrate the RPM creation.
1. Install the needed packages
yum install -y rpm-build rpmdevtools
# rpm-build is required which contains "rpmbuild" command
# rpmdevtools is optional which is helpful in creating the directory tree
2. Create the directory tree
rpmdev-setuptree
# that command will create /root/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
3. Copy the tarballed source file to SOURCES
cp mrepo-0.8.7.tar.bz2 /root/rpmbuild/SOURCES/
4. Copy the spec file to SPECS. It is usually included inside the tarball so unpack the tarball to /tmp first then
get the spec file from there.
tar xvf mrepo-0.8.7.tar.bz2 -C /tmp
cp /tmp/mrepo-0.8.7/mrepo.spec /root/rpmbuild/SPECS
INFO: A spec file describes the software and contains instructions on how to install the software. Creation
and detailed discussion of a spec file is not covered in this post but I might create one soon.
5. Now create the RPMs (src and binary)
rpmbuild -ba /root/rpmbuild/SPECS/mrepo.spec
# that command will create the following files
# /root/rpmbuilds/RPMS/noarch/mrepo-0.8.7-1.noarch.rpm -> the actual rpm you can install
# /root/rombuild/SRPMS/mrepo-0.8.7-1.src.rpm -> contains the original source code and the spec file
6. Validate by installing the rpm
rpm -ivh /root/rpmbuild/noarch/mrepo-0.8.7-1.noarch.rpm
SOURCES
Other tutorials:
http://www.tldp.org/HOWTO/RPM-HOWTO/build.html
http://www.thegeekstuff.com/2015/02/rpm-build-package-example/
Friday, April 2, 2021
Understanding Linux load average
We often hear the term “load average” but how much do we know about it? In this post, I will try my best to explain in the fewest letters everything we need to know about load average.
First, how do we determine the load average? We can use commands like uptime and top. Both will present you 3 values.
bash-3.2$ uptime
03:46:39 up 703 days, 11:00, 2 users, load average: 3.04, 3.10, 3.08
bash-3.2$
From the output above, 3.04, 3.10, and 3.08 is the load average for the past 1, 5, and 15 minutes respectively. You may also use “cat procloadavg” and will get same result for the 1st 3 columns.
Now, what is LOAD AVERAGE? It is the average of all cpu loads on your system.
What is a CPU LOAD? It is the NUMBER of processes using + NUMBER of processes in queue for a single cpu core. It is NOT the CPU usage.
What is CPU USAGE? It tells us how active is your cpu cores.
To determine CPU LOAD on each cpu core, we follow these 2 formulas:
load per cpu core = (load average) / (# of cpu cores)
load per cpu core = (processes using the cpu core) + (processes inqueue for that cpu core)
where:
load average = the one reported in procloadavg, uptime, top, etc ..
# of cores = grep -c ^proc proccpuinfo
To better illustrate, look at the scenarios below for a 1-core and 2-core machine.
1-core machine (CPU A only):
0 process using CPU A = load average of 0 (under capacity)
1 process using CPU A = load average of 1 (at max capacity)
1 process using CPU A, 1 process waiting in line = load average of 2 (overcapacity)
1 process using CPU A, 2 process waiting in line = load average of 3 (overcapacity)
… and so on ..
2-core machine (CPU A and CPU B):
0 process using CPU A, 0 process using CPU B = load average of 0 (under capacity)
1 process using CPU A, 0 process using CPU B = load average of 0.5 (under capacity)
1 process using CPU A, 1 process using CPU B = load average of 1 (max capacity)
1 process using CPU A, 1 process using CPU B, 1 processes waiting in line = load average of 1.5 (slightly overcapacity)
1 process using CPU A, 1 process using CPU B, 2 processes waiting in line = load average of 2 (overcapacity)
… and so on ..
Based from the examples above, a load average of 1 means all of your cpu cores are at max capacity. Load average of 2 means each cpu core has 1 process running and 1 process waiting in line.
There are instances where your load average is high but the CPU usage is low. An example is when you have several hunged processes occupying all your cpu cores. Sinced hunged, those processes doesn’t generate CPU activity but they still hold the CPU cores. When all CPU cores are being held, no other processes can use them. So if processes can’t get hold of the CPU, this can slow down your system.
In short, a system with LOW CPU USAGE but with HIGH LOAD AVERAGE can still slow down your system.
Thursday, April 1, 2021
SSL (Secure Sockets Layer)
What is SSL?
SSL stands for Secure Sockets Layer, a protocol developed by Netscape in 1994.
It provides a secure way of communication between computer systems by scrambling
the data to make it difficult to read while traversing the network.
Symmetric vs Asymmetric Key Cryptography
Before we learn how SSL process work, we must understand the 2 major
cryptographies used.
Symmetric Key Cryptography
This is also known as Secret Key Cryptography. Both communicating parties usessame key in decrypting and encrypting data. The cryptographic algorithm to use
in encrypting and decrypting data must be agreed by both ends. Example of
these are Data Encryption Standard (DES), Triple-Strength DES (3DES), Rivest
Cipher 2 (RC2), and Rivest Cipher 4 (RC4). Decryption/encryption of data is
quick but transferring the secret/symmetric key to both ends can be
intercepted by an attacker.
Asymmetric Key Cryptography
This is also known as Public Key Cryptography. This make use of a private and
public key to encrypt/decrypt data. Private key must never be shared to others
while public key can be shared. If a data is encrypted using the private key,
the data can be decrypted using its corresponding public key (and vice-versa).
Some well known public key algorithms are Rivest Shamir Adleman (RSA) and
Diffie-Hellman (DH) algorithm. Using this kind of cryptography requires more
processing power which makes it slow. This is the reason why we only use this
in ecnrypting small pieces of data like the symmetric key.
In the next sections, we will see how these 2 takes place in SSL handshake.
But first, here are some few items that are worth reading.
| Terminology | Definition |
|---------------|---------------------------------------------------------------|
| cipher suite | A set if cryptographic algorithms and key sizes used that a |
| | computer can use to encrypt data. A cipher suite typically |
| | consists of respective algorithms used for key exchange, |
| | authentication, bulk encyrption, and Message Authentication |
| | Code (MAC). For in depth discussion on cipher suites, I will |
| | provide another post right after this. |
|---------------|---------------------------------------------------------------|
| cryptographic | These are math functions that aims to scramble data to hide |
| algorithm | its contents. 2 major types are Symmetric (uses 1 key) and |
| | Asymmetric (uses public and private keys) and different kinds |
| | exists under those categories. E.g Symmetric has RC and DES |
| | while Asymmetric can be RSA or DH. |
|---------------|---------------------------------------------------------------|
| ciphertext | This is another name for encrypted data. The opposite is |
| | the unencrypted data or cleartext. |
|---------------|---------------------------------------------------------------|
The fun part: SSL Protocol in depth
Here is a more detailed explanation on what is happening in the background.
| CLIENT | | SERVER |
|-------------------------------|-----|--------------------------------|
| Client Hello | --> | |
|-------------------------------|-----|--------------------------------|
| | <-- | Server Hello |
|-------------------------------|-----|--------------------------------|
| | <-- | Certificate |
|-------------------------------|-----|--------------------------------|
| | <-- | Server Key Exchange (optional) |
|-------------------------------|-----|--------------------------------|
| | <-- | Server Hello Done |
|-------------------------------|-----|--------------------------------|
| Client Certificate (optional) | --> | |
|-------------------------------|-----|--------------------------------|
| Client Key Exchange | --> | |
|-------------------------------|-----|--------------------------------|
| Change Cipher Spec | --> | |
|-------------------------------|-----|--------------------------------|
| Finished | --> | |
|-------------------------------|-----|--------------------------------|
| | <-- | Change Cipher Spec |
|-------------------------------|-----|--------------------------------|
| | <-- | Finished |
|-------------------------------|-----|--------------------------------|
| encrypted data | <-> | encrypted data |
|-------------------------------|-----|--------------------------------|
Let's focus on the required steps below leaving off the optional ones with some
few details.
Client Hello
Client (in this case a browser like google chrome) initiates the connection by
going to a secure site (URL starting in https://). At this moment, SSL is
triggered automatically. Client sends the server (system on where the site is
hosted) 4 important information - cipher suite it can use for both symmetric
and asymmetric key encryptions, the SSL version it wish to use, session ID,
and compression method.
Protocol Version - Version of SSL the client wants to use
Session ID - Session identifier the client wants to use. The 1st
client hello is always empty for every new sessions.
Cipher Suite - Contains list of cryptographic algorithms supported by
the client (in order of preference). The server selects
from these choices. If nothing is selected, server will
return a handshake failure.
Compression Method - List of compression algorithms supported by the client.
If server doesn't support any method listed, connection
will fail.
Here is an actual packet capture of Client Hello message from wireshark.










