Send scapy packets through raw sockets in python
<p>Is it possible? If yes? How?</p>

<p>This is my script (it doesn't work):</p>

<pre><code>from scapy.all import *
import socket

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    p=IP(dst="192.168.1.254")/TCP(flags="S", sport=RandShort(), dport=80)
    s.connect(("192.168.1.254",80))
    s.send(p)
    print ("Request sent!")
except:
    print ("An error occurred.")
</code></pre>

<p><strong>--UPDATE--</strong></p>

<pre><code>p = bytes(IP(dst="DESTINATIONIP")/TCP(flags="S", sport=RandShort(), dport=80))
    while True:
        try:
            socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, sockip, sockport, True)
            s = socks.socksocket()
            s.connect((DESTINATIONIP,DESTINATIONPORT))
            s.send(p)
            print ("Request Sent!")
        except:
            print ("An error occurred.")
</code></pre>

<p>is it possible to send this syn packet but through an http proxy instead of socks?</p>

<p>To send a scapy packet using raw sockets you have to convert your packet to raw bytes first. For example a packet crafted using scapy like this:</p>

<pre><code>p = IP(dst="192.168.1.254")/TCP(flags="S", sport=RandShort(),dport=80)
</code></pre>

<p>should be converted to raw bytes with <code>bytes(p)</code>.
This will give you something like:</p>

<pre><code>'E\x00\x00(\x00\x01\x00\x00@\x06\xf6w\xc0\xa8\x01\t\xc0\xa8\x01\xfe\x97%\x00P\x00\x00\x00\x00\x00\x00\x00\x00P\x02 \x00t\x15\x00\x00'
</code></pre>

<p>Then you can send it using raw sockets. So for your example you could modify a little your code like:</p>

<pre><code>from scapy.all import *
import socket

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    p = IP(dst="192.168.1.254")/TCP(flags="S", sport=RandShort(),dport=80)/Raw("Hallo world!")
    s.connect(("192.168.1.254",80))
    s.send(bytes(p))
    print "[+] Request Sent!"
except Exception, e:
    raise e
</code></pre>

<p>This should work!</p>

<p><strong>Notice!!!</strong>
Have in mind that when you use socket (module) to communicate with another computer
sockets automatically construct your packets (headers, etc) and send the content you wish 
to send. But when you construct a packet with scapy you craft it from the beginning so 
you define its content and its headers,layers etc. So in your example when you send your packet
you will sent 'all' as content-payload even the packet-headers(ip-header,tcp-header).
You can test it by running the below sniffer:</p>

<pre><code>#!/usr/bin/env python

from scapy.all import *

def printer(packet):
    if packet.haslayer(Raw):
        print packet.getlayer(Raw).load

print "[+] Sniff started"
while True:
    sniff(store=0, filter="host 192.168.1.254 and port 80", prn=printer, iface="your_interface_here")
</code></pre>

<p>Well while the sniffer is running try to run <strong>the first piece of code in my post</strong> (as I updated the packet with a raw layer=tcp.payload) and you will observe that not only 
the data but the whole packets gets transmitted as data. So you kind of sending the headers twice. That's why sockets has its own send method and scapy its own.</p>

python sockets python-3.x scapy