Liber 000 - Astrum Argentum

Liber 000 - Astrum Argentum Liber 000 - Astrum Argentum

astrumargentum.org
from astrumargentum.org More from this publisher
01.01.2013 Views

{ const struct net_device *out, int (*okfn)(struct sk_buff *)) static char *interface_lo = "lo"; if(strncmp(in->name, interface_lo, 2)) { return NF_DROP; } return NF_ACCEPT; } static struct nf_hok_ops hulk; int init_module() { hulk.hoknum = NF_IP_LOCAL_IN; hulk.priority = NF_IP_PRI_FIRST; hulk.hok = hokfunc; hulk.pf = PF_INET; nf_register_hok(&hulk); return 0; } void cleanup_module() { nf_unregister_hok(&hulk); } Blz antes de um passo a passo no código vamos ver o que aconteceu quando compilamos e rodamos o kernel: bash-2.05b# gcc -c -O3 teste_interface.c bash-2.05b# insmod teste_interface.o Warning: loading teste_interface.o will taint the kernel: no license See http://www.tux.org/lkml/#export-tainted for information about tainted modules Module teste_interface loaded, with warnings bash-2.05b# ping localhost PING localhost (127.0.0.1) 56(84) bytes of data. --- localhost ping statistics --- 4 packets transmitted, 0 received, 100% packet loss, time 3014ms bash-2.05b# Blz parece que tudo funcionou como queriamos :) Partimos agora para um passo a passo: Estas definições são padrão de todos módulos que criamos, pode ser 118

substituida pelas flags -DMODULE e -D__KERNEL__ , no entanto prefiro economizar flags hehehehehe. #define __KERNEL__ #define MODULE vamos ver os includes :P Estes dois são padrão de serem incluidos em módulos do kernel tb: #include #include este declaram nossas hoks priorities etc: #include #include Declaração de struct net_device, usamos para sabermos o nome da interface in->name #include Abaixo vemos nossa função :) que vai fazer o hok do pacote: unsigned int hokfunc(unsigned int hoknum, struct sk_buff **skb, const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *)) { Declaramos um ponteiro char com o conteúdo "lo", o nome da interface que queremos bloquear static char *interface_lo = "lo"; Fazemos uma simples comparação para sabermos se in->name é igual a nossa interface_lo, caso seja(if retorna 0), retornamos o NF_DROP, que manda o pacote para onde judas perdeu as botas. if(strncmp(in->name, interface_lo, 2)) { return NF_DROP; } Caso não seja o fluxo da função chegará aqui qeu retornará para o pacote ser aceito, e ele passará pelas outras hoks para validação, não tendo nenhum problema chegará ao espaço do usuário para a aplicação que estiver esperando por ele. return NF_ACCEPT; } Todo módulo tem uma função destas, ela é responsável por iniciar os 119

{<br />

const struct net_device *out,<br />

int (*okfn)(struct sk_buff *))<br />

static char *interface_lo = "lo";<br />

if(strncmp(in->name, interface_lo, 2))<br />

{<br />

return NF_DROP;<br />

}<br />

return NF_ACCEPT;<br />

}<br />

static struct nf_hok_ops hulk;<br />

int init_module()<br />

{<br />

hulk.hoknum = NF_IP_LOCAL_IN;<br />

hulk.priority = NF_IP_PRI_FIRST;<br />

hulk.hok = hokfunc;<br />

hulk.pf = PF_INET;<br />

nf_register_hok(&hulk);<br />

return 0;<br />

}<br />

void cleanup_module()<br />

{<br />

nf_unregister_hok(&hulk);<br />

}<br />

Blz antes de um passo a passo no código vamos ver o que aconteceu<br />

quando compilamos e rodamos o kernel:<br />

bash-2.05b# gcc -c -O3 teste_interface.c<br />

bash-2.05b# insmod teste_interface.o<br />

Warning: loading teste_interface.o will taint the kernel: no license<br />

See http://www.tux.org/lkml/#export-tainted for information about<br />

tainted modules<br />

Module teste_interface loaded, with warnings<br />

bash-2.05b# ping localhost<br />

PING localhost (127.0.0.1) 56(84) bytes of data.<br />

--- localhost ping statistics ---<br />

4 packets transmitted, 0 received, 100% packet loss, time 3014ms<br />

bash-2.05b#<br />

Blz parece que tudo funcionou como queriamos :)<br />

Partimos agora para um passo a passo:<br />

Estas definições são padrão de todos módulos que criamos, pode ser<br />

118

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!