{"id":1040,"date":"2020-01-20T21:19:27","date_gmt":"2020-01-20T21:19:27","guid":{"rendered":"http:\/\/www.balajibandi.com\/?p=1040"},"modified":"2020-10-31T17:53:45","modified_gmt":"2020-10-31T17:53:45","slug":"firewall-with-ufw-on-an-ubuntu-20-x","status":"publish","type":"post","link":"https:\/\/www.balajibandi.com\/?p=1040","title":{"rendered":"Firewall with UFW on an Ubuntu 20.X"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p>One of the first lines of defense in securing your cloud server is a functioning firewall. In the past, this was often done through complicated and arcane utilities. There is a lot of functionality built into these utilities, iptables being the most popular nowadays, but they require a decent effort on behalf of the user to learn and understand them. Firewall rules are not something you want yourself second-guessing.<\/p>\n\n\n\n<p>To this end, UFW is a considerably easier-to-use alternative.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is UFW?<\/h2>\n\n\n\n<p>UFW, or Uncomplicated Firewall, is a front-end to iptables. Its main goal is to make managing your firewall drop-dead simple and to provide an easy-to-use interface. It\u2019s well-supported and popular in the Linux community\u2014even installed by default in a lot of distros. As such, it\u2019s a great way to get started securing your server.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Before We Get Started<\/h2>\n\n\n\n<p>First, obviously, you want to make sure UFW is installed. It should be installed by default in Ubuntu, but if for some reason it\u2019s not, you can install the package using aptitude or apt-get using the following commands:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo aptitude install ufw<\/pre>\n\n\n\n<p>or<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt-get install ufw<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Check the Status<\/h2>\n\n\n\n<p>You can check the status of UFW by typing:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw status<\/pre>\n\n\n\n<p>Right now, it will probably tell you it is inactive. Whenever ufw is active, you\u2019ll get a listing of the current rules that looks similar to this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"499\" height=\"83\" src=\"http:\/\/www.balajibandi.com\/wp-content\/uploads\/2020\/06\/image-12.png\" alt=\"\" class=\"wp-image-1041\" srcset=\"https:\/\/www.balajibandi.com\/wp-content\/uploads\/2020\/06\/image-12.png 499w, https:\/\/www.balajibandi.com\/wp-content\/uploads\/2020\/06\/image-12-300x50.png 300w\" sizes=\"auto, (max-width: 499px) 100vw, 499px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Set Up Defaults<\/h2>\n\n\n\n<p>One of the things that will make setting up any firewall easier is to define some default rules for allowing and denying connections. UFW\u2019s defaults are to deny all incoming connections and allow all outgoing connections. This means anyone trying to reach your cloud server would not be able to connect, while any application within the server would be able to reach the outside world. To set the defaults used by UFW, you would use the following commands:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw default deny incoming<\/pre>\n\n\n\n<p>and<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw default allow outgoing<\/pre>\n\n\n\n<p>Note: if you want to be a little bit more restrictive, you can also deny all outgoing requests as well. The necessity of this is debatable, but if you have a public-facing cloud server, it could help prevent against any kind of remote shell connections. It does make your firewall more cumbersome to manage because you\u2019ll have to set up rules for all outgoing connections as well. You can set this as the default with the following:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw default deny outgoing<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Allow Connections<\/h2>\n\n\n\n<p>The syntax is pretty simple. You change the firewall rules by issuing commands in the terminal. If we turned on our firewall now, it would deny all incoming connections. If you\u2019re connected over SSH to your cloud server, that would be a problem because you would be locked out of your server. Let\u2019s enable SSH connections to our server to prevent that from happening:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw allow ssh<\/pre>\n\n\n\n<p>As you can see, the syntax for adding services is pretty simple. UFW comes with some defaults for common uses. Our SSH command above is one example. It\u2019s basically just shorthand for:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw allow 22\/tcp<\/pre>\n\n\n\n<p>This command allows a connection on port 22 using the TCP protocol. If our SSH server is running on port 2222, we could enable connections with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw allow 2222\/tcp<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Other Connections We Might Need<\/h3>\n\n\n\n<p>Now is a good time to allow some other connections we might need. If we\u2019re securing a web server with FTP access, we might need these commands:<\/p>\n\n\n\n<p><code>sudo ufw allow www<\/code>&nbsp;or&nbsp;<code>sudo ufw allow 80\/tcp<\/code>&nbsp;<code>sudo ufw allow ftp<\/code>&nbsp;or&nbsp;<code>sudo ufw allow 21\/tcp<\/code><\/p>\n\n\n\n<p>You mileage will vary on what ports and services you need to open. There will probably be a bit of testing necessary. In addition, you want to make sure you leave your SSH connection allowed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Port Ranges<\/h3>\n\n\n\n<p>You can also specify port ranges with UFW. To allow ports 1000 through 2000, use the command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw allow 1000:2000\/tcp<\/pre>\n\n\n\n<p>If you want UDP:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw allow 1000:2000\/udp<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">IP Addresses<\/h3>\n\n\n\n<p>You can also specify IP addresses. For example, if I wanted to allow connections from a specific IP address (say my work or home address), I\u2019d use this command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw allow from 192.168.255.255<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Denying Connections<\/h2>\n\n\n\n<p>Our default set up is to deny all incoming connections. This makes the firewall rules easier to administer since we are only selectively allowing certain ports and IP addresses through. However, if you want to flip it and open up all your server\u2019s ports (not recommended), you could allow all connections and then restrictively deny ports you didn\u2019t want to give access to by replacing \u201callow\u201d with \u201cdeny\u201d in the commands above. For example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw allow 80\/tcp<\/pre>\n\n\n\n<p>would allow access to port 80 while:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw deny 80\/tcp<\/pre>\n\n\n\n<p>would deny access to port 80.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Deleting Rules<\/h2>\n\n\n\n<p>There are two options to delete rules. The most straightforward one is to use the following syntax:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw delete allow ssh<\/pre>\n\n\n\n<p>As you can see, we use the command \u201cdelete\u201d and input the rules you want to eliminate after that. Other examples include:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw delete allow 80\/tcp<\/pre>\n\n\n\n<p>or<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw delete allow 1000:2000\/tcp<\/pre>\n\n\n\n<p>This can get tricky when you have rules that are long and complex.<\/p>\n\n\n\n<p>A simpler, two-step alternative is to type:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw status numbered<\/pre>\n\n\n\n<p>which will have UFW list out all the current rules in a numbered list. Then, we issue the command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw delete [number]<\/pre>\n\n\n\n<p>where \u201c[number]\u201d is the line number from the previous command.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Turn It On<\/h2>\n\n\n\n<p>After we\u2019ve gotten UFW to where we want it, we can turn it on using this command (remember: if you\u2019re connecting via SSH, make sure you\u2019ve set your SSH port, commonly port 22, to be allowed to receive connections):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw enable<\/pre>\n\n\n\n<p>You should see the command prompt again if it all went well. You can check the status of your rules now by typing:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw status<\/pre>\n\n\n\n<p>or<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw status verbose<\/pre>\n\n\n\n<p>for the most thorough display.<\/p>\n\n\n\n<p>To turn UFW off, use the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw disable<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Reset Everything<\/h2>\n\n\n\n<p>If, for whatever reason, you need to reset your cloud server\u2019s rules to their default settings, you can do this by typing this command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw reset\n\nProtect site with DoS Attack\n\nAdd these lines before COMMIT\n<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>### start ###\n# Enter rule\n-A ufw-before-input -p tcp --dport 80 -j ufw-http\n-A ufw-before-input -p tcp --dport 443 -j ufw-http\n\n# Limit connections per Class C\n-A ufw-http -p tcp --syn -m connlimit --connlimit-above 50 --connlimit-mask 24 -j ufw-http-logdrop\n\n# Limit connections per IP\n-A ufw-http -m state --state NEW -m recent --name conn_per_ip --set\n-A ufw-http -m state --state NEW -m recent --name conn_per_ip --update --seconds 10 --hitcount 20 -j ufw-http-logdrop\n\n# Limit packets per IP\n-A ufw-http -m recent --name pack_per_ip --set\n-A ufw-http -m recent --name pack_per_ip --update --seconds 1 --hitcount 20 -j ufw-http-logdrop\n\n# Finally accept\n-A ufw-http -j ACCEPT\n\n# Log\n-A ufw-http-logdrop -m limit --limit 3\/min --limit-burst 10 -j LOG --log-prefix \"&#91;UFW HTTP DROP] \"\n-A ufw-http-logdrop -j DROP\n### end ###<\/code><\/pre>\n\n\n\n<p>With the above rules we are limiting the connections per IP at 20 connections \/ 10 seconds \/ IP and the packets to 20 packets \/ second \/ IP.<\/p>\n\n\n\n<p>Finally we need to reload our firewall<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#ufw reload<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction One of the first lines of defense in securing your cloud server is a functioning firewall. In the past, this was often done through complicated and arcane utilities. There is a lot of functionality built into these utilities, iptables being the most popular nowadays, but they require a decent effort on behalf of the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-1040","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.balajibandi.com\/index.php?rest_route=\/wp\/v2\/posts\/1040","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.balajibandi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.balajibandi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.balajibandi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.balajibandi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1040"}],"version-history":[{"count":3,"href":"https:\/\/www.balajibandi.com\/index.php?rest_route=\/wp\/v2\/posts\/1040\/revisions"}],"predecessor-version":[{"id":1147,"href":"https:\/\/www.balajibandi.com\/index.php?rest_route=\/wp\/v2\/posts\/1040\/revisions\/1147"}],"wp:attachment":[{"href":"https:\/\/www.balajibandi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.balajibandi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.balajibandi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}