GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — security/http_text_to_pcap.py at master · b40yd/security

Expand file tree

/

http_text_to_pcap.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
171 lines (137 loc) · 4.45 KB
/

http_text_to_pcap.py

Copy path

File metadata and controls

171 lines (137 loc) · 4.45 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#
# Copyright (C) 2023, b40yd
#
# Author: b40yd <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
import random
import re
import socket
import uuid
from scapy.all import IP, TCP, wrpcap, Raw, Ether
from sys import version_info
if version_info.major == 2:
from BaseHTTPServer import BaseHTTPRequestHandler
from StringIO import StringIO
else:
from http.server import BaseHTTPRequestHandler
from io import StringIO
class HTTPRequest(BaseHTTPRequestHandler):
def __init__(self, request_text):
self.rfile = StringIO(request_text)
self.raw_requestline = self.rfile.readline()
self.error_code = self.error_message = None
self.parse_request()
def send_error(self, code, message):
self.error_code = code
self.error_message = message
DOMAIN_REGEX = re.compile(
r"^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([a-zA-Z0-9\-]+\.[a-zA-Z]{2,})(?:[\/\w\.-]*)*\/?$"
)
def get_ip():
"""get local ip"""
ssc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
ssc.connect(("10.255.255.255", 1))
local_ip = ssc.getsockname()[0]
except:
local_ip = "127.0.0.1"
finally:
ssc.close()
return local_ip
def gen_pkt(
dst,
dst_port,
random_port=43454,
http_request_text="",
http_response_text="",
src_mac="18:26:3a:30:3c:e8",
dst_mac="02:42:AC:11:00:03",
):
"""gen pcap packet"""
http_request_bytes = bytes(http_request_text)
if not http_request_bytes.endswith("\n"):
http_request_bytes = http_request_bytes + b"\n"
http_response_bytes = bytes(http_response_text)
if not http_response_bytes.endswith("\n"):
http_response_bytes = http_response_bytes + b"\n"
http_request = (Ether(src=src_mac, dst=dst_mac) / IP(dst=dst) / TCP(
dport=dst_port,
sport=random_port,
flags="A",
) / Raw(http_request_bytes))
# http_request.show()
(http_response, ) = (Ether(dst=src_mac, src=dst_mac) / IP(
src=dst,
dst=get_ip(),
) / TCP(
dport=random_port,
sport=dst_port,
seq=http_request[TCP].ack,
ack=http_request[TCP].seq + len(http_request[Raw]),
flags="PA",
) / Raw(http_response_bytes))
# http_response.show()
return http_request, http_response
def get_mac_address():
"""get interface mac address"""
mac_address = uuid.getnode()
return ":".join([
"{:02x}".format((mac_address >> ele) & 0xFF)
for ele in range(0, 8 * 6, 8)
][::-1])
def get_host_and_port(request=""):
"""get host and port"""
host = ""
port = 80
req = HTTPRequest(request)
host_str = req.headers.get("host", "")
if ":" in host_str:
tmp = host_str.replace("http://", "").replace("https://","").split(":")
if len(tmp) >= 2:
host = tmp[0]
port = int(tmp[1])
else:
host = host_str
if re.search(DOMAIN_REGEX, host):
host = get_ip()
return host, port
def gen_all_packet(multi_http_packet):
"""gen all http text to http packet"""
result = []
for req, resp in multi_http_packet:
host, port = get_host_and_port(req)
http_pkt = gen_pkt(
host,
port,
random.randint(23456, 65500),
req,
resp,
src_mac=get_mac_address(),
)
result.append(http_pkt)
return result
def test_from_files():
"""test http request and response from file"""
with open("http-req.txt", "r") as f:
http_request_text = f.read()
# Read the HTTP response from a file
with open("http-resp.txt", "r") as f:
http_response_text = f.read()
pkts = gen_all_packet([(http_request_text, http_response_text)])
# Write the request and response packets to a PCAP file
wrpcap(
"http.pcap",
pkts,
)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.