代码抄袭改编自2025愚人节比赛N题
//在localhost:20254访问
//编译加入-lws2_32 -lgdi32
#include<bits/stdc++.h>
#include<winsock2.h>
#include<ws2tcpip.h>
using namespace std;
string password="***",token="access-token=";
mt19937 rnd(time(NULL));
struct http_response{
string content_type;
string body;
string head;
};
string decode_url(string url){
string decoded_url;
for (size_t i=0;i<url.length();i++){
if (url[i]=='%') {
if (i+2<url.length()){
int value=0;
stringstream ss;
ss<<hex<<url.substr(i+1,2);
ss>>value;
decoded_url+=static_cast<char>(value);
i+=2;
}
}
else if(url[i]=='+'){
decoded_url+=' ';
}
else {
decoded_url+=url[i];
}
}
return decoded_url;
}
string get_bmp_data(){
HDC hScreen=GetDC(NULL);
int w=GetSystemMetrics(SM_CXSCREEN);
int h=GetSystemMetrics(SM_CYSCREEN);
w*=2;
h*=2;
HDC hMemDC=CreateCompatibleDC(hScreen);
HBITMAP hBmp=CreateCompatibleBitmap(hScreen,w,h);
SelectObject(hMemDC, hBmp);
BitBlt(hMemDC,0,0,w,h,hScreen,0,0,SRCCOPY);
BITMAPINFOHEADER bmi={sizeof(bmi),w,-h,1,32,BI_RGB};
vector<char> pixels(w*h*4);
GetDIBits(hMemDC,hBmp,0,h,pixels.data(),(BITMAPINFO*)&bmi,DIB_RGB_COLORS);
string data;
data.append("BM",2);
uint32_t fileSize=54+pixels.size();
data.append((char*)&fileSize,4);
data.append(4,0);//Reserved
uint32_t offset=54;
data.append((char*)&offset,4);
data.append((char*)&bmi,sizeof(bmi));
data.append(pixels.data(),pixels.size());
DeleteObject(hBmp);
DeleteDC(hMemDC);
ReleaseDC(NULL,hScreen);
return data;
}
http_response work_with_request(string method,string request,string data,string cookie){
// cout<<"work "<<method<<' '<<request<<' '<<data<<' '<<cookie<<endl;
string login_first= "<head>"
" <meta http-equiv=\"refresh\" content=\"1;url=/login\">"
"</head>"
"<body>"
" <h3>Login First</h3>"
"</body>";
if(request=="favicon.ico"){
FILE *icofile=fopen("C:\\Program Files (x86)\\Dev-Cpp\\Icons\\Crazy.ico","rb");
string res;
char tmp;
while(fscanf(icofile,"%c",&tmp)!=EOF) res+=tmp;
fclose(icofile);
return {"image/x-icon",res,""};
}
else if(request.substr(0,5)=="login"&&method=="GET"){
return {"text/html","<head>"
"</head>"
"<body>"
" <h2>login</h2>"
" <form method=\"post\">"
" <input type=\"password\" name=\"password\">"
" <input type=\"submit\" value=\"ok\">"
" </form>"
"</body>"
,""};
}
else if(request.substr(0,5)=="login"&&method=="POST"){
if(data=="password="+password){
return {"text/html","<head>"
" <meta http-equiv=\"refresh\" content=\"1;url=/\">"
"</head>"
"<body>"
" <h3>Password Correct</h3>"
"</body>"
,"Set-Cookie: "+token+"; Path=/; HttpOnly; Secure; SameSite=Strict;"};
}
else return {"text/html", "<head>"
" <meta http-equiv=\"refresh\" content=\"1;url=/login\">"
"</head>"
"<body>"
" <h3>Password Wrong</h3>"
"</body>"
,""};
}
else if(request.substr(0,4)=="cmd/"){
size_t pos=cookie.find("access-token=");
int pass=0;
if(pos!=string::npos) {
string client_token=cookie.substr(pos,token.size());
if (client_token==token) pass=1;
}
if(pass==1){
string cmdline=request.substr(4);
char buf[524288]={};
FILE *pip=popen(cmdline.c_str(),"r");
fread(buf,1,sizeof(buf),pip);
return {"text/plain",buf,""};
}
else return {"text/html",login_first,""};
}
else if(request.substr(0,10)=="screenshot"){
size_t pos=cookie.find("access-token=");
int pass=0;
if(pos!=string::npos) {
string client_token=cookie.substr(pos,token.size());
if (client_token==token) pass=1;
}
if(pass==1) return {"image/bmp",get_bmp_data(),""};
else return {"text/html",login_first,""};
}
else if(request.substr(0,6)=="upload"&&method=="GET"){
size_t pos=cookie.find("access-token=");
int pass=0;
if(pos!=string::npos) {
string client_token=cookie.substr(pos,token.size());
if (client_token==token) pass=1;
}
if(pass==1) return {"text/html","<head>"
"</head>"
"<body>"
" <form method=\"post\">"
" <textarea rows=\"20\" cols=\"150\" name=\"file\"></textarea>"
" <input type=\"submit\" value=\"ok\">"
" </form>"
"</body>"
,""};
else return {"text/html",login_first,""};
}
else if(request.substr(0,6)=="upload"&&method=="POST"){
size_t pos=cookie.find("access-token=");
int pass=0;
if(pos!=string::npos) {
string client_token=cookie.substr(pos,token.size());
if (client_token==token) pass=1;
}
if(pass==1){
if(data.length()<5) return {"text/plain","err:data is null",""};
data=data.substr(5);
string filename="./uploads/";
for(int i=1;i<=10;i++) filename+='a'+rnd()%26;
filename+=".txt";
FILE *fout=fopen(filename.c_str(),"wb");
fprintf(fout,data.c_str());
fclose(fout);
return {"text/plain","saved as "+filename,""};
}
else return {"text/html",login_first,""};
}
return {"text/html","<head>"
"</head>"
"<body>"
" <a href=\"/login\">login</a>"
" <a href=\"/upload\">upload</a><br>"
" <img src=\"/screenshot\" style=\"height:460px;width:768px;\">"
"</body>"
,""};
}
void handle_request(SOCKET client_socket){
char buffer[32768];
int bytes_received=recv(client_socket,buffer,sizeof(buffer)-1,0);
if (bytes_received>0) {
buffer[bytes_received]='\0';
// for(int i=0;i<=13000;i++) printf("%c",buffer[i]);
string request(buffer);
string method;
string uri;
istringstream request_stream(request);
request_stream>>method;
request_stream>>uri;
//cout<<request<<endl<<endl;
size_t pos=request.find("\r\n\r\n");
string data;
if(pos!=string::npos){
data=decode_url(request.substr(pos+4));
request=request.substr(0,pos);
}
// cout<<request<<endl<<endl;
// cout<<data<<endl<<endl;
string key,val,cookie;
request_stream>>key;
while(request_stream>>key){
getline(request_stream,val);
if(key=="Cookie:") cookie=val.substr(1);
}
string decoded_uri=decode_url(uri.substr(1));
http_response response_body=work_with_request(method,decoded_uri,data,cookie);
string response_header= "HTTP/1.1 200 OK\n"
"Content-Type: "+response_body.content_type+"\n";
if(response_body.head!="") response_header+=response_body.head+"\n";
response_header+= "Content-Length: "+to_string(response_body.body.size()) + "\n\n";
string full_response=response_header+response_body.body;
send(client_socket,full_response.c_str(),full_response.size(),0);
}
closesocket(client_socket);
}
void start_server(int port){
for(int i=1;i<=128;i++) token+='A'+rnd()%26;
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);
SOCKET server_socket=socket(AF_INET,SOCK_STREAM,0);
sockaddr_in server_address;
server_address.sin_family=AF_INET;
server_address.sin_addr.s_addr=INADDR_ANY;
server_address.sin_port=htons(port);
bind(server_socket,(struct sockaddr*)&server_address,sizeof(server_address));
listen(server_socket,SOMAXCONN);
//printf("%s\n",GetLastError());
while (true){
SOCKET client_socket=accept(server_socket,nullptr,nullptr);
thread(handle_request,client_socket).detach();
}
closesocket(server_socket);
WSACleanup();
}
int main(){
start_server(20254);
return 0;
}