Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/sploit 947 | Added Open Custom Ports plugin #277

Closed
wants to merge 46 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
e1708af
SPLOIT-113: Added Plain Text Parameters plugin for CloudFormation
AkhtarAmir Aug 12, 2020
2122ade
Added vpcEndpointAcceptance plugin and spec file
AkhtarAmir Aug 13, 2020
306d721
SPLOIT-113: Added Plain Text Parameters plugin for CloudFormation
AkhtarAmir Aug 14, 2020
7c8616c
Added plugin and spec file for launch wizard security groups
AkhtarAmir Aug 14, 2020
de52e8a
Merge branch 'master' into feature/ec2-plugin-mark-launch-wizard-secu…
AkhtarAmir Aug 14, 2020
f3b72f6
Merge pull request #1 from AkhtarAmir/feature/ec2-plugin-mark-launch-…
AkhtarAmir Aug 14, 2020
d32cbac
Merge branch 'master' of https://github.com/AkhtarAmir/scans into fea…
AkhtarAmir Aug 14, 2020
3da6672
Refactored code in plaintextParameters plugin and spec file
AkhtarAmir Aug 14, 2020
928c712
Merge pull request #2 from AkhtarAmir/feature/SPLOIT-113
AkhtarAmir Aug 14, 2020
ce65325
SPLOIT-113: Updated custom settings
AkhtarAmir Aug 14, 2020
f3e3ab5
Merge pull request #3 from AkhtarAmir/feature/SPLOIT-113
AkhtarAmir Aug 14, 2020
e70b96a
Made PR requested changes
AkhtarAmir Aug 14, 2020
f62a1d5
SPLOIT-113: Added regex to check if NoEcho is enabled
AkhtarAmir Aug 14, 2020
865e0b6
Merge pull request #4 from AkhtarAmir/feature/SPLOIT-113
AkhtarAmir Aug 14, 2020
9745614
Merge remote-tracking branch 'upstream/master' into master
AkhtarAmir Aug 15, 2020
1b80ac4
Accommodated PR changes
AkhtarAmir Aug 17, 2020
c0dc834
Fixed eslint issues
AkhtarAmir Aug 18, 2020
7d457bd
Update exports.js
AkhtarAmir Aug 18, 2020
8c2466c
Fixed eslint issues
AkhtarAmir Aug 18, 2020
ccb92ee
Update index.js
AkhtarAmir Aug 18, 2020
93c553d
Update index.js
AkhtarAmir Aug 18, 2020
cf21d1d
Added cloudformation in china and gov regions
AkhtarAmir Aug 18, 2020
aac8ece
Accomodated PR changes
AkhtarAmir Aug 18, 2020
7707dbd
Updated status in result of failure
AkhtarAmir Aug 18, 2020
45b7973
SPLOIT-947: Added 'Custom Open Ports' plugin
AkhtarAmir Aug 16, 2020
fb12862
Refactor plugin file
AkhtarAmir Aug 20, 2020
a4ea92f
SPLOIT-113: Added Plain Text Parameters plugin for CloudFormation
AkhtarAmir Aug 14, 2020
8e5b6be
Added plugin and spec file for launch wizard security groups
AkhtarAmir Aug 14, 2020
a9afe44
Added vpcEndpointAcceptance plugin and spec file
AkhtarAmir Aug 13, 2020
0323a98
Refactored code in plaintextParameters plugin and spec file
AkhtarAmir Aug 14, 2020
aab9f79
SPLOIT-113: Updated custom settings
AkhtarAmir Aug 14, 2020
12d88c1
Made PR requested changes
AkhtarAmir Aug 14, 2020
7dd1a5b
SPLOIT-113: Added regex to check if NoEcho is enabled
AkhtarAmir Aug 14, 2020
c8a23c3
Accommodated PR changes
AkhtarAmir Aug 17, 2020
92821dd
Fixed eslint issues
AkhtarAmir Aug 18, 2020
e32accc
Update index.js
AkhtarAmir Aug 18, 2020
90094a2
Update index.js
AkhtarAmir Aug 18, 2020
c29ab07
Accomodated PR changes
AkhtarAmir Aug 18, 2020
49f1202
Updated status in result of failure
AkhtarAmir Aug 18, 2020
fa70d92
Updated master from upstream/master
AkhtarAmir Aug 21, 2020
278ad3f
SPLOIT-947: Added 'Custom Open Ports' plugin
AkhtarAmir Aug 16, 2020
67a0d81
Refactor plugin file
AkhtarAmir Aug 20, 2020
e11122a
SPLOIT-947: Added 'Custom Open Ports' plugin
AkhtarAmir Aug 16, 2020
d9ac92d
Refactor plugin file
AkhtarAmir Aug 20, 2020
69fec8c
Merge branch 'feature/SPLOIT-947' of https://github.com/AkhtarAmir/sc…
AkhtarAmir Aug 21, 2020
a3ed63f
Updated openCustomPorts plugin
AkhtarAmir Aug 24, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ module.exports = {
'vpcEndpointAcceptance' : require(__dirname + '/plugins/aws/ec2/vpcEndpointAcceptance'),
'ebsEncryptedSnapshots' : require(__dirname + '/plugins/aws/ec2/ebsEncryptedSnapshots.js'),
'ec2MetadataOptions' : require(__dirname + '/plugins/aws/ec2/ec2MetadataOptions.js'),
'openCustomPorts' : require(__dirname + '/plugins/aws/ec2/openCustomPorts.js'),

'efsEncryptionEnabled' : require(__dirname + '/plugins/aws/efs/efsEncryptionEnabled.js'),

Expand Down
91 changes: 91 additions & 0 deletions plugins/aws/ec2/openCustomPorts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
var async = require('async');
var helpers = require('../../../helpers/aws');

module.exports = {
title: 'Open Custom Ports',
category: 'EC2',
description: 'Ensures that the defined ports are not exposed publicly',
more_info: 'Security groups should be used to restrict access to ports from known networks.',
link: 'https://docs.aws.amazon.com/vpc/latest/userguide/VPC_SecurityGroups.html',
recommended_action: 'Modify the security group to ensure the ports are not exposed publicly.',
apis: ['EC2:describeSecurityGroups'],
settings: {
open_port_allowed_list: {
name: 'EC2 Allowed Open Ports',
description: 'A comma-delimited list of ports that indicates open ports allowed for any connection',
regex: '[a-zA-Z0-9,]',
default: [80, 443]
}
},

run: function(cache, settings, callback) {
var results = [];
var source = {};
var regions = helpers.regions(settings);

var allowed_open_ports = settings.open_port_allowed_list || this.settings.open_port_allowed_list.default;

async.each(regions.ec2, function(region, rcb){
var describeSecurityGroups = helpers.addSource(cache, source,
['ec2', 'describeSecurityGroups', region]);
if (!describeSecurityGroups) return rcb();
if (describeSecurityGroups.err || !describeSecurityGroups.data) {
helpers.addResult(results, 3,
'Unable to query for security groups: ' + helpers.addError(describeSecurityGroups), region);
return rcb();
}

if (!describeSecurityGroups.data.length) {
helpers.addResult(results, 0, 'No security groups found', region);
return rcb();
}

// Loop through each security group
for (var g in describeSecurityGroups.data) {
var group = describeSecurityGroups.data[g];
var resource = group.GroupId;
AkhtarAmir marked this conversation as resolved.
Show resolved Hide resolved
var openPorts = [];

if (!group.IpPermissions) continue;

// Loop through each ip permissions in a security group
for (var p in group.IpPermissions) {
var permission = group.IpPermissions[p];

// Loop through each ip range for an ip permissions list
for (var r in permission.IpRanges) {
var range = permission.IpRanges[r];

if (range.CidrIp && range.CidrIp === '0.0.0.0/0') {
var portRange = permission.ToPort - permission.FromPort;

// Check for all the ports in port range
for (let p=0; p <= portRange; p++) {
var port = permission.FromPort + p;

if (!allowed_open_ports.includes(port)) {
var openPort = permission.IpProtocol.toUpperCase() + ' port ' + port;
if (openPorts.indexOf(openPort) === -1) openPorts.push(openPort);
}
}
}
}
}

if (openPorts.length) {
helpers.addResult(results, 2,
'Security group ' + group.GroupName + ' has: ' + openPorts.join(' , ') + ' open to 0.0.0.0/0',
region, resource);
} else {
helpers.addResult(results, 0,
'Security group: ' + group.GroupName + ' has no open ports',
region, resource);
}
}

rcb();
}, function(){
callback(null, results, source);
});
}
};
169 changes: 169 additions & 0 deletions plugins/aws/ec2/openCustomPorts.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
var expect = require('chai').expect;
const openCustomPorts = require('./openCustomPorts');

const securityGroups = [
{
"Description": "Allows SSh access to developer",
"GroupName": "spec-test-sg",
"IpPermissions": [{
"FromPort": 25,
"IpProtocol": "tcp",
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"Ipv6Ranges": [
{
"CidrIpv6": "::/0"
}
],
"PrefixListIds": [],
"ToPort": 30,
"UserIdGroupPairs": []
}],
"OwnerId": "12345654321",
"GroupId": "sg-0b5f2771716acfee4",
"IpPermissionsEgress": [
{
"FromPort": 25,
"IpProtocol": "tcp",
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"Ipv6Ranges": [
{
"CidrIpv6": "::/0"
}
],
"PrefixListIds": [],
"ToPort": 25,
"UserIdGroupPairs": []
}
],
"VpcId": "vpc-99de2fe4"
},
{
"Description": "launch-wizard-1 created 2020-08-10T14:28:09.271+05:00",
"GroupName": "launch-wizard-1",
"IpPermissions": [
{
"FromPort": 80,
"IpProtocol": "tcp",
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"Ipv6Ranges": [],
"PrefixListIds": [],
"ToPort": 80,
"UserIdGroupPairs": []
}
],
"OwnerId": "12345654321",
"GroupId": "sg-0ff1642cae23c309a",
"IpPermissionsEgress": [
{
"IpProtocol": "-1",
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"Ipv6Ranges": [],
"PrefixListIds": [],
"UserIdGroupPairs": []
}
],
"VpcId": "vpc-99de2fe4"
}
]

const createCache = (groups) => {
return {
ec2: {
describeSecurityGroups: {
'us-east-1': {
data: groups
},
},
},
};
};

const createErrorCache = () => {
return {
ec2: {
describeSecurityGroups: {
'us-east-1': {
err: {
message: 'error describing cloudformation stacks'
},
},
},
},
};
};

const createNullCache = () => {
return {
ec2: {
describeSecurityGroups: {
'us-east-1': null,
},
},
};
};

describe('openCustomPorts', function () {
describe('run', function () {

it('should not return any results if unable to fetch any security groups description', function (done) {
const cache = createNullCache();
openCustomPorts.run(cache, {}, (err, results) => {
expect(results.length).to.equal(0);
done();
});
});

it('should UNKNOWN if error occurs while fetching any security groups description', function (done) {
const cache = createErrorCache();
openCustomPorts.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(3);
done();
});
});

it('should PASS if no security groups are present', function (done) {
const cache = createCache([]);
openCustomPorts.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(0);
done();
});
});

it('should FAIL if any public open port is found', function (done) {
const cache = createCache([securityGroups[0]]);
openCustomPorts.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(2);
done();
});
});

it('should PASS if no public open port is found', function (done) {
const cache = createCache([securityGroups[1]]);
openCustomPorts.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(0);
done();
});
});

});
});