<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jay Tailor</title>
    <description>The latest articles on DEV Community by Jay Tailor (@jaytailor45).</description>
    <link>https://dev.to/jaytailor45</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F163359%2Fa4f0dd17-38a2-4a6a-9bcc-e367b9f6b6ca.png</url>
      <title>DEV Community: Jay Tailor</title>
      <link>https://dev.to/jaytailor45</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jaytailor45"/>
    <language>en</language>
    <item>
      <title>The Anatomy of a PostgreSQL Hack: How It Happened and What We Did About It.</title>
      <dc:creator>Jay Tailor</dc:creator>
      <pubDate>Fri, 10 Feb 2023 11:20:26 +0000</pubDate>
      <link>https://dev.to/jaytailor45/the-anatomy-of-a-postgresql-hack-how-it-happened-and-what-we-did-about-it-b9k</link>
      <guid>https://dev.to/jaytailor45/the-anatomy-of-a-postgresql-hack-how-it-happened-and-what-we-did-about-it-b9k</guid>
      <description>&lt;p&gt;A database hack can have serious consequences for an organization, including financial loss, damage to reputation, and loss of sensitive information. PostgreSQL is a popular open-source relational database management system, but it is not immune to hacking attempts. In this article, we will discuss the very basic way in which a PostgreSQL database can be hacked, as well as best practices for preventing and responding to such a breach. By understanding the risks and taking steps to secure your PostgreSQL database, you can help protect your organization and its data.&lt;/p&gt;

&lt;h2&gt;
  
  
  The beginning
&lt;/h2&gt;

&lt;p&gt;I have been using PostgreSQL in my web application using a Docker container. I noticed that suddenly my server usage spiked up to 400%! At FULL CAPACITY! &lt;em&gt;(more than 100% as I had 4 virtual CPUs)&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The quick fix
&lt;/h2&gt;

&lt;p&gt;I fixed it by restarting Postgres docker container. It fixed the issue but it happened again. I assumed I got hacked. So I decided to move to a new server instance. I go through all the processes to deploy a full-stack web application from scratch.&lt;br&gt;
To me, that fixed the issue only for a week or so. Then I received an email from the hosting provider saying &lt;code&gt;"A network scan has been performed from the IP address which is under your server"&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The realization
&lt;/h2&gt;

&lt;p&gt;I was not sure what exactly was going on. On initial research, I suspect that Postgres runs a service called vacuum. The Postgres docs say,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;VACUUM reclaims storage occupied by dead tuples. In normal PostgreSQL operation, tuples that are deleted or obsoleted by an update are not physically removed from their table; they remain present until a VACUUM is done. Therefore it's necessary to do VACUUM periodically, especially on frequently-updated tables.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I believed that the vacuum is the culprit for all of these. Because high CPU usage was getting fixed as soon as I restart the Postgres docker container.&lt;/p&gt;

&lt;p&gt;But I was wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  The final fix
&lt;/h2&gt;

&lt;p&gt;After a week of restarting the server and same time searching for a potential solution, I found that my server was hacked indeed.&lt;br&gt;
I tried a lot of things to fix the issue. Finally, I came up with a solution that worked.&lt;br&gt;
Long story short, I fixed the server by recreating a new Postgres container with a strong password.&lt;/p&gt;

&lt;h2&gt;
  
  
  Technical overview
&lt;/h2&gt;

&lt;p&gt;There are different kinds of hackers and different kinds of hacks they perform. One I was the victim of was the PostgresSQL server hack.&lt;br&gt;
The hacker got into my server and was mining cryptocurrency using my server resources at full capacity.&lt;/p&gt;

&lt;p&gt;The hacker got into my server by guessing the supper user password of the database server. They used the &lt;a href="https://stackoverflow.com/questions/8787075/postgresql-export-large-object-to-client" rel="noopener noreferrer"&gt;lo_export&lt;/a&gt; utility to upload malicious executables for a user-defined-function which executes shell commands. Function with random name &lt;code&gt;fun451813001&lt;/code&gt; is created, the SQL function which was created to wrap the executable. Now hackers have access to my database server and server resources.&lt;/p&gt;

&lt;p&gt;Things don't end here. They use &lt;a href="https://nmap.org/" rel="noopener noreferrer"&gt;nmap&lt;/a&gt; to find more publicly exposed PostgreSQL databases on the same network to find the next victims. This is the reason why I got the following notice from the server provider.&lt;br&gt;
&lt;code&gt;"A network scan has been performed from the IP address which is under your server"&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we learn about how hackers can get into our system and use servers to do potentially anything.&lt;br&gt;
Also, we learn that simple things like choosing strong can help you from a serious data breach or heavy financial loss.&lt;/p&gt;

&lt;p&gt;If you like the post please share your thoughts in the comments. Feel free to follow me &lt;a class="mentioned-user" href="https://dev.to/jaytailor45"&gt;@jaytailor45&lt;/a&gt; for more tech content. Have a fantastic day. &lt;/p&gt;

&lt;p&gt;TLDR; DON'T USE SIMPLE PASSWORD&lt;/p&gt;

&lt;p&gt;PS - One of my friend &lt;a class="mentioned-user" href="https://dev.to/slk5611"&gt;@slk5611&lt;/a&gt; has also written a blog and mongodb attack. Here's the interesting read on &lt;a href="https://dev.to/slk5611/avoid-a-malicious-attack-mongodb-how-it-happened-and-what-we-did-about-it-51hg"&gt;Avoid a Malicious Attack MongoDB: How It Happened and What We Did About It&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>hack</category>
      <category>database</category>
      <category>security</category>
    </item>
  </channel>
</rss>
