Package: accompanist
🎻 IntroductionWhat is Accomapnist?Accompanist is an AWS WAF log analysis tool and an "accompanist" of WAF operation.It enables you to analyze AWS WAF logs and automatically generate instant analysis reports in just three simple steps.If you're operating AWS WAF, this tool can be helpful for instant log analysis and help you understand the trends of blocked or counted accesses.Reporting ItemsHistgram of requestsTotal numberNumber by each WAF rule providerTop 5 for the following categories:Rule groupRulePaths nameUser agentIP addresswsCountry codesHit count on specific pathsCommentAn optional field where you can write additional notesPrerequisiteRequirementWAF: AWS WAFv2 and/or Third Party WAF (using WebACL)Logging Destination: CloudWatch Logs or S3Support Action: BLOCK, COUNT or EXCLUDED_AS_COUNTIAM Role/User policyExample policies are noted in Appnedix, the last section of this page.🚀 Get StartedInstall$pipinstallaccompanistUsageFirst, configure the settings using the following command; this will generate aconfig.jsonfile.$accompanistinitYou must set the log group name using this command, or you can choose to edit theconfig.jsonfile directly.Second, retrieve the query results with the following commnad.For example, to retrieve theBLOCKlog for the past three days$accompanistlisten-aBLOCK-d3If the log destination is S3, you can use "hear" instead of the "listen".Finally, generate a report in PDF format.$accompanistplayUninstall$pipuninstallaccompanist🌱 How to useConvenient usageThe "listen" or "hear" with--json_logor-joption can be help to investigate the detail of WAF logs.[Important] Please handle it carefully as it may include sensitive or confidential information.If you want to confirm a specific access of log, you should re-run "play" with the option and open a json file generated in the current directory.The report color is customizable for when you run "play" with the option,--coloror-d.PrecautionThis tool works with AWS WAFv2 and does not support WAFv1.For AWS Managed Rules, you need to append "AWS" to the prefix, or the histogram works wrong.In COUNT action, as it is a well-known general mode of AWS WAF, COUNT or EXCLUDED_AS_COUNT is used.Using the "COUNT" action, the results are double counted by each rule.The logs may not be correctly calculated if you use the "Label" features in WAF.If the log destination is S3, it will take some time to acquire the log (several minutes)The analysis target period (days) can be set up to 40 days at most.🔨 SubcommnadsinitConfigure a CWL log group setting and more.Usage:accompanistinit# interactive inputting processOptions:-l,--log-groupTEXTSetaCloudWatchLogsLoggroupname.[required]-p,--pathTEXTSetaURIpathforcountsthatisblocked/counted.[required]-c,--commentTEXTSetacommentforreport.[required]-h,--helpShowthismessageandexit.listenGet a WAF log file in CSV format.Usage:accompanistlisten[OPTIONS]Options:-a,--action[BLOCK|COUNT]ChoseanactiontypeofAWSWAF.Thedefaultis"BLOCK".[required]-d,--daysINTEGERSetanumberofthepastdaysuntiltodayforanalysistargetperiod.-s,--start_timeINTEGERSetaUNIXtimeoftheoldesttimeforanalysistargetperiod(insteadof"--days").-e,--end_timeINTEGERSetaUNIXtimeofthelatesttimeforanalysistargetperiod(insteadof"--days").-j,--json-logOutputaJSONlogfile.Pleasehandleitwithcare.fordebugging.-h,--helpShowthismessageandexit.hearNote: This is experimental feature.Get a WAF log file in CSV format from S3.Usage:accompanisthear[OPTIONS]Options:-a,--action[BLOCK|COUNT]ChoseanactiontypeofAWSWAF.Thedefaultis"BLOCK".[required]-d,--daysINTEGERSetanumberofthepastdaysuntiltodayforanalysistargetperiod.-s,--start_timeINTEGERSetaUNIXtimeoftheoldesttimeforanalysistargetperiod(insteadof"--days").-e,--end_timeINTEGERSetaUNIXtimeofthelatesttimeforanalysistargetperiod(insteadof"--days").-h,--helpShowthismessageandexit.playAnalysis WAF logs and generate a report.Usage:accompanistplay[OPTIONS]Options:-c,--colorfulSetarandomcolorofreporttheme(insteadofcolor).-d,--colorTEXTCustomizeacolorofreportthemewithcolorcode,(e.g.)#cccccc.-m,--mask-ipMaskIPaddressesonpiechart.-u,--utc-offsetINTEGERSetanumberofUTCoffest.ThedefautoffsetisUTC+9(Asia/Tokyo).-y,--y-limit[50|100|500|1000]AdjustaY-axismaxlimitationforhistogramsduetomanyrequests.-h,--helpShowthismessageandexit.📚 AppendixIAM Policy for CWLHere is an example of IAM policy with restricted permissions.{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"logs:StartQuery","Resource":["arn:aws:logs:<region>:<aws-acount>:log-group:<log-group-name>:*"]},{"Effect":"Allow","Action":"logs:GetQueryResults","Resource":"*"}]}Note: The<region>,<aws-acount>and<log-group-name>in this sample policy should be replaced.IAM Policy for S3Here is an example of IAM policy with restricted permissions.{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:ListBucket","Resource":"arn:aws:s3:::<waf-log-bucket>"},{"Effect":"Allow","Action":"s3:GetObject","Resource":"arn:aws:s3:::<waf-log-bucket>/*"}]}Note: The<waf-log-bucket>in this sample policy should be replaced.LicenseThe MIT LicenseCopyright 2023 Itsuki YutakaPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.