Thursday, June 18, 2020

Run Levels In Linux




Before understanding run levels lets understand the booting stages first. The following are the 6 high-level stages of a Linux boot process.

1 BIOS: basic input/output system, Perform some system integrity checks and executes the boot loader program.

2 MBR: Master boot record, loads and executes the Grub boot loader.

3 GRUB: Grand Unified Boot-loader, it loads the default kernel image as specified in the grub configuration file.

4 Kernel: Kernel executes the /sbin/init program.

5 Init: init executes run levels program.


6. Run-level programs: A run levels is a state or level of init process means A run-level can simply be thought of as the state your system enters like if a system is in a single-user mode it will have a run-level 1 while if the system is in a multi-user mode it will have a run level 5.



Linux relies on the run level to determine which services are started and which services are stopped. Run levels are identified by numbers. Run levels are numbered from 0 to 6, with each run level offering different services. Run levels 0, 1, and 6 are standard run levels reserved for special purposes. Run levels 2, 3, and 4 are used for whatever purpose you or your Linux distribution provider decide.



Init scripts


Init (initialization) is the program on Unix and Unix-like systems that spawns all other processes. It runs as a daemon and typically has PID 1.

The /etc/inittab file is used to set the default run level for the system. This is the run level that a system will start up on upon reboot. The applications that are started by init are located in the /etc/rc.d folder. Within this directory, there is a separate folder for each run level, eg rc0.d, rc1.d, and so on.


Example of  /etc/inittab file:  

Image via [https://geek-university.com/linux/runlevels/]

 

Depending on your default init level setting, the system will execute the programs from one of the following directories.

run levels 0 – /etc/rc.d/rc0.d/

run levels 1 – /etc/rc.d/rc1.d/

run levels 2 – /etc/rc.d/rc2.d/

run levels 3 – /etc/rc.d/rc3.d/

run levels 4 – /etc/rc.d/rc4.d/

run levels 5 – /etc/rc.d/rc5.d/

run levels 6 – /etc/rc.d/rc6.d/


Please note that there are also symbolic links available for these directory under /etc directly. So, /etc/rc0.d is linked to /etc/rc.d/rc0.d.


Under the /etc/rc.d/rc*.d/ directories, you would see programs that start with S and K.

Programs start with S are used during startup. S for startup.

Programs start with K are used during shutdown. K for kill.


When init starts, it looks for a line in /etc/inittab that specifies the default run level:

id : 2 : initdefault:


You can ask init to go to a non-default run level at startup by giving the kernel a command-line argument of single or emergency. Kernel command-line arguments can be given via LILO, for example. This allows you to choose the single user mode (run level 1).

While the system is running, the telinit command can change the run level. When the run level is changed, init runs the relevant command from /etc/inittab.


A system can be booted any of several run-levels, each of which is represented by a single-digit integer. Each run-levels designates a different system configuration and allows access to a different combination of processes.

There are differences in the run-levels according to the operating system. Seven run-levels are supported in the standard Linux Kernel. They are


ID

Name

Description

0

Halt

Shuts down the system.

1

Single-user mode

Mode for administrative tasks.[2][b]

2

Multi-user mode

It does not configure network interfaces and does not export network services.[c]

3

Multi-user mode with networking

Starts the system normally.

4

Not used/user-definable

For special purposes.

5

Start the system normally with appropriate display manager (with GUI)

Same as run-level 3 + display manager.

6

Reboot

Reboots the system.

Image via[https://en.wikipedia.org/wiki/Runlevel]


In addition to the standard run levels, users can modify the preset run levels or even create new ones if desired. Run levels 2 and 4 are usually used for user-defined run levels.

The program responsible for altering the run level is init, and it can be called using the telinit command. For example, changing from run level 3 to run level 5, which allows the GUI to be started, can be accomplished by the root (i.e., administrative) user by issuing the following command:    telinit 5


Booting into a different run level can help solve certain problems. For example, if a change made in the X Windows System configuration on a machine that has been set up to boot into a GUI has rendered the system unusable, it is possible to temporarily boot into a console (i.e., all-text mode) run level (i.e., run levels 3 or 1) to repair the error and then reboot into the GUI. The X Window System is a widely used system for managing GUIs on a single computer and on the network of computers.

Likewise, if a machine will not boot due to a damaged configuration file or will not allow logging in because of a corrupted /etc/passwd file (which stores user names and other data about users) or because of a forgotten password, the problem can be solved by first booting into single-user mode (i.e. run level 1).


The chkconfig tool is used in Red Hat-based systems (like CentOS) to control what services are started at which run levels. Running the command chkconfig - - list will display a list of services whether they are enabled or disabled for each run level.


In recent versions of Linux systems such as RHEL 7, the concept of run levels has been replaced with systemd targets.


Systemd 

Inittab is no longer used when using systemd


Systemd- system daemon is a new system that replaces the old “system V init” which gives all booting privileges to init generated process.systemd is developed and designed for better handling of dependencies and multitasking.

Systemd uses target instead of run levels, which supports our machine to keep track of all processes especially the daemon process(background running processes). The main purpose for develop this target based system is to perform parallel processing. All the new and popular Linux distribution such as Centos 7, RHEL 7.0, Fedora, and Arch Linux uses this


Runlevel

Systemd Description

0

poweroff.target

1

rescue.target

2

multi-user.target

3

multi-user.target

4

multi-user.target

5

graphical.target

6

reboot.target


By default, there are 2 main targets

  1. Multi-user.taget: Analogous to Runlevel 3
  1. Graphical.target: Analogous to run level 5
Inittab
via[https://landoflinux.com/linux_runlevels_systemd.html]


To view current default target run: systemctl -getdefault

And to change the default target:  rm /etc/systemd/system/default.target

Changing run level to 5:

ln -s /lib/systemd/system/runlevel5.target /etc/systemd/system/default.target


Then reboot system to  use runlevel5

Wednesday, June 3, 2020

SELinux In Linux Server CentOS 7



Security-Enhanced Linux
Security-Enhanced Linux 


 





Security-Enhanced Linux as its name suggests it is security enhancement in Red Hat distribution. As we know in Linux the main benefit is that we can modify the kernel according to our needs and make our own flavor.  So SELinux is a kernel module(Centos) with more security by more security I mean access control security.
Before the introduction of SELinux which works on Mandatory access control(MAC), Discretionary access control was used.
The DAC means the access control is based on the file owner and its permission. The root user has full access control with a DAC system.
If the system is a normal workstation then using the DAC may be a good choice but if it a Red Hat-based Linux server the use of DAC is not a good practice. Giving all access control to one user is not advisable. 
But after the MAC policy introduction by SELinux, there is an administratively prebuilt policy for access means if the DAC policy or its setting changes on the file, directory then SELinux policies are there to prevent the misuse of permission. Of course, SELinux gives flexibility to limit access between users for files, directory, and more. So, now comes the benefits of this hole thing we discussed, suppose you are running a Linux server(centos) with disable SELinux on it. if Your Linux server hosting many webpages and website, if any hacker can gain access through one of the vulnerability on these website vulnerabilities like 
1. Web ports HTTP(80) and https(443)
2. File transfer services FTP or proftpd
3. Through ssh service
4. Vulnerable plugin and theme installed on the website 
 After finding vulnerability first approach hackers is to put the shell inside the web server and gain the reverse shell to get the root access if we use old access policy like DAC then our hole server compromises and it makes easier for the hacker to do his job but with SELinux, there are some changes to protect our server. SELinux isolates the working of processes run by the local user to remove this type of chance of privilege escalation. The policies used by Mandatory access control are prebuilt we can’t change it but can make custom policy according to our needs.
 Configuration files of SELinux 
There are two methods to configure the config file SELinux 
   1. Using GUI: You can access GUI through   
                           system>Administrator>SELinux management
   2. Manually via config file
  You can directly access the config file using any text editor like vim   
 using the location /etc/selinux/config


command to edit config file
Command to edit config file 



After the modification in config file we need to reboot the system and after that login to your system and view the cat /var/log/messages if you do not see any error then all is fine and all MAC policy are implemented successfully but if you observer any error then the chances are policy is not implemented successfully.
When SELinux denies an action an Access vector is generated called AVC(access vector cache) which shows its message to /var/log/audit/audit.log and /var/log/messages, for troubleshooting use research to find any recent AVC messages.


config file of SELinux
Config file of SELinux


There are three basic mode for SELinux
Permissive: SELinux is enabled but the MAC policies that are not enforcing on the system only the warring to the user shows and a log of it displays.
Enforcing: The default mode, strict mode enabling all policies and restrictions on the system.
Disabled:  The default mode which is mainly present on the system, SELinux is off in this mode.
We can view the status of the current running mode of SELinux before editing the config file through “SEStatus” 


Status of  SELnix in linux server centos
SEStatus


Inside the config file, we also see SELINUXTYPE this option is used to set policy type which you want to use the default one is Targeted policy
Targeted:  default one which implements all the MAC policy(most restricted)
Minimum: Only selected processes are protected we have the liberty of modification of some  targeted made policy
Mis: server follows Multi-level security protection to prevent users from obtaining access to information for which they don’t authorize.
SELinux Policy Overview:
In the SELinux policy, there are 700 main topics with approx. 10 lakh permission defined already inside “policy.config file”. It is a very large size life so it is obvious that policy ensures the safety of all services which run inside the system from this. It seems the local administrator cannot make such a huge policy himself for system security to satisfy the security needs.


after enabling selinux sestatus
Status file after enabling SELinux

SELinux policies are developed in such a manner that all applications cover at least one policy or more. The policy is design in such method that all measure which used to enable the service and configure the service is at his highest level if the user or any domain user wants to run the service apart from his scope user must have to change the policy means we can think that lowest policy privileges are given to the local user. SELinux policy is not only to protect user-level thread but also kernel-level thread and its classes.
SELinux supports a variety of access control models among them main focus was on extended type enforcement.


Type_enforcement_model_in_Selinux
Type_enforcement_model_in_Selinux


 In the type enforcement model, set off operation represented by an object of any class with data types. Permission on each operation and its associated classes are different in fact each object of the different class have different permission on it then it is up to administrator how to manage permission of object of different classes.
The main difference between the old and new policy is set off rules that define the security and access right for process in the system. An SELinux predefined policy for users, group and other for accessing to roles, role access to their domain
SELinux introduced the predefined users, the users we create on the system are linked to one or more SELinux domain the basic roles of users are allowed to users plus one advantage is that process which is running by the local user are mapped to the predefined policy or we can say role allowed to the user. The definition of role in SELinux policy defines which users have access right to what role.
Sometimes, the pre-built policy makes hard for administrator and local users to use the system and resolve an issue regarding policy therefore the only remaining option is to choose permissive SELinux mode.





Saturday, May 23, 2020

Linux terminal subsystem



The terminal subsystem consists of three layers

  • The upper layer implements the character device interface ( open, read, write, close …)
  • The line discipline
  • The lower layer which communicates with the hardware or the pseudoterminal.

The line discipline glues the upper and the lower layer together.
The tty subsystem is passive: it only reacts on either a key being pressed or the user requesting something from it.

How terminal communicate with process(file descriptor file)
Terminal read and write operation
A Unix based system processes communicate with the terminal through terminal character device file, all the text we write in the terminal are store in the input buffer of this file and then the process may read from this to get the data. 

In this mechanism file descriptor 0 SDTIN used for read data from device file and file descriptor 1 STDOUT used for writing data to the file this type of terminal are called hardware-based terminal used in the past.
Now we emulate the terminal using software, a personal computer can run terminal emulator software that replicates the function of a terminal, sometimes allowing concurrent use of local programs and access to a distant terminal host system. The terminal emulators on most Unix-like systems such as gnome-terminal, qterminal, xterm, do emulate physical terminal including support for escaping sequences. 



how process communication with the master and slave terminal system
master and slave terminal
X windows system the program that controls the display, drawing the boxes and buttons.  Suppose we open a terminal emulator in X windows and start writing text in the terminal, the text we write goes from the X window server to terminal emulator which writes the text in the input buffer of "master" pseudo-terminal character device file. The operating system copies data from master to its associated "slave" pseudo-terminal character device file to be read by the process. Each terminal emulator has its own master and slave pseudo-terminal pair.
Usually, the master is connected to a terminal emulator (such as xterm) and the slave is connected to a program being run, most commonly a shell (such as bash). Thus, the slave behaves exactly like a classical terminal.
When the master side is opened, the corresponding slave device can be used in the same manner as any TTY device. The master and the slave device are connected by the Kernel.
Pseudo terminals are used, among others, by network login services (ssh, rlogin, telnet) and to implement terminal emulators (such as xterm, script, screen, tmux, unbuffer, expect). They are also be used to send data to su or passwd (they refuse to read from pipes).

There are two APIs: BSD style and Unix 98 (System V) style.
BSD style pseudo terminals are deprecated on Linux since kernel version 2.6.4 

Unix 98 style

Master pseudo terminals: /dev/ptmx
Slave pseudo terminals: /dev/pts

BSD style

Master: /dev/ptyXY
Slave: /dev/ttyXY

/dev/pts :

When a process opens /dev/ptmx, it gets a file descriptor for a pseudoterminal master (PTM), and a pseudoterminal slave (PTS) device is created in the /dev/pts directory.  Each file descriptor obtained by opening /dev/ptmx is an independent PTM with its own associated PTS, whose path can be found bypassing the file descriptor to its name.
Before opening the pseudoterminal slave, you must pass the master's file descriptor to grantpt() and unlockpt(). Once both the pseudoterminal master and slave are open, the slave provides processes with an interface that is identical to that of a real terminal. Data written to the slave is presented on the master file descriptor as input.  Data written to the master is presented to the slave as input.

/dev/tty : 

TTY is the abbreviation of Tele-Typewriter (or Tele-Type)
Originally, a TTY was physically connected to a Unix machine as an input/output device, usually on an RS-232 cable. 

The input was fed on a keyboard.In the beginning, the computer's output was going to a printer and later also to screens It is a special kind of file, it doesn't represent the character device. When a program opens /dev/tty file they get the file descriptor for the so-called controlling terminal that is terminal associated with their process. In simple words, it used for controlling the terminal file.
A TTY (or terminal) device is a special class of a character device.

A Terminal device might act as a controlling terminal for a session, such as
  • virtual consoles
  • serial ports
  • Pseudo-terminal (PTYs)
All registered TTY devices that are present in the kernel are found under sys/class/tty. The most important data structure to implement a TTY driver is the struct tty_driver.

/dev/ttyN: 

These are character device file for representing the virtual console and these virtual consoles has specific features where N represent the number. So let's say in any case if you open /dev/tty1, what you are doing is writing to the terminal of the first virtual console.

We can open six virtual terminals in Linux using CTRL+ALT+F(1 to 6) for each terminal we have one character device file like for the first terminal you have tty1.


Friday, May 22, 2020

Type of binding in IIS(Internet Information Services) in windows server



IIS web server binding types in windows 2012R2
IIS webserver 

   



Before understanding binding, you must be able to understand the concept of these terms.

IP Address:

An IP address is a number identifying a computer or another device on the Internet. IP addresses uniquely identify the source and destination of data transmitted with the Internet Protocol.

IP address is the unique identification address of your website similar to your house address in your city with the help of the IP address of your website is uniquely identifiable among other websites on the internet.

Port Number :

A port in computer networking is an access channel for communication between two devices. When a service (server program) initially is started, it is said to bind to its designated port number. As any client program wants to use that server, it also must request to bind to the designated port number.
Port represents the numbers and basically specific to the protocol. Port numbers are from 0 to 65535. Ports 0 to 1024 are reserved for use by certain privileged services whenever there are exchanges of data or communication present, there must be a port for this.

Hostname :

A hostname is a domain name that has at least one associated IP address. For example, the domain names www.example.com and example.com are two hostnames, whereas the com domain is not, In this Second-level domain is example and hostname is www.
In simpler terms the name for your website.
All hostnames have a corresponding IP Address that is mapped to it. These hostnames are resolved to their IP Address using DNS. There are four parameters that can be used in a site binding: IP Address, Port and Host Name, and type of website but with a combination of IP address and port, you may generate a fifth possibility for binding.


iis server panel in windows server 2012R2
IIS binding panel view


Website with IP address binding :

In this we specify the IP address for each of our website, Every website has a unique IP address. For example we bind IP address 192.168.43.51 with website name www.example.com whenever an HTTP request coming to 192.168.43.51 will be served by this website irrespective to the host header


website name(hostname) with ip address
website with IP address
                       


Website with hostname binding :

In this, we have the advantage of the host header we can change the host header according to our needs. For example, we are running a small business and we afford the only cost of one IP address then we host multiple hostnames using same IP address like www.example.com and example.com host on same IP address 192.168.43.51


website binding with hostname addresss
website with hostname


Website with unique port:

In this port number of every website must be different IP address or hostname may be the same. So whenever we search for a website we specify the port also like 192.168.43.50:80 or 192.168.43.50:8010

Sometimes this type of binding causes problems also like the browser default port for HTTP is 80 so www.example.com will work fine but for example.com we must specify the port as I mentioned above.


website binding with unique port
website with unique port binding



Website with Type binding:

Type binding helps us to differentiate between HTTP and https website IIS mainly have two types through which we can identify the website whether it is bind to HTTP or https. For https type SSL certificate is required and the default port for this 443 as shown below


website binding with unique type(http/https)
website with unique type(http/https) binding




Types of web Hosting

When you think about hosting your website it is important for you to consider some factor that directly affects your website hosting. Like:- Bandwidth, storage, dependability, scalability, secure site certification, malware and spam protection, etc.


how to choose which web hosting is good for you
Types of web hosting

With all the different types of web hosting available, it can be confusing to choose the right one but after gathering all the data regarding these above factors will help you to clear your view. There are lots of options currently present on the internet so let’s go one by one to each of them.



Shared hosting :

This is the first step towards hosting or we can say just the beginning of hosting a website if you are new to this area. In this type of hosting your website is hosted on a shared server as its name suggests the factors like storage, bandwidth, and many more are shared among all the clients of that shared server.

Because of this sharing sometimes a problem arises as if someone’s site has large traffic at a specific time then it will slow down your website to load and increases the response time also.
The hosting service provider provides its own system administration. If you belong to the non-technical field, needed less headache for managing your website, and having fewer visitors then shared hosting is one of the best options for you.


 Dedicated hosting :

This hosting type is totally opposite to the shared hosting, in shared hosting where we are sharing the server among all clients in this we are hosting our website in a separate server. All the resources on that server are dedicated to our website use.

In this we are responsible for our server security and its management, we have admin privileges means full access control over the server. Also, a high level of technical expertise is required for the installation and ongoing management of the server. Dedicated hosting is one of the expensive types of hosting services and it is only be chosen if your daily visitors are large enough and having large traffic on the website.

                            


VPS(virtual private server) :

This server is somewhere in the middle of a dedicated server and shared server in the functionality because we have our own server resources but the difference is that the whole server does not belong to us meaning the overall CPU time, memory, and many more resources are shared. Suppose hosting service provider has a server of size 10GB of memory whole but for you, it may reserve 4GB of memory. Resources are divided among the client virtually according to the requested arises by the client.
The resources and our own website are still in the hand of the server provider. There may be a concern of security if any of the servers misconfigure then maybe your website compromises therefore security is less as compared to a dedicated server but much more by the shared server.
This type of hosting gives less control over the server, the service provider is totally responsible for security, maintenance, and administration over our website.


Cloud-based hosting:
This is one of the latest type of hosting where we use the cloud service of any provider. In this, so many servers are presently making the clusters of servers. The resources required for maintaining your website are spread across more than one web server, reducing the chance of any downtime due to server malfunction.
Cloud-based hosting is scalable meaning if your site can grow over time, using as many resources as it requires and you only have to pay for what you need. This allows users to employ as many resources as they need without having to build and maintain their own computing infrastructure.
It’s a hosting solution that works via a network and enables companies to consume computing resources like a utility.


Self-service web hosting :

Self-service hosting means by its name itself, you have to rent a place for your server and storage. The complete web hosting is at your own responsibility. The hardware, software power, bandwidth, cooling, backups, administration are maintained by your self. It is the most expensive type of hosting because all the resources are own by us.
Then comes some of the hosting types which are not so popular but have their own importance.

Colocation web hosting :
When you opt for colocation hosting means you rent a space from a data center of any company. You provide your own server, hardware and they provide physical space, security, internet connectivity, cooling. this directly means we are owning our own server and its software so if any fault comes inside our server then it's our responsibility to replace it and take care of it. So its directly implements that we should only opt for these type of hosting types if we have some technical background or expertise. 


WordPress Hosting :

In this hosting you will get the hosting service inside the Wordpress server means Wordpress subdomain inside it. In clear words, WordPress hosting is a service where the web hosting provider will keep your WordPress installation up-to-date. It is not expensive as shared web hosting, this comes as a great option if you are a beginner to this and want no headache because Wordpress offer vary large number of plugin and themes for your website



Knowing Kali Linux for OSCP

Kali Linux is developed and maintained by the offensive security professional. It is a Debian-based Linux distribution focus at advanced Pen...