コーヒーが好きな emi です。
AWS 環境で Active Directory(以降、AD)ドメイン環境を構築するには、非常に多くのプロトコルやポートを許可する必要があります。
EC2 上の Windows Server 2022 で AD ドメイン環境を構築する際、AD に必要な通信を許可するセキュリテイグループルールを一発で設定する AWS CLI コマンドを作成しました。
今回は AWS アプリケーションとネイティブの Active Directory ワークロードのサポート を参考に作成しました。
コマンド
セキュリテイグループの作成
あらかじめセキュリテイグループは作成しておいてください。
AWS CLI コマンドで作成する場合は以下のコマンドを参考にしてください。
以下のコマンドを実行いただくと、AD-sg
という名前のセキュリティグループができます。
aws ec2 create-security-group \
--description "AD-sg" \
--group-name AD-sg \
--vpc-id vpc-xxxxxxxxxxxxxxxxx \
--tag-specifications "ResourceType=security-group,Tags=[{Key=Name,Value=AD-sg}]"
VPC は作成済の想定で、--vpc-id
には VPC ID(vpc-xxxxxxxxxxxxxxxx
)をコピーして貼り付けて実行してください。
CloudShell での実行結果は以下のようになります。
実行結果(クリックで展開)
[cloudshell-user@ip-10-6-83-5 ~]$ aws ec2 create-security-group \
> --description "AD-sg" \
> --group-name AD-sg \
> --vpc-id vpc-09e3f648fdb492c1b \
> --tag-specifications "ResourceType=security-group,Tags=[{Key=Name,Value=AD-sg}]"
{
"GroupId": "sg-00fcfb66ed8e45e95",
"Tags": [
{
"Key": "Name",
"Value": "AD-sg"
}
]
}
[cloudshell-user@ip-10-6-83-5 ~]$
セキュリテイグループルールの追加
以下のコマンドでセキュリテイグループルールを追加します。
aws ec2 authorize-security-group-ingress --group-id sg-xxxxxxxxxxxxxxxxx --ip-permissions '[
{"IpProtocol": "tcp", "FromPort": 53, "ToPort": 53, "IpRanges": [{"CidrIp": "xx.xx.xx.xx/xx", "Description": "DNS_TCP"}]},
{"IpProtocol": "udp", "FromPort": 53, "ToPort": 53, "IpRanges": [{"CidrIp": "xx.xx.xx.xx/xx", "Description": "DNS_UDP"}]},
{"IpProtocol": "tcp", "FromPort": 88, "ToPort": 88, "IpRanges": [{"CidrIp": "xx.xx.xx.xx/xx", "Description": "Kerberos_TCP"}]},
{"IpProtocol": "udp", "FromPort": 88, "ToPort": 88, "IpRanges": [{"CidrIp": "xx.xx.xx.xx/xx", "Description": "Kerberos_UDP"}]},
{"IpProtocol": "tcp", "FromPort": 389, "ToPort": 389, "IpRanges": [{"CidrIp": "xx.xx.xx.xx/xx", "Description": "LDAP_TCP"}]},
{"IpProtocol": "udp", "FromPort": 389, "ToPort": 389, "IpRanges": [{"CidrIp": "xx.xx.xx.xx/xx", "Description": "LDAP_UDP"}]},
{"IpProtocol": "tcp", "FromPort": 445, "ToPort": 445, "IpRanges": [{"CidrIp": "xx.xx.xx.xx/xx", "Description": "SMB/CIFS_TCP"}]},
{"IpProtocol": "udp", "FromPort": 445, "ToPort": 445, "IpRanges": [{"CidrIp": "xx.xx.xx.xx/xx", "Description": "SMB/CIFS_UDP"}]},
{"IpProtocol": "tcp", "FromPort": 464, "ToPort": 464, "IpRanges": [{"CidrIp": "xx.xx.xx.xx/xx", "Description": "Change/set_Kerberos_password_TCP"}]},
{"IpProtocol": "udp", "FromPort": 464, "ToPort": 464, "IpRanges": [{"CidrIp": "xx.xx.xx.xx/xx", "Description": "Change/set_Kerberos_password_UDP"}]},
{"IpProtocol": "tcp", "FromPort": 135, "ToPort": 135, "IpRanges": [{"CidrIp": "1xx.xx.xx.xx/xx", "Description": "Replication_TCP"}]},
{"IpProtocol": "tcp", "FromPort": 49152, "ToPort": 65535, "IpRanges": [{"CidrIp": "xx.xx.xx.xx/xx", "Description": "RPC"}]},
{"IpProtocol": "tcp", "FromPort": 3268, "ToPort": 3268, "IpRanges": [{"CidrIp": "xx.xx.xx.xx/xx", "Description": "LDAP_GC_&_LDAP_GC_SSL_TCP"}]},
{"IpProtocol": "tcp", "FromPort": 3269, "ToPort": 3269, "IpRanges": [{"CidrIp": "xx.xx.xx.xx/xx", "Description": "LDAP_GC_&_LDAP_GC_SSL_UDP"}]},
{"IpProtocol": "tcp", "FromPort": 9389, "ToPort": 9389, "IpRanges": [{"CidrIp": "xx.xx.xx.xx/xx", "Description": "SOAP"}]},
{"IpProtocol": "udp", "FromPort": 123, "ToPort": 123, "IpRanges": [{"CidrIp": "xx.xx.xx.xx/xx", "Description": "NTP_UDP"}]},
{"IpProtocol": "udp", "FromPort": 138, "ToPort": 138, "IpRanges": [{"CidrIp": "xx.xx.xx.xx/xx", "Description": "DFS_&_NetLogon"}]}
]'
--group-id sg-xxxxxxxxxxxxxxxxx
にはルールを追加するセキュリテイグループ ID を入力してください。"CidrIp": "xx.xx.xx.xx/xx"
には AD の通信を許可する送信元 CIDR 範囲を入力してください。
CloudShell での実行結果は以下のようになります。
実行結果(クリックで展開)
[cloudshell-user@ip-10-4-17-86 ~]$ aws ec2 authorize-security-group-ingress --group-id sg-043fba65d8ca11f8c --ip-permissions '[
> {"IpProtocol": "tcp", "FromPort": 53, "ToPort": 53, "IpRanges": [{"CidrIp": "10.0.10.0/23", "Description": "DNS_TCP"}]},
> {"IpProtocol": "udp", "FromPort": 53, "ToPort": 53, "IpRanges": [{"CidrIp": "10.0.10.0/23", "Description": "DNS_UDP"}]},
> {"IpProtocol": "tcp", "FromPort": 88, "ToPort": 88, "IpRanges": [{"CidrIp": "10.0.10.0/23", "Description": "Kerberos_TCP"}]},
> {"IpProtocol": "udp", "FromPort": 88, "ToPort": 88, "IpRanges": [{"CidrIp": "10.0.10.0/23", "Description": "Kerberos_UDP"}]},
> {"IpProtocol": "tcp", "FromPort": 389, "ToPort": 389, "IpRanges": [{"CidrIp": "10.0.10.0/23", "Description": "LDAP_TCP"}]},
> {"IpProtocol": "udp", "FromPort": 389, "ToPort": 389, "IpRanges": [{"CidrIp": "10.0.10.0/23", "Description": "LDAP_UDP"}]},
> {"IpProtocol": "tcp", "FromPort": 445, "ToPort": 445, "IpRanges": [{"CidrIp": "10.0.10.0/23", "Description": "SMB/CIFS_TCP"}]},
> {"IpProtocol": "udp", "FromPort": 445, "ToPort": 445, "IpRanges": [{"CidrIp": "10.0.10.0/23", "Description": "SMB/CIFS_UDP"}]},
> {"IpProtocol": "tcp", "FromPort": 464, "ToPort": 464, "IpRanges": [{"CidrIp": "10.0.10.0/23", "Description": "Change/set_Kerberos_password_TCP"}]},
> {"IpProtocol": "udp", "FromPort": 464, "ToPort": 464, "IpRanges": [{"CidrIp": "10.0.10.0/23", "Description": "Change/set_Kerberos_password_UDP"}]},
> {"IpProtocol": "tcp", "FromPort": 135, "ToPort": 135, "IpRanges": [{"CidrIp": "10.0.10.0/23", "Description": "Replication_TCP"}]},
> {"IpProtocol": "tcp", "FromPort": 49152, "ToPort": 65535, "IpRanges": [{"CidrIp": "10.0.10.0/23", "Description": "RPC"}]},
> {"IpProtocol": "tcp", "FromPort": 3268, "ToPort": 3268, "IpRanges": [{"CidrIp": "10.0.10.0/23", "Description": "LDAP_GC_&_LDAP_GC_SSL_TCP"}]},
> {"IpProtocol": "tcp", "FromPort": 3269, "ToPort": 3269, "IpRanges": [{"CidrIp": "10.0.10.0/23", "Description": "LDAP_GC_&_LDAP_GC_SSL_UDP"}]},
> {"IpProtocol": "tcp", "FromPort": 9389, "ToPort": 9389, "IpRanges": [{"CidrIp": "10.0.10.0/23", "Description": "SOAP"}]},
> {"IpProtocol": "udp", "FromPort": 123, "ToPort": 123, "IpRanges": [{"CidrIp": "10.0.10.0/23", "Description": "NTP_UDP"}]},
> {"IpProtocol": "udp", "FromPort": 138, "ToPort": 138, "IpRanges": [{"CidrIp": "10.0.10.0/23", "Description": "DFS_&_NetLogon"}]}
> ]'
{
"Return": true,
"SecurityGroupRules": [
{
"SecurityGroupRuleId": "sgr-0187b606bfc8a409f",
"GroupId": "sg-043fba65d8ca11f8c",
"GroupOwnerId": "123456789012",
"IsEgress": false,
"IpProtocol": "tcp",
"FromPort": 53,
"ToPort": 53,
"CidrIpv4": "10.0.10.0/23",
"Description": "DNS_TCP"
},
{
"SecurityGroupRuleId": "sgr-02486ec44a3465065",
"GroupId": "sg-043fba65d8ca11f8c",
"GroupOwnerId": "123456789012",
"IsEgress": false,
"IpProtocol": "udp",
"FromPort": 53,
"ToPort": 53,
"CidrIpv4": "10.0.10.0/23",
"Description": "DNS_UDP"
},
{
"SecurityGroupRuleId": "sgr-03033bef0e7837401",
"GroupId": "sg-043fba65d8ca11f8c",
"GroupOwnerId": "123456789012",
"IsEgress": false,
"IpProtocol": "tcp",
"FromPort": 88,
"ToPort": 88,
"CidrIpv4": "10.0.10.0/23",
"Description": "Kerberos_TCP"
},
{
"SecurityGroupRuleId": "sgr-031e077b19fb0e1ed",
"GroupId": "sg-043fba65d8ca11f8c",
"GroupOwnerId": "123456789012",
"IsEgress": false,
"IpProtocol": "udp",
"FromPort": 88,
"ToPort": 88,
"CidrIpv4": "10.0.10.0/23",
"Description": "Kerberos_UDP"
},
{
"SecurityGroupRuleId": "sgr-05e4dc7c71709b282",
"GroupId": "sg-043fba65d8ca11f8c",
"GroupOwnerId": "123456789012",
"IsEgress": false,
"IpProtocol": "tcp",
"FromPort": 389,
"ToPort": 389,
"CidrIpv4": "10.0.10.0/23",
"Description": "LDAP_TCP"
},
{
"SecurityGroupRuleId": "sgr-067002d34bfa8f4c4",
"GroupId": "sg-043fba65d8ca11f8c",
"GroupOwnerId": "123456789012",
"IsEgress": false,
"IpProtocol": "udp",
"FromPort": 389,
"ToPort": 389,
"CidrIpv4": "10.0.10.0/23",
"Description": "LDAP_UDP"
},
{
"SecurityGroupRuleId": "sgr-06a7559ca1a7e3bd6",
"GroupId": "sg-043fba65d8ca11f8c",
"GroupOwnerId": "123456789012",
"IsEgress": false,
"IpProtocol": "tcp",
"FromPort": 445,
"ToPort": 445,
"CidrIpv4": "10.0.10.0/23",
"Description": "SMB/CIFS_TCP"
},
{
"SecurityGroupRuleId": "sgr-0702e98870a2c821e",
"GroupId": "sg-043fba65d8ca11f8c",
"GroupOwnerId": "123456789012",
"IsEgress": false,
"IpProtocol": "udp",
"FromPort": 445,
"ToPort": 445,
"CidrIpv4": "10.0.10.0/23",
"Description": "SMB/CIFS_UDP"
},
{
"SecurityGroupRuleId": "sgr-07060c03cda37d94a",
"GroupId": "sg-043fba65d8ca11f8c",
"GroupOwnerId": "123456789012",
"IsEgress": false,
"IpProtocol": "tcp",
"FromPort": 464,
"ToPort": 464,
"CidrIpv4": "10.0.10.0/23",
"Description": "Change/set_Kerberos_password_TCP"
},
{
"SecurityGroupRuleId": "sgr-07371f64d983ce835",
"GroupId": "sg-043fba65d8ca11f8c",
"GroupOwnerId": "123456789012",
"IsEgress": false,
"IpProtocol": "udp",
"FromPort": 464,
"ToPort": 464,
"CidrIpv4": "10.0.10.0/23",
"Description": "Change/set_Kerberos_password_UDP"
},
{
"SecurityGroupRuleId": "sgr-08fa65552b41ce39b",
"GroupId": "sg-043fba65d8ca11f8c",
"GroupOwnerId": "123456789012",
"IsEgress": false,
"IpProtocol": "tcp",
"FromPort": 135,
"ToPort": 135,
"CidrIpv4": "10.0.10.0/23",
"Description": "Replication_TCP"
},
{
"SecurityGroupRuleId": "sgr-0b83e515913636168",
"GroupId": "sg-043fba65d8ca11f8c",
"GroupOwnerId": "123456789012",
"IsEgress": false,
"IpProtocol": "tcp",
"FromPort": 49152,
"ToPort": 65535,
"CidrIpv4": "10.0.10.0/23",
"Description": "RPC"
},
{
"SecurityGroupRuleId": "sgr-0b8b5f9545ae21909",
"GroupId": "sg-043fba65d8ca11f8c",
"GroupOwnerId": "123456789012",
"IsEgress": false,
"IpProtocol": "tcp",
"FromPort": 3268,
"ToPort": 3268,
"CidrIpv4": "10.0.10.0/23",
"Description": "LDAP_GC_&_LDAP_GC_SSL_TCP"
},
{
"SecurityGroupRuleId": "sgr-0d5f8dfc9c939458b",
"GroupId": "sg-043fba65d8ca11f8c",
"GroupOwnerId": "123456789012",
"IsEgress": false,
"IpProtocol": "tcp",
"FromPort": 3269,
"ToPort": 3269,
"CidrIpv4": "10.0.10.0/23",
"Description": "LDAP_GC_&_LDAP_GC_SSL_UDP"
},
{
"SecurityGroupRuleId": "sgr-0df39c1c6d289874e",
"GroupId": "sg-043fba65d8ca11f8c",
"GroupOwnerId": "123456789012",
"IsEgress": false,
"IpProtocol": "tcp",
"FromPort": 9389,
"ToPort": 9389,
"CidrIpv4": "10.0.10.0/23",
"Description": "SOAP"
},
{
"SecurityGroupRuleId": "sgr-0eae49739905208e8",
"GroupId": "sg-043fba65d8ca11f8c",
"GroupOwnerId": "123456789012",
"IsEgress": false,
"IpProtocol": "udp",
"FromPort": 123,
"ToPort": 123,
"CidrIpv4": "10.0.10.0/23",
"Description": "NTP_UDP"
},
{
"SecurityGroupRuleId": "sgr-0fd60f12fa70636c5",
"GroupId": "sg-043fba65d8ca11f8c",
"GroupOwnerId": "123456789012",
"IsEgress": false,
"IpProtocol": "udp",
"FromPort": 138,
"ToPort": 138,
"CidrIpv4": "10.0.10.0/23",
"Description": "DFS_&_NetLogon"
}
]
}
[cloudshell-user@ip-10-4-17-86 ~]$
詳細
AD の通信を許可するには、以下のインバウンドルール許可が必要になります。
プロトコル | ポート範囲 | トラフィックの種類 | Active Directory の使用 |
---|---|---|---|
TCP と UDP | 53 | DNS | ユーザーとコンピュータの認証、名前解決、信頼 |
TCP と UDP | 88 | Kerberos | ユーザーとコンピュータの認証、フォレストレベルの信頼 |
TCP と UDP | 389 | LDAP | ディレクトリ、レプリケーション、ユーザーとコンピュータの認証、グループポリシー、信頼 |
TCP と UDP | 445 | SMB/CIFS | レプリケーション、ユーザーとコンピュータの認証、グループポリシー、信頼 |
TCP と UDP | 464 | Kerberos | パスワードの変更/設定 レプリケーション、ユーザーとコンピュータの認証、信頼 |
TCP | 135 | レプリケーション | RPC、EPM |
TCP | 636 | LDAP SSL | ディレクトリ、レプリケーション、ユーザーとコンピュータの認証、グループポリシー、信頼 |
TCP | 49152-65535 | RPC | レプリケーション、ユーザーとコンピュータの認証、グループポリシー、信頼 |
TCP | 3268-3269 | LDAP GC および LDAP GC SSL | ディレクトリ、レプリケーション、ユーザーとコンピュータの認証、グループポリシー、信頼 |
TCP | 9389 | SOAP | AD DS ウェブサービス |
UDP | 123 | Windows タイム | Windows タイム、信頼 |
UDP | 138 | DFSN と NetLogon | DFS、グループポリシー |
※ TCP 636 は、SSL 経由で LDAP を使用している場合にのみ必要となります。本記事上部に記載したコマンドには含んでおりません。
今回は以下のような AD on EC2(Windows Server 2022 Datacenter Edition)で検証するため、EC2 に付与するセキュリテイグループのルールを作成しました。
AWS ドキュメント AWS Managed Microsoft AD のネットワークセキュリティ設定を強化する を見ると様々なケースが記載されております。
今回は AWS 環境 プライベートサブネット内に閉じた検証なので、AD ドメインコントローラーが存在するサブネットとドメインに参加させるメンバーサーバーが存在するサブネットの中で通信が許可されていればよいです。ですので
- Private subnet1 10.0.10.0/24(10.0.10.0 ~ 10.0.10.255)
- Private subnet2 10.0.11.0/24(10.0.11.0 ~ 10.0.11.255)
を共に含む CIDR
- 10.0.10.0/23(10.0.10.0 ~ 10.0.11.255)
を送信元として設定してコマンドを実行しました。(実行結果参照)
こうすることで、AD ドメインコントローラーが存在するサブネットとドメインに参加させるメンバーサーバーが存在するサブネットの中で AD ドメイン関連の通信が自由に行われるようになります。
以下のような場合は、オンプレミスの CIDR 範囲を送信元としたルールも追加します。
- オンプレミス上にドメイン参加させたい PC がある
- オンプレミス上の AD ドメインコントローラーと AWS 上の AD ドメインコントローラーでレプリケーションしたい
- オンプレミス上の AD ドメインと AWS 上の AD ドメイン間で信頼関係を構築したい
ルール数が 2 倍に増えますが、こうすることで、オンプレミス上のノードとも AD ドメイン関連の通信が自由に行われるようになります。
経緯
検証なので最初はマネジメントコンソールでポチポチしていたのですが、ルール数が多く、途中で割り込み作業が入ったり間違えてリロードしてしまった際に今まで入力していたルールが全部真っ白に戻ってしまって悲しくなりました。
また、ルールに説明(Description)を記載して何の通信を許可しているのかわかりやすくしたかったのですが、CloudFormation で作成しようとするとルールを 1 行ずつ記載する必要があり面倒でした。
所感
ドメイン環境内でさらにファイルの授受が必要であったり、リモートデスクトップ接続が必要であったりする機会は多いと思います。
今回セキュリテイグループルールを追加する AWS CLI コマンドを作成してはみたものの、必要なルールが増えるたびに対象のセキュリティグループにルールを追加するのは手間ですし、作業が終わったら削除するか?など検討し始めるとさらに運用が手間です。
また、セキュリテイグループのルール数は 60 ルールまでという制約があります。今回のコマンドだけでも 17 ルールが追加され、オンプレミスとの通信も許可すると倍の 34 ルールになります。ルールを追加するセグメントが増えるとどんどんルールが増えてしまい、管理が大変です。
VPN や専用線で接続している閉域内では基本的に通信は全許可しておくのが分かりやすくてよいかもしれません。