Advertisement
IWBH_01

node_insec_basic_wup.js

Jul 27th, 2020
2,308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  This script depends on "mime_types.txt" accessible in my public pastes https://pastebin.com/u/IWBH_01
  3.  It is a basic insecure nodejs server with upload.
  4. */
  5.  
  6. global.self=global;0;
  7.  
  8.  
  9. var http = require("http"),
  10. fs=require("fs"),
  11. zlib=require("zlib"),
  12.  
  13.  
  14. gurl=function(s,bf,af){
  15. var a1="",ci0=s.indexOf(bf);
  16. if(ci0+1){a1=s.substr(ci0+bf.length);
  17.     var ci1=a1.indexOf(af);
  18.     if(ci1+1)a1=a1.substring(0,ci1);
  19. }
  20. return a1;},
  21.  
  22. urlqp=function(wlc){
  23. var oo={},a0=wlc.substr(wlc.indexOf("?")+1).split("&"),i2=0;
  24. while(i2<a0.length){
  25.     var zi2=a0[i2],ei=zi2.indexOf("="),ez=ei>0;
  26.     oo[decodeURIComponent((ez?zi2.substr(0,ei):zi2).replace(/\+/g," ")).replace(/\x20/g,"_")]=ez?decodeURIComponent(zi2.substr(ei+1).replace(/\+/g," ")):"";
  27.     i2++;
  28. }
  29. return oo;},
  30.  
  31. mimes_=urlqp(gurl(fs.readFileSync("//mnt/sdb2/stuff_stored_here/windows/www_partial/mime_types.txt","utf-8"),'"','";')),
  32.  
  33.  
  34. esc_str=function(s,l,e,h){
  35. if(!l)l=127;
  36. s=s.split('');
  37. var i0=0,cc,cs,ex="\"'\r\n\0"+(typeof e=="string"?e:""),L='length';
  38. while(i0<s[L]){
  39. cc=s[i0].charCodeAt(0);
  40. if((ex.indexOf(s[i0])+1)||cc>l){
  41. if(h) s[i0]="&#"+cc+";"; else{
  42. cs=cc.toString(16);
  43. if(cs[L]<2)cs="0"+cs;
  44. if(cs[L]==2)s[i0]="\\x"+cs;
  45. else if(cs[L]==3)cs="0"+cs;
  46. if(cs[L]==4)s[i0]="\\u"+cs;}
  47. }i0++;
  48. }return s.join('');},
  49.  
  50.  
  51. getFoF=function(pth,fp,si){ //fp = client side path, si = show index.html
  52.  if(fs.existsSync(pth)){
  53.   try{
  54.    var itms=fs.readdirSync(pth),skp;
  55.    if(si){ for(var nm of itms){ var nL=nm.toLowerCase(); if(nL=="index.html"||nL=="index.htm"){ skp=!0;if(pth[pth.length-1]!="/")pth+="/";pth+=nm;break; } } }
  56.    
  57.    if(!skp){
  58.    itms.sort().reverse();
  59.    var p2=fp||pth,lcs=p2[p2.length-1]=="/",p3=p2+(lcs?"":"/"),ti="Index of "+esc_str(p3,127,!1,!0),r="<!Doctype html><html><head><meta content=\"text/html;charset=utf-8\" http-equiv=\"Content-Type\"/>"+(lcs?"":"<base href=\""+p3+"\">")+"<title>"+ti+"</title></head><body><div style=\"position:fixed;top:0;left:0;overflow:scroll;\">\r\n"+ti+"<br>\r\n<table><tr><td>Name</td><td>Type</td><td>Size</td><td>Created</td><td>Modified</td></tr><tr><td><a href=\"../\">../ (parent folder)</a></td><td>Folder</td><td>-</td><td>-</td><td>-</td></tr>\r\n";
  60.    for(var fn of itms){
  61.     var st2=fs.statSync(p3+fn),idr=st2.isDirectory();
  62.     r+="<tr><td><a href=\""+encodeURI(fn)+(idr?"/":"")+"\">"+esc_str(fn,127,!1,!0)+"</a></td><td>"+(idr?"Folder":"File")+"</td><td>"+st2.size+" bytes</td><td>"+st2.ctime.toGMTString()+"</td><td>"+st2.mtime.toGMTString()+"</td></tr>\r\n";
  63.    }
  64.    return r+"</table><br>Upload here: <input type='file' id='f1' multiple='true' ><br><span id='res'></span></div><script type=\"text/javascript\">var d=document,gI='getElementById',sy1=d.getElementsByTagName('div')[0].style,f1=d[gI]('f1'),res=d[gI]('res'); sy1.width=self.innerWidth+'px';sy1.height=self.innerHeight+'px'; f1.onchange="+(function(){
  65.  var i=0,frds=[],
  66.     rdff=function(e){
  67.        var fi=f1.files[this.n],xhr1=new XMLHttpRequest(),d0="/*ulf"+location.pathname;
  68.        if(d0[d0.length-1]!="/")d0+="/";
  69.        xhr1.open("POST",d0+fi.name,!0);
  70.        res.innerHTML+="<br>sending "+fi.name;
  71.        xhr1.onload=function(){res.innerHTML+="<br>"+fi.name+": "+xhr1.responseText;};
  72.        xhr1.send(e.target.result);
  73.        delete frds[this.n];
  74.     },
  75.     errf=function(e){res.innerHTML+=("<br>The file '"+f1.files[this.n].name+"' could not be read! Code "+e.target.error.code);};
  76.  
  77.     while(i<f1.files.length){
  78.         frds[i] = new FileReader();
  79.         frds[i].n=i;
  80.         frds[i].addEventListener("load",rdff);
  81.         frds[i].addEventListener("error",errf);
  82.         frds[i].readAsArrayBuffer(f1.files[i]);
  83.         i++;
  84.     }
  85. }).toString()+";</script></body></html>";
  86.    }
  87.   }catch(e){}
  88.   return fs.readFileSync(pth);
  89.  }
  90. },
  91.  
  92.  
  93. snd_rsp=function(rc,rspS,hed){
  94.  if(!rspS)rspS="no content";
  95.  var h1={"Content-Type":"text/html;charset=utf-8","Content-Length":rspS.length,"Access-Control-Allow-Origin":"*"};
  96.  if(typeof hed=="object")Object.assign(h1,hed);
  97.  if(rspS.length>2048){
  98.   if(typeof rspS=="string")rspS=new Buffer(rspS);
  99.   h1["Content-Encoding"]="gzip";
  100.   rspS=zlib.gzipSync(rspS);
  101.   h1["Content-Length"]=rspS.length;
  102.  }
  103.  this.writeHead(rc||200,h1);
  104.  this.write(rspS);
  105.  this.end();
  106. },
  107.  
  108. srv_func=function(req, rsp){
  109.   self.L_req=req;
  110. var Sk=req.socket,
  111. o_={"rep":{"ip":Sk.remoteAddress,"port":Sk.remotePort},
  112.  "lep":{"ip":Sk.localAddress,"port":Sk.localPort}},
  113. rspnd=snd_rsp.bind(rsp),
  114. buf_=[];
  115.   console.log("got "+req.url+"\nfrom: "+o_.rep.ip+"\n");
  116.  req.on('data',buf_.push.bind(buf_));
  117.  req.on('end', function(){
  118.   var body=Buffer.concat(buf_),
  119.   qi=req.url.indexOf("?"),unq=decodeURIComponent(qi>0?req.url.substr(0,qi):req.url).replace(/\/\.\.\//g,"/").replace(/\/.\//g,"/"),
  120.   sc,rspS,rshed={},rspS;
  121.   if(unq.substr(0,6)=="/*ulf/"&&body.length){
  122.    console.log("upload?");
  123.    var fp=unq.substr(5); rspS="";
  124.    if(fs.existsSync(fp)){ rspS="file exists."; }else{ try{ fs.writeFileSync(fp,body); rspS="did? "; }catch(e){ rspS+=e; } }
  125.   }else if(unq=="/*fuwp.html"){
  126.    rspS=getFoF("//home/tc/Documents/fuwp.html");
  127.   }else{
  128.    rspS=getFoF(unq);
  129.    var pxt=unq.lastIndexOf(".")+1,ext=pxt?unq.substr(pxt):0;
  130.    if(ext)rshed["Content-Type"]=mimes_[ext]+";charset=utf-8";
  131.   }
  132.   if(!rspS){ sc=404; rspS="not found."; }
  133.  
  134.   rspnd(sc,rspS,rshed);
  135.  });
  136.  
  137. };
  138.  
  139.  
  140. self.svr1=http.createServer(srv_func);
  141. svr1.listen(120);
  142.  
  143.  
  144.  
  145.  
  146.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement