{"id":321,"date":"2025-09-20T13:58:53","date_gmt":"2025-09-20T05:58:53","guid":{"rendered":"https:\/\/yinyang.taichi.us.kg\/?p=321"},"modified":"2025-09-20T13:58:56","modified_gmt":"2025-09-20T05:58:56","slug":"ubuntu","status":"publish","type":"post","link":"https:\/\/yinyang.taichi.us.kg\/?k=29a36f2c54dd085f","title":{"rendered":"Ubuntu"},"content":{"rendered":"\n<h1>Recovering and Securing a Hacked Google Cloud VM: A Complete Guide<\/h1>\n\n<p>This guide documents the step-by-step process of recovering a compromised Google Cloud VM, troubleshooting complex SSH and system issues, and re-securing the server environment.<\/p>\n\n<hr>\n\n<h2>Step 1: Regaining Initial Access When Locked Out<\/h2>\n<p>If you&#8217;re locked out of your VM due to a compromised password, you can regain access using a startup script.<\/p>\n<h3>Action: Create a Temporary Admin User<\/h3>\n<p>Add a startup script to your VM&#8217;s metadata in the Google Cloud Console. This script creates a new user with sudo privileges.<\/p>\n<ol>\n    <li>Navigate to <strong>Compute Engine > VM instances<\/strong> and click <strong>EDIT<\/strong> on your VM.<\/li>\n    <li>Scroll to the <strong>Metadata<\/strong> section and add an item with:<\/li>\n    <ul>\n        <li><strong>Key:<\/strong> <code>startup-script<\/code><\/li>\n        <li><strong>Value (The Script):<\/strong>\n<pre><code>#!\/bin\/bash\nuseradd -m -s \/bin\/bash -G google-sudoers tempadmin\necho \"tempadmin:newpassword\" | chpasswd<\/code><\/pre>\n        <\/li>\n    <\/ul>\n    <li><strong>Save<\/strong> and then <strong>Stop\/Start<\/strong> the VM. You can now log in via the <strong>Serial Console<\/strong> with the username <code>tempadmin<\/code> and password <code>newpassword<\/code>.<\/li>\n<\/ol>\n\n<hr>\n\n<h2>Step 2: Troubleshooting the Serial Console<\/h2>\n<h3>Problem: Scrolling System Messages<\/h3>\n<p>The serial console can be flooded with kernel messages, making it difficult to type.<\/p>\n<h4>Action: Silence the Console<\/h4>\n<p>Run the following command to tell the kernel to only show critical panic messages:<\/p>\n<pre><code>echo \"1 4 1 7\" | sudo tee \/proc\/sys\/kernel\/printk<\/code><\/pre>\n\n<hr>\n\n<h2>Step 3: Securing User Accounts<\/h2>\n<h3>Problem: Resetting Passwords and Checking the Root Account<\/h3>\n<p>After regaining access, immediately secure your original user and verify the root account&#8217;s status.<\/p>\n<h4>Action: Reset Passwords and Lock Root<\/h4>\n<ol>\n    <li>Find your original username: <code>ls \/home<\/code><\/li>\n    <li>Reset its password: <code>sudo passwd your_original_username<\/code><\/li>\n    <li>Check the root account&#8217;s status: <code>sudo passwd --status root<\/code>. The output should be <code>root L<\/code> (Locked).<\/li>\n    <li>If it&#8217;s not locked, lock it for security: <code>sudo passwd --lock root<\/code><\/li>\n<\/ol>\n\n<hr>\n\n<h2>Step 4: Fixing Website and CDN Issues<\/h2>\n<h3>Problem: Website Down (Error 520) After VM Restart<\/h3>\n<p>Restarting a VM with an ephemeral IP address will change its IP, breaking DNS connections from your CDN.<\/p>\n<h4>Action: Update DNS and Set a Static IP<\/h4>\n<ol>\n    <li>Find the VM&#8217;s new <strong>External IP<\/strong> in the Google Cloud Console.<\/li>\n    <li>Update the <strong>&#8216;A&#8217; record<\/strong> in your CDN&#8217;s (e.g., QUIC.cloud or Cloudflare) DNS settings to point to the new IP.<\/li>\n    <li><strong>Prevent this in the future:<\/strong> In the Google Cloud Console, go to <strong>VPC network > IP addresses<\/strong> and reserve the VM&#8217;s current IP address to make it <strong>static<\/strong>.<\/li>\n<\/ol>\n\n<h3>Problem: Using Two CDNs (Cloudflare and QUIC.cloud)<\/h3>\n<p>Chaining CDNs slows down your site.<\/p>\n<h4>Action: Choose One CDN<\/h4>\n<ul>\n    <li><strong>If using LiteSpeed Server:<\/strong> Use QUIC.cloud as the primary CDN. In Cloudflare, set your domain\n\n\n\n<h1>Recovering and Securing a Hacked Google Cloud VM: A Complete Guide<\/h1>\n\n<p>This guide documents the step-by-step process of recovering a compromised Google Cloud VM, troubleshooting complex SSH and system issues, and re-securing the server environment.<\/p>\n\n<hr>\n\n<h2>Step 1: Regaining Initial Access When Locked Out<\/h2>\n<p>If you&#8217;re locked out of your VM due to a compromised password, you can regain access using a startup script.<\/p>\n<h3>Action: Create a Temporary Admin User<\/h3>\n<p>Add a startup script to your VM&#8217;s metadata in the Google Cloud Console. This script creates a new user with sudo privileges.<\/p>\n<ol>\n    <li>Navigate to <strong>Compute Engine > VM instances<\/strong> and click <strong>EDIT<\/strong> on your VM.<\/li>\n    <li>Scroll to the <strong>Metadata<\/strong> section and add an item with:<\/li>\n    <ul>\n        <li><strong>Key:<\/strong> <code>startup-script<\/code><\/li>\n        <li><strong>Value (The Script):<\/strong>\n<pre><code>#!\/bin\/bash\nuseradd -m -s \/bin\/bash -G google-sudoers tempadmin\necho \"tempadmin:newpassword\" | chpasswd<\/code><\/pre>\n        <\/li>\n    <\/ul>\n    <li><strong>Save<\/strong> and then <strong>Stop\/Start<\/strong> the VM. You can now log in via the <strong>Serial Console<\/strong> with the username <code>tempadmin<\/code> and password <code>newpassword<\/code>.<\/li>\n<\/ol>\n\n<hr>\n\n<h2>Step 2: Troubleshooting the Serial Console<\/h2>\n<h3>Problem: Scrolling System Messages<\/h3>\n<p>The serial console can be flooded with kernel messages, making it difficult to type.<\/p>\n<h4>Action: Silence the Console<\/h4>\n<p>Run the following command to tell the kernel to only show critical panic messages:<\/p>\n<pre><code>echo \"1 4 1 7\" | sudo tee \/proc\/sys\/kernel\/printk<\/code><\/pre>\n\n<hr>\n\n<h2>Step 3: Securing User Accounts<\/h2>\n<h3>Problem: Resetting Passwords and Checking the Root Account<\/h3>\n<p>After regaining access, immediately secure your original user and verify the root account&#8217;s status.<\/p>\n<h4>Action: Reset Passwords and Lock Root<\/h4>\n<ol>\n    <li>Find your original username: <code>ls \/home<\/code><\/li>\n    <li>Reset its password: <code>sudo passwd your_original_username<\/code><\/li>\n    <li>Check the root account&#8217;s status: <code>sudo passwd --status root<\/code>. The output should be <code>root L<\/code> (Locked).<\/li>\n    <li>If it&#8217;s not locked, lock it for security: <code>sudo passwd --lock root<\/code><\/li>\n<\/ol>\n\n<hr>\n\n<h2>Step 4: Fixing Website and CDN Issues<\/h2>\n<h3>Problem: Website Down (Error 520) After VM Restart<\/h3>\n<p>Restarting a VM with an ephemeral IP address will change its IP, breaking DNS connections from your CDN.<\/p>\n<h4>Action: Update DNS and Set a Static IP<\/h4>\n<ol>\n    <li>Find the VM&#8217;s new <strong>External IP<\/strong> in the Google Cloud Console.<\/li>\n    <li>Update the <strong>&#8216;A&#8217; record<\/strong> in your CDN&#8217;s (e.g., QUIC.cloud or Cloudflare) DNS settings to point to the new IP.<\/li>\n    <li><strong>Prevent this in the future:<\/strong> In the Google Cloud Console, go to <strong>VPC network > IP addresses<\/strong> and reserve the VM&#8217;s current IP address to make it <strong>static<\/strong>.<\/li>\n<\/ol>\n\n<h3>Problem: Using Two CDNs (Cloudflare and QUIC.cloud)<\/h3>\n<p>Chaining CDNs slows down your site.<\/p>\n<h4>Action: Choose One CDN<\/h4>\n<ul>\n    <li><strong>If using LiteSpeed Server:<\/strong> Use QUIC.cloud as the primary CDN. In Cloudflare, set your domain&#8217;s proxy status to <strong>DNS Only (Grey Cloud)<\/strong>.<\/li>\n    <li><strong>If using NGINX\/Apache:<\/strong> Use Cloudflare as the primary CDN. Set the proxy status to <strong>Proxied (Orange Cloud)<\/strong> and point it directly to your VM&#8217;s static IP.<\/li>\n<\/ul>\n\n<hr>\n\n<h2>Step 5: Solving Persistent SSH Failures (&#8220;Server refused our key&#8221;)<\/h2>\n<p>This was the most complex issue, caused by deep system changes from the hacker.<\/p>\n<h3>Action 1: Fix Firewall for &#8220;SSH-in-browser&#8221;<\/h3>\n<p>The browser-based SSH client connects via Google&#8217;s IAP service. You need a firewall rule to allow this.<\/p>\n<ul>\n    <li>Go to <strong>VPC network > Firewall<\/strong> and create a rule.<\/li>\n    <li><strong>Source IPv4 ranges:<\/strong> <code>35.235.240.0\/20<\/code><\/li>\n    <li><strong>Protocols and ports:<\/strong> TCP, port <code>22<\/code><\/li>\n<\/ul>\n\n<h3>Action 2: Fix File Permissions on the Server<\/h3>\n<p>The SSH service requires strict permissions. This is the most common cause of SSH key rejection.<\/p>\n<pre><code># Secure your home directory\nchmod g-w,o-w \/home\/your_username\n\n# Secure the .ssh directory\nchmod 700 \/home\/your_username\/.ssh\n\n# Secure the authorized_keys file\nchmod 600 \/home\/your_username\/.ssh\/authorized_keys\n\n# Ensure you own the files\nsudo chown -R your_username:your_username \/home\/your_username\/.ssh<\/code><\/pre>\n\n<h3>Action 3: Verify the SSH Server Configuration<\/h3>\n<p>If permissions are correct, ensure the SSH server itself is configured to allow key authentication.<\/p>\n<ol>\n    <li>Open the configuration file: <code>sudo nano \/etc\/ssh\/sshd_config<\/code><\/li>\n    <li>Ensure the following two lines are present and are not commented out (they should not start with #):\n<pre><code>PubkeyAuthentication yes\nAuthorizedKeysFile .ssh\/authorized_keys<\/code><\/pre>\n    <\/li>\n    <li>If you make changes, save the file and restart the SSH service: <code>sudo systemctl restart sshd<\/code><\/li>\n<\/ol>\n\n<h3>Action 4: The Final Fix &#8211; Restoring Sudo and Reinstalling SSH<\/h3>\n<p>The ultimate root cause was that the hacker had removed the user from the &#8220;sudoers&#8221; group, and the SSH server package was corrupted.<\/p>\n<ol>\n    <li><strong>Restore Sudo Rights:<\/strong> Add a startup script to the VM&#8217;s metadata to add your user back to the admin group and restart the VM.\n<pre><code>#!\/bin\/bash\nusermod -a -G google-sudoers your_username<\/code><\/pre>\n    <\/li>\n    <li><strong>Fix Broken SSH Installation:<\/strong> After regaining sudo access, completely purge and reinstall the SSH server to fix any corruption.\n<pre><code># Force fix any broken packages\nsudo dpkg --configure -a\n\n# Purge the old, broken server\nsudo apt-get purge openssh-server -y\n\n# Install a fresh, clean copy\nsudo apt-get install openssh-server -y<\/code><\/pre>\n    <\/li>\n    <li><strong>Re-add your Public Key:<\/strong> After reinstalling, the authorized_keys file will be empty. You must edit it (`nano ~\/.ssh\/authorized_keys`), paste your single-line public SSH key, and set the permissions one last time.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Recovering and Securing a Hacked Google Cloud VM: A Complete Guide This guide documents the step-by-step process of recovering a compromised Google Cloud VM, troubleshooting complex SSH and system issues, and re-securing the server environment. Step 1: Regaining Initial Access When Locked Out If you&#8217;re locked out of your VM due to a compromised password, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-321","post","type-post","status-publish","format-standard","hentry","category-itai"],"_links":{"self":[{"href":"https:\/\/yinyang.taichi.us.kg\/index.php?rest_route=\/wp\/v2\/posts\/321","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yinyang.taichi.us.kg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yinyang.taichi.us.kg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yinyang.taichi.us.kg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yinyang.taichi.us.kg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=321"}],"version-history":[{"count":4,"href":"https:\/\/yinyang.taichi.us.kg\/index.php?rest_route=\/wp\/v2\/posts\/321\/revisions"}],"predecessor-version":[{"id":325,"href":"https:\/\/yinyang.taichi.us.kg\/index.php?rest_route=\/wp\/v2\/posts\/321\/revisions\/325"}],"wp:attachment":[{"href":"https:\/\/yinyang.taichi.us.kg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yinyang.taichi.us.kg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yinyang.taichi.us.kg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}