Introduction
sngrep is an invaluable tool for analyzing SIP (Session Initiation Protocol) traffic. This comprehensive guide will walk you through basic to advanced techniques, helping you harness the full power of sngrep for troubleshooting and analysis.
1. Basic Usage
Starting sngrep
To start sngrep and capture SIP traffic:
sngrep
This will capture traffic on the default interface.
Capturing on a Specific Interface
To capture on a specific network interface:
sngrep -d eth0
Replace eth0 with your desired interface name.
Reading from a PCAP File
To analyze a previously captured PCAP file:
sngrep -I capture.pcap
2. Filtering Options
Basic Filtering
Filter by IP address:
sngrep host 192.168.1.100
Filter by port:
sngrep port 5060
Advanced Filtering with Regular Expressions
Use the -f option for complex filters:
sngrep -f 'METHOD INVITE'
sngrep -f 'ip 192\.168\.1\.100'
sngrep -f 'METHOD INVITE && ip 192\.168\.1\.100'
Combining Multiple Filters
You can combine multiple filters:
sngrep host 192.168.1.100 and port 5060
3. Interface Navigation and Customization
F1: Help menuq: Quit the current screen or sngrepc: Clear the current sessionC: Customize columns in the call list view<and>: Adjust column widthsV: Toggle between different viewsF: Switch between SIP message flow and RTP stream analysisS: View detailed statistics for each RTP streamW: Save the current session to a PCAP file
4. Advanced Call Flow Analysis
Displaying Only Matched Dialogs
sngrep -d eth0 -O
Saving Call Flow Diagrams
To save a call flow diagram as an image:
- Select a call in the main interface
- Press
F2 - Choose the image format (ASCII or SVG)
Analyzing RTP Streams
- Select a call in the main interface
- Press
F - Use arrow keys to navigate between streams
- Press
Sfor detailed statistics
5. Packet Capture and Playback
Continuous Capture to File
To capture traffic continuously to a file:
sngrep -d eth0 -O capture.pcap
Reading Multiple PCAP Files
sngrep -I file1.pcap -I file2.pcap
Saving Current Session
Press W in the main interface to save the current session to a PCAP file.
6. Remote Monitoring
SSH Tunneling
To analyze traffic on a remote host:
ssh -C user@remote_host sngrep | sngrep -I -
Monitoring Multiple Interfaces
sngrep -d eth0 -d eth1
7. Scripting with sngrep
Extracting Specific SIP Headers
sngrep -I capture.pcap -O '%ua %to %from'
Counting Calls
sngrep -I capture.pcap -c | grep "INVITE" | wc -l
Generating Call Statistics
sngrep -I capture.pcap -s
8. Performance Tuning
Limiting Displayed Calls
sngrep -l 1000
This limits the display to 1000 calls.
Setting Maximum Memory Usage
sngrep -m 1024
This sets the maximum memory usage to 1024 MB.
9. Integration with Other Tools
Piping to awk for Custom Formatting
sngrep -I capture.pcap -O '%rs %rd' | awk '{print $2 " -> " $4}'
Using with tshark for Detailed Analysis
tshark -r capture.pcap -Y sip | sngrep -I -
Conclusion
This guide covers a wide range of sngrep functionalities, from basic usage to advanced techniques. By mastering these commands and options, you’ll be well-equipped to handle complex SIP traffic analysis scenarios. Remember to explore the built-in help (F1 key) for more details on each feature.