Skip to content

Commit 579c98a

Browse files
jeroenvermeulenjvoisin
authored andcommittedJul 17, 2018
Automatic generation of Magento (1 and 2) whitelists
1 parent 882a1ab commit 579c98a

File tree

6 files changed

+1207
-45
lines changed

6 files changed

+1207
-45
lines changed
 

‎php-malware-finder/utils/generate_whitelist.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
output_list = list()
2525

2626
for curdir, dirnames, filenames in os.walk(sys.argv[2]):
27-
for filename in fnmatch.filter(filenames, '*.ph*'):
27+
for filename in filenames:
2828
fname = os.path.join(curdir, filename)
2929
if 0 < os.stat(fname).st_size < 5 * 1024 * 1024:
3030
matches = rules.match(fname, fast=True)
@@ -36,7 +36,6 @@
3636

3737
if output_list:
3838
output_rule = 'import "hash"\n\nrule %s\n{\n\tcondition:\n\t\t/* %s */\n\t\t' % (sys.argv[1].split(' ')[0], sys.argv[1])
39-
output_list.append(output_list.pop().replace(' or ', ' '))
4039
output_rule += '\n\t\t'.join(output_list)
41-
output_rule += '\n}'
40+
output_rule += '\n\t\tfalse\n}'
4241
print(output_rule)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
# Quit script if something goes wrong
3+
set -o errexit -o nounset -o pipefail;
4+
5+
SCRIPTDIR="$( dirname "$(readlink -f "$0")" )";
6+
OUTFILE="${SCRIPTDIR}/../whitelists/magento1ce.yar";
7+
TMPFILE="${OUTFILE}.new";
8+
9+
# First empty the target whitelist so we can completely generate a new one
10+
cat <<EOF >"${OUTFILE}";
11+
private rule Magento1Ce : ECommerce
12+
{
13+
condition:
14+
false
15+
}
16+
EOF
17+
18+
# Create a temporary directory and make sure it is empty
19+
GENTEMPDIR="$( mktemp -d --suffix="_gen_whitelist_m1" )";
20+
21+
# Add header to whitelist tempfile
22+
cat <<EOF | tee "${TMPFILE}";
23+
private rule Magento1Ce : ECommerce
24+
{
25+
condition:
26+
EOF
27+
28+
# Fetch tags (releases) from Github repo
29+
TAGS=$( git ls-remote --tags https://github.com/OpenMage/magento-mirror.git | cut -d '/' -f3 | grep -P '^[\d\.]+$' );
30+
31+
# Foreach tag (release)
32+
while read -r TAG; do
33+
# Download tarball of release
34+
wget "https://github.com/OpenMage/magento-mirror/archive/${TAG}.tar.gz" -O "${GENTEMPDIR}/${TAG}.tgz";
35+
# Unpack tarball
36+
tar -C "${GENTEMPDIR}" -xpzf "${GENTEMPDIR}/${TAG}.tgz";
37+
# Add version comment to whitelist tempfile
38+
echo " /* Magento CE ${TAG} */" | tee -a "${TMPFILE}";
39+
# Generate whitelist for version, add output to whitelist tempfile
40+
${SCRIPTDIR}/generate_whitelist.py "Magento CE ${TAG}" "${GENTEMPDIR}/magento-mirror-${TAG}" | grep 'hash.sha1' | sed "s|// ${GENTEMPDIR}/magento-mirror-${TAG}/|// |" | tee -a "${TMPFILE}";
41+
# Add white line, with indent
42+
echo " " | tee -a "${TMPFILE}";
43+
done <<< "${TAGS}";
44+
45+
# Add footer to whitelist tempfile
46+
cat <<EOF | tee -a "${TMPFILE}";
47+
false
48+
}
49+
EOF
50+
51+
# Copy temporary file to target whitelist while removing duplicate lines except empty ones
52+
cat "${TMPFILE}" | awk 'match($0,/^\s*$/)||!seen[$0]++' > "${OUTFILE}";
53+
54+
# Clean up
55+
rm "${TMPFILE}";
56+
rm -rf "${GENTEMPDIR}";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/bin/bash
2+
# Quit script if something goes wrong
3+
set -o errexit -o nounset -o pipefail;
4+
5+
SCRIPTDIR="$( dirname "$(readlink -f "$0")" )";
6+
OUTFILE="${SCRIPTDIR}/../whitelists/magento2.yar";
7+
TMPFILE="${OUTFILE}.new";
8+
9+
# First empty the target whitelist so we can completely generate a new one
10+
cat <<EOF >"${OUTFILE}";
11+
private rule Magento2 : ECommerce
12+
{
13+
condition:
14+
false
15+
}
16+
EOF
17+
18+
# Create a temporary directory and make sure it is empty
19+
GENTEMPDIR="$( mktemp -d --suffix="_gen_whitelist_m2" )";
20+
21+
# Composer access tokens
22+
if [ ! -f "${HOME}/.composer/auth.json" ]; then
23+
echo -e "\nYou have no '.composer/auth.json' in your home dir. We will create it from a template and open an editor.";
24+
echo -e "Press [Enter] to continue. Press Ctrl-C if you wish to leave.";
25+
read;
26+
mkdir -p "${HOME}/.composer";
27+
cat <<EOF >"${HOME}/.composer/auth.json"
28+
{
29+
"INFO_GITHUB": "==== GET TOKEN: https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/ ====",
30+
"github-oauth": {
31+
"github.com": "---github-token-goes-here---"
32+
},
33+
"INFO_MAGENTO": "==== GET TOKEN: https://devdocs.magento.com/guides/v2.0/install-gde/prereq/connect-auth.html ====",
34+
"http-basic": {
35+
"repo.magento.com": {
36+
"username": "---public-key-goes-here---",
37+
"password": "---private-key-goes-here---"
38+
}
39+
}
40+
}
41+
EOF
42+
editor "${HOME}/.composer/auth.json";
43+
fi
44+
45+
# Add header to whitelist tempfile
46+
cat <<EOF | tee "${TMPFILE}";
47+
private rule Magento2 : ECommerce
48+
{
49+
condition:
50+
EOF
51+
52+
# Fetch tags (releases) from Github repo
53+
TAGS=$( git ls-remote --tags https://github.com/magento/magento2.git | cut -d '/' -f3 | grep -P '^[\d\.]+$' | sort -V );
54+
55+
# Foreach tag (release)
56+
while read -r TAG; do
57+
# Download tarball of release
58+
wget "https://github.com/magento/magento2/archive/${TAG}.tar.gz" -O "${GENTEMPDIR}/${TAG}.tgz";
59+
# Unpack tarball
60+
tar -C "${GENTEMPDIR}" -xpzf "${GENTEMPDIR}/${TAG}.tgz";
61+
# Run 'composer install' inside unpacked release
62+
SOURCEDIR="${GENTEMPDIR}/magento2-${TAG}";
63+
composer --working-dir="${SOURCEDIR}" -- install;
64+
# Add version comment to whitelist
65+
echo " /* Magento2 ${TAG} */" | tee -a "${TMPFILE}";
66+
# Generate whitelist for version, add output to whitelist tempfile
67+
${SCRIPTDIR}/generate_whitelist.py "Magento2 ${TAG}" "${SOURCEDIR}" | grep 'hash.sha1' | sed "s|// ${SOURCEDIR}/|// |" | tee -a "${TMPFILE}";
68+
# Add white line, with indent
69+
echo " " | tee -a "${TMPFILE}";
70+
done <<< "${TAGS}";
71+
72+
# Add footer to whitelist tempfile
73+
cat <<EOF | tee -a "${TMPFILE}";
74+
false
75+
}
76+
EOF
77+
78+
# Copy temporary file to target whitelist while removing duplicate lines except empty ones
79+
cat "${TMPFILE}" | awk 'match($0,/^\s*$/)||!seen[$0]++' > "${OUTFILE}";
80+
81+
# Clean up
82+
rm "${TMPFILE}";
83+
rm -rf "${GENTEMPDIR}";

‎php-malware-finder/whitelist.yar

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ include "whitelists/drupal.yar"
88
include "whitelists/wordpress.yar"
99
include "whitelists/symfony.yar"
1010
include "whitelists/phpmyadmin.yar"
11+
include "whitelists/magento1ce.yar"
1112
include "whitelists/magento2.yar"
1213
include "whitelists/prestashop.yar"
1314
include "whitelists/custom.yar"
@@ -116,6 +117,7 @@ private rule IsWhitelisted
116117
Wordpress or
117118
Prestashop or
118119
Magento or
120+
Magento1Ce or
119121
Magento2 or
120122
Drupal or
121123
Roundcube or

0 commit comments

Comments
 (0)
Failed to load comments.