packagehttpimport("honeypot/internal/logger""honeypot/internal/types""io/fs""net/http""strconv""strings""time")// wpFormsHandler handles POST requests to /wp-admin/admin-post.php.func(h*httpHoneypot)wpFormsHandler()http.Handler{returnhttp.HandlerFunc(func(whttp.ResponseWriter,r*http.Request){fields:=h.buildRequestFields(r)h.addHeadersToFields(r,fields)// read post timepostTime:=r.FormValue("form_nonce")ifpostTime==""||postTime=="0"{fields["time_diff"]="zero"}// calculate time difference between post time and current timepostTimeInt,err:=strconv.ParseInt(postTime,10,64)iferr!=nil{fields["time_diff"]="invalid"}timeDiff:=time.Now().Unix()-postTimeIntfields["time_diff"]=timeDiffemail:=r.FormValue("email")fields["email"]=emailname:=r.FormValue("name")fields["name"]=namemessage:=r.FormValue("message")fields["message"]=messageremoteHost,remotePort:=h.getRemoteAddr(r)vardstPortuint16ifport,ok:=r.Context().Value(dstPortKey).(uint16);ok{dstPort=port}event:=types.LogEvent{Type:HoneypotType,Event:"contact_form",RemoteAddr:remoteHost,RemotePort:remotePort,DstPort:dstPort,Fields:fields,}logger.LogEvent(h.logger,event)h.recordHTTPMetrics(event)w.Header().Add("Location","/?success=true#kontakt")w.WriteHeader(http.StatusMovedPermanently)w.Write([]byte("Form submission received"))})}// wpAdminHandler handles WordPress admin routes, redirecting to login page or serving static files.func(h*httpHoneypot)wpAdminHandler(staticRootfs.FS)http.Handler{returnhttp.HandlerFunc(func(whttp.ResponseWriter,r*http.Request){// Redirect /wp-admin/ and /wp-admin to wp-login.phpifr.URL.Path=="/wp-admin/"||r.URL.Path=="/wp-admin"{http.Redirect(w,r,"/wp-login.php",http.StatusFound)return}// Serve static files under /wp-admin/h.serveFile(w,r,r.URL.Path)})}// staticFileHandler serves files from the static directory.func(h*httpHoneypot)staticFileHandler(staticRootfs.FS)http.Handler{returnhttp.HandlerFunc(func(whttp.ResponseWriter,r*http.Request){filename:=h.resolveStaticPath(r.URL.Path)h.serveFile(w,r,filename)})}func(h*httpHoneypot)fakeAdminOK(whttp.ResponseWriter,r*http.Request){h.setResponseHeaders(w)w.WriteHeader(http.StatusOK)w.Write([]byte(`<!DOCTYPE html>
<html>
<head><title>Admin Panel</title></head>
<body>
<h1>Admin Panel</h1>
<p>Access denied.</p>
</body>
</html>
`))}func(h*httpHoneypot)fakeBearerProtectedHandler(realmstring,resourcestring)http.Handler{returnhttp.HandlerFunc(func(whttp.ResponseWriter,r*http.Request){auth:=r.Header.Get("Authorization")vartokenstringifstrings.HasPrefix(strings.ToLower(auth),"bearer "){token=strings.TrimSpace(auth[len("Bearer "):])}fields:=h.buildRequestFields(r)h.addHeadersToFields(r,fields)event:=types.LogEvent{}iftoken!=""{fields["auth_type"]="bearer"fields["token"]=tokenfields["realm"]=realmifresource!=""{fields["resource"]=resource}event.Event=types.EventAuthAttempt}else{event.Event=types.EventRequest}remoteHost,remotePort:=h.getRemoteAddr(r)vardstPortuint16ifport,ok:=r.Context().Value(dstPortKey).(uint16);ok{dstPort=port}event.Type=HoneypotTypeevent.RemoteAddr=remoteHostevent.RemotePort=remotePortevent.DstPort=dstPortevent.Fields=fieldslogger.LogEvent(h.logger,event)h.recordHTTPMetrics(event)iftoken!=""&&looksLikeBearerToken(token){h.writeNginxError(w,http.StatusForbidden)return}w.Header().Set("WWW-Authenticate",`Bearer realm="`+realm+`", error="invalid_token"`,)h.writeNginxError(w,http.StatusUnauthorized)})}func(h*httpHoneypot)serveWordPressPHP(whttp.ResponseWriter,r*http.Request){h.setResponseHeaders(w)w.Header().Set("Content-Type","text/html; charset=UTF-8")w.WriteHeader(http.StatusOK)// Intentionally empty body}