ファイヤーウォールとはパケットの制御または破棄を行うソフトウェアである
特定のパケットとは、
– 特定のポートにアクセスするパケット
– 特定のIPアドレスからのパケット
– 特定の内容をもつパケット
などがある。
ファイヤーウォールを作成するには
– パケットの中身を見る
– パケットの転送可否を行う
### パケットキャプチャ
パケットの中身を見る
TCPやUDPはもとより、IPやEthernetレベルのデータリンク情報も解析する
一般的に tcpdump や wireshark などがある
#include <stdio.h>
#include <net/if.h> // ネットワークインターフェイス構造体 PF_PACKET
#include <net/ethernet.h> // ETH_P_ALL
void analyzePacket(u_char *buf){
printEtherHeader(buf);
}
void printEtherHeader(u_char *buf){
struct ether_header *eth;
eth = (struct ether_header *)buf;
printf("Dst MAC addr :%17s\n", mac_ntoa(eth->ether_dhost));
printf("Src MAC addr :%17s\n", mac_ntoa(eth->ether_shost));
printf("Ethernet Type :0x%04x\n", ntohs(ether_type));
}
char *mac_ntoa(u_char *d){
static char str[18];
sprintf(str,"%02x:%02x:%02x:%02x:%02x:%02x",d[0],d[1],d[2],d[3],d[4],d[5]);
return str;
}
int main(int argc, char** argv[]){
int soc;
u_char buf[65535]; // u_charはunsigned char
soc = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
while(1){
read(soc,buf,sizeof(buf));
analyzePacket(buf);
}
return 0;
}
int socket(int domain, int type, int protocol);
PF_PACKETを指定すると,データリンク層やネットワーク層の生データを扱える
通常はSOCK_STREAM(TCPの場合)やSOCK_DGRAM(UDPの場合)
ETHERNETとは、TCP/IPプロトコルのネットワークインターフェース層に対応する有線の規格
宛先MAC address(固有識別番号)、送信元MAC address, type, ip以降
ビッグエンディアン