2

I am facing an issue where unwanted JavaScript code is being injected into my Magento 2 CMS static blocks. Every time I manually remove this obfuscated JavaScript code from a static block, it reappears automatically after about 5 minutes. enter image description here

Steps I Followed:

  1. Navigated to Content → Blocks in Magento 2 admin.
  2. Edited the affected static block and removed the injected JavaScript.
  3. Saved the block.
  4. After a few minutes, the unwanted JavaScript was injected back into the block.

Troubleshooting Done:

  • Checked the core_config_data table to see if any suspicious scripts are stored in the database.
  • Scanned Magento files for possible malware injections.
  • Reviewed Magento logs (var/log/system.log, var/log/exception.log) but found no relevant errors.
  • Disabled third-party extensions temporarily to see if any extension is causing this issue.
  • Checked for any automated cron jobs that might be modifying the CMS block content.

Possible Causes I Suspect:

  1. Malware Infection: There might be a malicious script running on the server injecting this code.
  2. Compromised Admin Account: A script or extension might be modifying CMS content using Magento's API.
  3. Database Injection: The JavaScript could be stored directly in the database and injected whenever the page loads.

My Questions:

  1. How can I prevent this JavaScript from being re-added to my CMS blocks?
  2. Are there any specific Magento logs or database tables I should inspect further?
  3. Has anyone encountered a similar issue before, and what was the resolution?

Any guidance would be greatly appreciated! Thanks in advance.

2
  • Use ecomscan to find persistent malware on your server. Commented Mar 2 at 16:26
  • Hi @Bhagyesh, Did you find any solution? I am facing the same issue after upgrading to 2.4.7-p4. Commented Mar 6 at 6:04

2 Answers 2

3

It might be that cosmicsting attack in which hacker somehow using Magento vulnerability and get the encrypt key of evn and can use add script to capture details. Can read more here. https://sansec.io/research/cosmicsting.

First upgrade your Magento version to lastest or apply patch "CVE-2024-34102". Recommended to upgrade Magento.

Change your encrypt key.

Can also use cloud watch to get updates on any change in any CMS block.

1

It happened to us several times.

Here is what helped:

  • Upgrade to the latest Magento 2 version.
  • Password and IP protect the admin area. Use HTTP Basic Auth and allow only selected IPs to view the backend.
  • 2FA for the backend.
  • Force admin password change every 30 days (Stores > Configuration > Advanced > Admin Security).
  • Set admin session lifetime to 15 minutes. enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.