前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >轻量化HTTP服务器环境快速搭建部署

轻量化HTTP服务器环境快速搭建部署

作者头像
全栈工程师修炼指南
发布2022-09-28 16:22:44
1.1K0
发布2022-09-28 16:22:44
举报

[TOC]

0x01 快速搭建轻量化HTTP服务器

描述:在做运维或者安全相关的测试项目的时候,需要快速搭建HTML服务器环境来下载文件或者POC,主要针对于HTML代码与文件浏览下载;

1.Python的SimpleHTTPServer模块实现

命令示例:

代码语言:javascript
复制
# Python2
python -m SimpleHTTPServer 8080

# python3已经改成了http.server
python -m http.server 端口号
WeiyiGeek.python快速搭建web
WeiyiGeek.python快速搭建web

WeiyiGeek.python快速搭建web

2.php的-S命令实现

命令示例:

代码语言:javascript
复制
exec php -S 0:8081 index.php
WeiyiGeek.php快速搭建web
WeiyiGeek.php快速搭建web

WeiyiGeek.php快速搭建web

3.PowerShell的System.Net.HttpListener对象实现

https://www.pstips.net/question/18306.html powershell New-Object System.Net.HttpListener

代码语言:javascript
复制
$Port = 8888
$Url = ""
$listener = New-Object System.Net.HttpListener
$prefix = "http://*:$Port/$Url"
$listener.Prefixes.Add($prefix)
$listener.Start()
 
使用 $listener.GetContext() 或 $listener.BeginGetContext 都是等待任务运行完成才能处理下一个请求,如何同时处理多个请求?

最近有个项目要构建一个轻量化的HTTP服务器,而且需要支持HTTPS

因为要求最好不使用IIS,所以用的是HttpListener类,HTTP部分已经完成,但是在增加HTTPS支持时出现报错

已知需要支持HTTPS时,服务器Demo应该增加的部分首先是增加https开头的前缀

listener=New−ObjectSystem.Net.HttpListenerlistener=New−ObjectSystem.Net.HttpListenerprefix = “http://:Port/Port/Url”

$prefix_s = “https://:Port/Port/Url”

listener.Prefixes.Add(listener.Prefixes.Add(prefix)

listener.Prefixes.Add(listener.Prefixes.Add(prefix_s)

然后应该做的是完成监听端口和证书的绑定,证书是使用openssl生成的自签名证书,仅用于Demo的测试

openssl genrsa -des3 -out server.key 2048 #生成RSA私钥openssl genrsa -des3 -out server.key 2048 #生成RSA私钥 openssl req -new -key server.key -out server.csr #生成证书签名请求

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt #生成自签名证书openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt #生成自签名证书 openssl pkcs12 -export -inkey server.key -in server.crt -out server.pfx #生成PFX格式的证书

接着应该安装证书,使用Import-PfxCertificate可以完成这个工作

$mypwd= ConvertTo-SecureString -String “**“ -Force -AsPlainText

Import-PfxCertificate -FilePath mypath\server.pfxCert:\CurrentUser\My−Passwordmypath\server.pfxCert:\CurrentUser\My−Passwordmypwd

证书安装完成后就可以进行与端口的绑定了,Add-NetIPHttpsCertBinding应该是能完成这个功能的,参照微软文档中的Example如下:

Add-NetIPHttpsCertBinding -IpPort “127.0.0.1:443” -CertificateHash “806ADBC1A866167765450EB5735C86F7AD35327C” -CertifcateStoreName Cert:\CurrentUser\My -ApplicationId “{3ccf7768-e7d7-4b7f-af0c-f0ce698ef083}” -NullEncryption $false

其中806ADBC1A866167765450EB5735C86F7AD35327C是证书中的指纹信息,3ccf7768-e7d7-4b7f-af0c-f0ce698ef083是使用[Guid]::NewGuid()生成的GUID,但是执行后报错信息如下:

参照Add-NetIPHttpsCertBinding 命令的帮助文档CertifcateStoreName参数应对像下面那样: Add-NetIPHttpsCertBinding -IpPort “10.1.1.1:443” -CertificateHash “0102030405060708090A0B0C0D0E0F1011121314” –CertifcateStoreName “My” –ApplicationId “{3F2504E0-4F89-11D3-9A0C-0305E82C3301}” -NullEncryption $false

代码语言:javascript
复制
function Load-Packages
{
    param ([string] $directory = 'Packages')
    $assemblies = Get-ChildItem $directory -Recurse -Filter '*.dll' | Select -Expand FullName
    foreach ($assembly in $assemblies) { [System.Reflection.Assembly]::LoadFrom($assembly) }
}

Load-Packages

$routes = @{
    "/ola" = { return '<html><body>Hello world!</body></html>' }
}

$url = 'http://localhost:8080/'
$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add($url)
$listener.Start()

Write-Host "Listening at $url..."

while ($listener.IsListening)
{
    $context = $listener.GetContext()
    $requestUrl = $context.Request.Url
    $response = $context.Response

    Write-Host ''
    Write-Host "> $requestUrl"

    $localPath = $requestUrl.LocalPath
    $route = $routes.Get_Item($requestUrl.LocalPath)

    if ($route -eq $null)
    {
        $response.StatusCode = 404
    }
    else
    {
        $content = & $route
        $buffer = [System.Text.Encoding]::UTF8.GetBytes($content)
        $response.ContentLength64 = $buffer.Length
        $response.OutputStream.Write($buffer, 0, $buffer.Length)
    }
    
    $response.Close()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-03-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 0x01 快速搭建轻量化HTTP服务器
    • 1.Python的SimpleHTTPServer模块实现
      • 2.php的-S命令实现
        • 3.PowerShell的System.Net.HttpListener对象实现
        相关产品与服务
        SSL 证书
        腾讯云 SSL 证书(SSL Certificates)为您提供 SSL 证书的申请、管理、部署等服务,为您提供一站式 HTTPS 解决方案。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档