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

Sploit 947 | Added EC2 Open Custom Ports plugin, spec file #304

Closed
wants to merge 96 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
96 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
357cd0b
SPLOIT-113: Added Plain Text Parameters plugin for CloudFormation
AkhtarAmir Aug 14, 2020
77ca002
Added plugin and spec file for launch wizard security groups
AkhtarAmir Aug 14, 2020
73172b8
Added vpcEndpointAcceptance plugin and spec file
AkhtarAmir Aug 13, 2020
7b448ff
Refactored code in plaintextParameters plugin and spec file
AkhtarAmir Aug 14, 2020
50d9d70
SPLOIT-113: Updated custom settings
AkhtarAmir Aug 14, 2020
46ff92c
Made PR requested changes
AkhtarAmir Aug 14, 2020
ec1e5ef
SPLOIT-113: Added regex to check if NoEcho is enabled
AkhtarAmir Aug 14, 2020
e89dd5e
Accommodated PR changes
AkhtarAmir Aug 17, 2020
4d7ee30
Fixed eslint issues
AkhtarAmir Aug 18, 2020
f5c9bf0
Update index.js
AkhtarAmir Aug 18, 2020
53e18b8
Update index.js
AkhtarAmir Aug 18, 2020
6ec73bc
Accomodated PR changes
AkhtarAmir Aug 18, 2020
2f55a11
Updated status in result of failure
AkhtarAmir Aug 18, 2020
0ba2fbf
SPLOIT-113: Added Plain Text Parameters plugin for CloudFormation
AkhtarAmir Aug 12, 2020
e9415b9
SPLOIT-113: Added Plain Text Parameters plugin for CloudFormation
AkhtarAmir Aug 14, 2020
8820775
Added plugin and spec file for launch wizard security groups
AkhtarAmir Aug 14, 2020
8e6b23b
Added vpcEndpointAcceptance plugin and spec file
AkhtarAmir Aug 13, 2020
82d8406
Refactored code in plaintextParameters plugin and spec file
AkhtarAmir Aug 14, 2020
b022a52
SPLOIT-113: Updated custom settings
AkhtarAmir Aug 14, 2020
d3ee380
Made PR requested changes
AkhtarAmir Aug 14, 2020
560d273
SPLOIT-113: Added regex to check if NoEcho is enabled
AkhtarAmir Aug 14, 2020
9195d32
Accommodated PR changes
AkhtarAmir Aug 17, 2020
0287cc5
Fixed eslint issues
AkhtarAmir Aug 18, 2020
1315ccd
Update exports.js
AkhtarAmir Aug 18, 2020
229461c
Update index.js
AkhtarAmir Aug 18, 2020
f5a3b8b
Update index.js
AkhtarAmir Aug 18, 2020
c574f76
Accomodated PR changes
AkhtarAmir Aug 18, 2020
1a4b495
Updated status in result of failure
AkhtarAmir Aug 18, 2020
a45a9f7
Merge branch 'master' of https://github.com/AkhtarAmir/scans into master
AkhtarAmir Aug 28, 2020
156d027
Removed unnecesary rebase changes
AkhtarAmir Aug 28, 2020
339dd44
Merge branch 'feature/SPLOIT-947' of https://github.com/AkhtarAmir/sc…
AkhtarAmir Aug 21, 2020
5375e9f
SPLOIT-947: Added 'Custom Open Ports' plugin
AkhtarAmir Aug 16, 2020
e1de20e
Refactor plugin file
AkhtarAmir Aug 20, 2020
339275e
SPLOIT-947: Added 'Custom Open Ports' plugin
AkhtarAmir Aug 16, 2020
90f8c7b
Refactor plugin file
AkhtarAmir Aug 20, 2020
730b020
Merge branch 'sploit-947' of https://github.com/AkhtarAmir/scans into…
AkhtarAmir Sep 4, 2020
32ee6d9
Merge remote-tracking branch 'upstream/master' into master
AkhtarAmir Sep 12, 2020
5ae196a
Merge branch 'master' of https://github.com/AkhtarAmir/scans into fea…
AkhtarAmir Sep 13, 2020
c21fadb
Accommodated PR changes
AkhtarAmir Sep 13, 2020
e8ecabf
Merge branch 'master' into sploit-947
AkhtarAmir Sep 13, 2020
6744c09
Accommodated PR changes requested
AkhtarAmir Sep 20, 2020
c687820
Merge remote-tracking branch 'upstream/master' into master
AkhtarAmir Sep 22, 2020
c24eae0
Accommodated PR changes
AkhtarAmir Sep 23, 2020
a73983f
Accommodated PR changes
AkhtarAmir Sep 23, 2020
29075e7
Merge branch 'master' of https://github.com/aquasecurity/cloudsploit …
AkhtarAmir Sep 24, 2020
c51edd1
Merge remote-tracking branch 'upstream/master' into master
AkhtarAmir Sep 25, 2020
d6c8d47
Merge branch 'master' of https://github.com/AkhtarAmir/scans into fea…
AkhtarAmir Oct 16, 2020
50837f9
Modified plugin to avoid infinite loop
AkhtarAmir Oct 17, 2020
bc1bb60
Merge branch 'feature/SPLOIT-947' of https://github.com/AkhtarAmir/sc…
AkhtarAmir Oct 17, 2020
31412b5
Update plugins/aws/ec2/openCustomPorts.js
AkhtarAmir Oct 19, 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
3 changes: 2 additions & 1 deletion exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ module.exports = {
'crossVpcPublicPrivate' : require(__dirname + '/plugins/aws/ec2/crossVpcPublicPrivate.js'),
'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'),
'ebsUnusedVolumes' : require(__dirname + '/plugins/aws/ec2/ebsUnusedVolumes.js'),
'ec2MetadataOptions' : require(__dirname + '/plugins/aws/ec2/ec2MetadataOptions.js'),

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

Expand Down
128 changes: 128 additions & 0 deletions plugins/aws/ec2/openCustomPorts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
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: 'tcp:80,tcp:443'
}
},

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

var allowed_open_ports = settings.open_port_allowed_list || this.settings.open_port_allowed_list.default;
allowed_open_ports = allowed_open_ports.split(',');

var ports = {};
allowed_open_ports.forEach(port => {
var [protocol, portNo] = port.split(':');
if (ports[protocol]) {
ports[protocol].push(Number(portNo));
} else {
ports[protocol] = [Number(portNo)];
}
});

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 = `arn:${awsOrGov}:ec2:${region}:${group.OwnerId}:security-group/${group.GroupId}`;

if (!group.IpPermissions) continue;

var cidrIps = [];

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

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

if (range.CidrIp && range.CidrIp === '0.0.0.0/0') {
var allowedPorts = ports[permission.IpProtocol] || [];
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 (!allowedPorts.includes(port)) {
if (!cidrIps.length) cidrIps.push(range.CidrIp);
break IpRangesLoop;
}
}
}
}


for (var l in permission.Ipv6Ranges) {
var rangeV6 = permission.Ipv6Ranges[l];

if (rangeV6.CidrIpv6 && rangeV6.CidrIpv6 === '::/0') {
var allowedV6Ports = ports[permission.IpProtocol] || [];
var portRangeV6 = permission.ToPort - permission.FromPort;

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

if (!allowedV6Ports.includes(portV6)) {
cidrIps.push(rangeV6.CidrIpv6);
break IpPermissionsLoop;
}
}
}
}
}

if (!cidrIps.length) {
helpers.addResult(results, 0,
`Security group "${group.GroupName}" does not have ports open to 0.0.0.0/0`,
region, resource);
} else {
helpers.addResult(results, 2,
`Security group "${group.GroupName}" has ports open to ${cidrIps.join(', ')}`,
region, resource);
}
}

rcb();
}, function(){
callback(null, results, source);
});
}
};
167 changes: 167 additions & 0 deletions plugins/aws/ec2/openCustomPorts.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
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 security groups'
},
},
},
},
};
};

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

describe('openCustomPorts', function () {
describe('run', function () {
it('should FAIL if security group has open ports', 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 security group does not have open ports', 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();
});
});

it('should PASS if no security groups found', 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 UNKNOWN if unable to describe security groups', 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 not return any results if describe security groups response not found', function (done) {
const cache = createNullCache();
openCustomPorts.run(cache, {}, (err, results) => {
expect(results.length).to.equal(0);
done();
});
});
});
});