■ Semgrep에 대한 개념
- Semgrep은 소스 코드 분석 도구로, 패턴 매칭 기술을 이용하여 보안 취약점, 버그 등의 코드 문제를 찾아내는데 도움을 주는 도구임
- Semgrep은 Abstract Syntax Tree(AST) 기술을 기반으로 한 소스 코드 분석 도구임
( AST는 프로그래밍 언어의 코드를 구조화된 형태로 나타낸 것임 ) - 즉, semgrep은 오픈소스 정적도구임
■ Semgrep의 장점
- 쉬운 설정 : Semgrep은 단일 YAML 파일로 구성하여 단순하고 쉽게 사용할 수 있음
- 작업 효율성 : Semgrep은 기존 코드에서 이전에 발견되지 않은 문제를 빠르게 식별할 수 있음
- 다양한 언어 지원 : Semgrep은 다양한 프로그래밍 언어를 지원함
- 자동화 : Semgrep을 이용하면, 코드 검사를 자동화하여 빠르게 문제를 식별할 수 있음
- 안정성 : Semgrep은 코드 문제를 식별할 때 false positive(=오탐)를 최소화하여, 개발자가 정확한 문제를 식별할 수 있도록 도와줌
■ Semgrep의 CLI 설치 방법
1. pip로 semgrep CLI 도구설치
pip install semgrep
2. pip 설치 확인
semgrep --version
3. codebase에 있는 코드 점검
semgrep --config config.yaml my_codebase/
■ Semgrep의 CLI와 APP 버전 차이점
| 특징 | Semgrep App | Semgrep CLI |
| Scan on a schedule | ✔️ | ✔️ |
| Scan with custom rules and rulesets | ✔️ | ✔️ |
| Scan mainline (trunk) and non-mainline branches | ✔️ | ✔️ |
| Trigger scans when a Pull request (PR) or merge request (MR) is created | ✔️ | ✔️ |
| Prevent vulnerable code from merging into mainline branches | ✔️ | ✔️ |
| Generate CI configuration files for many providers | ✔️ | ❌ |
| Receive PR or MR review comments in your source code management (SCM) tool | ✔️ | ❌ |
| Manage false positives in bulk through triage | ✔️ | ❌ |
| Receive notifications in Slack and email | ✔️ | ❌ |
| Pricing | Free for up to 20 develop | Free |
■ Semgrep에 Rule 예시
| Ban dangerous APIs | Prevent use of exec |
| Search routes and authentication | Extract Spring routes |
| Enforce the use secure defaults | Securely set Flask cookies |
| Tainted data flowing into sinks | ExpressJS dataflow into sandbox.run |
| Enforce project best-practices | Use assertEqual for == checks, Always check subprocess calls |
| Codify project-specific knowledge | Verify transactions before making them |
| Audit security hotspots | Finding XSS in Apache Airflow, Hardcoded credentials |
| Audit configuration files | Find S3 ARN uses |
| Migrate from deprecated APIs | DES is deprecated, Deprecated Flask APIs, Deprecated Bokeh APIs |
| Apply automatic fixes | Use listenAndServeTLS |
