[ hiroc13 @ 25.09.2006. 13:34 ] @
Dali je moguce u pythonu?

Imam neki software koji mi šalje neke podatke na zadani IP, koristi protokol UDP i port 50000,
dali mogu nekako snimiti šta se točno šalje, i
napraviti svoj program s kojim ću slati iste te podatke na neki drugi IP.
[ Au197/79 @ 25.09.2006. 18:16 ] @
Nabavi python knjge, mnoge se vrte po netu. Evo odgovora iz Python Cookbooka:
za serverski deo (za koji nisam siguran da ti treba):
Code:
import socket

port = 8081
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Accept UDP datagrams, on the given port, from any sender
s.bind(("", port))
print "waiting on port:", port

while True:
    # Receive up to 1,024 bytes in a datagram
    data, addr = s.recvfrom(1024)
    print "Received:", data, "from", addr

i klijentski:
Code:
import socket

port = 8081
host = "localhost"

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto("Holy Guido! It's working.", (host, port))

Za čas posla u Pythonu :)
[ hiroc13 @ 26.09.2006. 07:40 ] @
Zahvaljujem na ovome, ali s čime
pročitati šta mi trenutni program šalje na IP
[ bzero @ 26.09.2006. 08:29 ] @
Pogledaj:

http://www.ethereal.com/
[ Bouncer @ 02.10.2006. 12:33 ] @
Takodjer primjer iz cooobooka

A simple beta-tool that fills the DATA field of an address packet with strings, then send the packet(s) to an host specified.
You can observe the strings received by putting an ICMP sniffer on the remote machines.

Code:

#!/usr/bin/python
import time
import sys
from impacket import ImpactPacket
from socket import *


if len(sys.argv) < 3:
    print """"Usage: <source IP> <dest IP> "data" """
    sys.exit(1)
    
src = sys.argv[1]
dst = sys.argv[2]
str = sys.argv[3]

# define RAW socket
s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)
s.setsockopt(IPPROTO_IP, IP_HDRINCL, 1)

# define IP packet
ip = ImpactPacket.IP()
ip.set_ip_src(src)
ip.set_ip_dst(dst)

# define ICMP packet
icmp = ImpactPacket.ICMP()
icmp.set_icmp_type(icmp.ICMP_ECHOREPLY) #ICMP packet type

# fragmentation for DATA fileds > of 54 bytes
x = len(str) / 54                                 
y = len(str) % 54                                 

seq_id = 0                                        
for i in range(1,x+2):                             
    str_send = str[54*(i-1): 54*i]                 
    icmp.contains(ImpactPacket.Data(str_send)) # fill ICMP DATA field
    ip.contains(icmp) # encapsulate ICMP packet in the IP packet     
    seq_id = seq_id + 1                             
    icmp.set_icmp_id(seq_id)                     
    icmp.set_icmp_cksum(0)                         
    icmp.auto_checksum = 1                         
    s.sendto(ip.get_packet(), (dst, 0)) # send packet         
    time.sleep(1)                                 
# eventual rest of the string 
str_send = str[54*i:54*i+ y]
icmp.contains(ImpactPacket.Data(str_send))
ip.contains(icmp)
seq_id = seq_id + 1
icmp.set_icmp_id(seq_id)
icmp.set_icmp_cksum(0)
icmp.auto_checksum = 1
s.sendto(ip.get_packet(), (dst, 0))
time.sleep(1)


Mislim da s gore navedenim clientom sta je Au197/79 napisa mozes slat upute samo ne gore isti primjer servera koji ce bit aktivan na remote mashini....
A stranica od impacket modula je down tako da ako mozes kod nekog to nac ovaj primjer ce ti bit od koristi...

Btw ima li ko taj modul za poslat na mail ?

[Ovu poruku je menjao Bouncer dana 04.10.2006. u 12:53 GMT+1]
[ hiroc13 @ 04.10.2006. 09:20 ] @
Ovo je sve ok, zahvaljujem na codovima, ali imam još jedno pitanje!

Ja bih sad ovdje umjesto abc trebao poslati hex: "10 06 00 0f 02 01 29 bc 11 02 00 02 d1 00 81"
s.send("abc") # send test string

može objašnjenje kako to izvesti!
[ Bouncer @ 04.10.2006. 11:52 ] @
http://docs.python.org/lib/module-struct.html

Primjeri... :

>>> GEN_STREAMTYPE_NULL = 0x51
>>> def send4bytes(num):
... mysocket.send(struct.pack("L", num))
...
>>> send4bytes(GEN_STREAMTYPE_NULL)


>>> hex(0x1234 & 0xFF)
'0x34'
>>> hex(0x34 << 8)
'0x3400'