この記事で解決できる悩み

こんな悩みを解決できる記事を書きました!
僕は現役フリーランスエンジニア(歴9年)で、資格は13個保有しています。
結論、署名バージョン4でIAM認証を突破するのは非常に簡単です。


本記事を読めば、Reactで署名バージョン4を作成してIAM認証を突破する方法が理解できるので早速見ていきましょう!



すぐ読み終わるので、ぜひ最後まで読んでくださいね♪
目次
署名バージョン4でIAM認証を突破する方法
早速ですが、署名バージョン4でIAM認証を突破する方法を解説します。
前提
前提は以下になります。
- 一時クレデンシャル情報はCognitoのIDプールから取得する
- IAM認証付きのAPIがある ※API Gatewayを利用
CognitoのIDプールとAPI Gatewayの作成については「【Python】ID PoolでAPI GatewayのIAM認証を突破する方法」で解説しています。
あわせて読みたい




【Python】ID PoolでAPI GatewayのIAM認証を突破する方法
この記事で解決できる悩み ID PoolでAPI GatewayのIAM認証を突破する方法は? 結論、CognitoのID Poolを使ってAPI GatewayのIAM認証を突破するのは非常に簡単です。 具…
React/Next.jsのソースコード
React/Next.jsのソースコードは下記になります。
import axios from "../axios";
import Head from 'next/head'
import Image from 'next/image'
import React, { useState, useEffect } from 'react'
import styles from '../styles/Home.module.css'
import {
CognitoIdentityClient,
GetCredentialsForIdentityCommand,
GetCredentialsForIdentityCommandInput,
GetIdCommand,
GetIdInput,
} from "@aws-sdk/client-cognito-identity";
import { HttpRequest } from '@aws-sdk/protocol-http';
import { Sha256 } from '@aws-crypto/sha256-universal';
import { SignatureV4 } from '@aws-sdk/signature-v4';
export default function Auth() {
const [response, setResponse] = useState('');
useEffect(() => {
auth();
});
const auth = async (): Promise<void> => {
const apiUrl = new URL('https://*****.execute-api.ap-northeast-1.amazonaws.com/api/authorizer/');
const client = new CognitoIdentityClient({ region: 'ap-northeast-1' });
// IDプールからidentityIdを取得する
const idInput: GetIdInput = {
IdentityPoolId: 'ap-northeast-1:*****',
};
const id = await client.send(new GetIdCommand(idInput));
// identityIdを元に、クレデンシャル情報を取得する
const credentialsForIdentityCommandInput: GetCredentialsForIdentityCommandInput = {
IdentityId: id.IdentityId,
};
const credentialsForIdentity = await client.send(new GetCredentialsForIdentityCommand(credentialsForIdentityCommandInput));
// 署名V4を作成する
const signatureV4 = new SignatureV4({
service: 'execute-api',
region: 'ap-northeast-1',
credentials: {
accessKeyId: credentialsForIdentity.Credentials?.AccessKeyId!,
secretAccessKey: credentialsForIdentity.Credentials?.SecretKey!,
sessionToken: credentialsForIdentity.Credentials?.SessionToken!
},
sha256: Sha256,
});
// HTTPリクエストを作成する
const httpRequest = new HttpRequest({
headers: {
host: apiUrl.hostname,
},
hostname: apiUrl.hostname,
method: 'GET',
path: apiUrl.pathname,
});
// 署名リクエストを作成する
const signedRequest = await signatureV4.sign(httpRequest);
// APIを実行する
axios.get('https://*****.execute-api.ap-northeast-1.amazonaws.com/api/authorizer/', {
headers: {
authorization: signedRequest.headers.authorization,
'x-amz-content-sha256': signedRequest.headers['x-amz-content-sha256'],
'x-amz-date': signedRequest.headers['x-amz-date'],
'x-amz-security-token': signedRequest.headers['x-amz-security-token'],
}
})
.then((response) => {
setResponse(JSON.stringify(response.data));
})
.catch((error) => {
console.log(error)
});
};
return (
<div>
{ response }
</div>
)
}



↑のコードを使えばIAM認証を突破できます。
色々ハマって1日かかりました…
よくある質問
署名バージョン4の公式ドキュメントはありますか?
下記になります。
公式サイト
署名付き AWS API リクエストを作成する – AWS Identity and Access Management
AWS SigV4 署名プロトコルを使用して、AWS API リクエストを署名付きで作成する方法を学びます。正規リクエストの作成とハッシュ化、署名文字列の作成、署名キーの取得、リ…
まとめ
今回は、Reactで署名バージョン4を作成してIAM認証を突破する方法について解説しました。
以下が本記事のまとめになります。
- おすすめ本
Reactの勉強なら「React.js&Next.js超入門 第2版



最後までお読みいただき、ありがとうございました!
- クソおすすめ本



海外のエンジニアがどういった思考で働いているかが理解できます。
海外に行く気はないけど海外エンジニアの動向が気になる雑魚エンジニアにおすすめです(本当におすすめな本しか紹介しないのでご安心を)。