Write it in front
Today I need to use the page to upload the bin package. The file is about 3.9mb, but the result keeps getting errors
POST /cgi-bin/Upgrade .cgi undefined
Host: 192.168.137.200:8888
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/ 20100101 Firefox/120.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp, /;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q =0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Content-Type: multipart/form-data ; boundary=--------------------------279364548015623859994223247018
Content-Length: 4102997< a i=12> Origin: http://192.168.137.200:8888 Sec-GPC: 1 Connection: keep-alive Referer: http://192.168.137.200:8888/system-upgrade.html Upgrade-Insecure-Requests: 1 Pragma: no-cache< /span> It can be seen that the cgi program is not located. I am puzzled, but I must use method="post " enctype="multipart/form-data", finally found that boa has a file upload limit, the default is 1mb. . . . Cache-Control: no-cache
The solution is as follows
- Modify boa.conf configuration file
# SinglePostLimit: The maximum allowable number of bytes in
# a single POST. Default is normally 1MB.
SinglePostLimit 16777216 #16MB
Change it here to 16mb
Then use ps -aux |grep boa kill -9 process number
Restart ./boa and there will be no problem uploading
- Modify source code
If you modify the boa source code macro definition, modify define.h in the src folder:
#define SINGLE_POST_LIMIT_DEFAULT 1024 * 1024 * 16 /* 16 MB */
Then recompile the code
Paste the cgi code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "cgic.h"
#define BufferLen 4096
static unsigned char xor_mask[] =
{
46,175,116,168,85,253,15,163,167,87,
121,198,175,136,79,125,156,10,72,135,
208,71,39,164,238,167,42,101,154,14,
235,225,76,27,106,109,193,208,89,35,
101,37,12,121,241,205,7,227,169,30,
109,80,203,49,245,169,26,111,218,156,
145,78,186,219,215,80,28,186,60,191,
33,23,109,144,131,139,52,10,29,254,242,
236,30,144,84,124,46,95,30,240,217,211,
248,32,48,28,243,144
};
int cgiMain(int argc, char *argv[]) {
cgiFilePtr file;
int targetFile;
char name[128];
char fileNameOnServer[64];
char contentType[1024];
char buffer[BufferLen];
int filelen;
int fdNew, fdOld;
char *msg;
FILE *New, *Old;
struct stat buf;
int size;
int got;
int i;
int ret = 0;
int ret1 = 0;
cgiHeaderContentType("text/html; charset=UTF-8");
mkdir("/tmp/upgrade", 0755);
if (cgiFormFileName("file_Upgrade", name, sizeof(name)) != cgiFormSuccess) {
fprintf(cgiOut,"could not retrieve filename\n");
goto FAIL;
}
cgiFormFileSize("file_Upgrade", &size);
cgiFormFileContentType("file_Upgrade", contentType, sizeof(contentType));
if (cgiFormFileOpen("file_Upgrade", &file) != cgiFormSuccess) {
fprintf(cgiOut,"could not open the file\n");
goto FAIL;
}
targetFile = open ("/tmp/upgrade/test", O_RDWR|O_CREAT|O_TRUNC|O_APPEND, 0644);
if(targetFile<0){
fprintf(cgiOut,"could not create the new file,%s\n", fileNameOnServer);
goto FAIL;
}
while (cgiFormFileRead(file, buffer, BufferLen, &got) == cgiFormSuccess){
ret = 0;
if (got > 0) {
while (ret < got) {
ret += write(targetFile, buffer + ret, got - ret);
}
}
}
ret = 0;
cgiFormFileClose(file);
close(targetFile);
goto END;
FAIL:
fprintf(cgiOut, "Failed to upload");
return 1;
END:
stat("/tmp/upgrade/test", &buf);
filelen = buf.st_size;
New = fopen("/tmp/upgrade/upgrade.tar.gz", "wb");
if (New == NULL) {
return -1;
}
Old = fopen("/tmp/upgrade/test", "rb");
if (Old == NULL) {
return -1;
}
fdNew = fileno(New);
fdOld = fileno(Old);
msg = (char *)malloc(filelen * sizeof(char));
memset(msg, 0, filelen);
ret1 = 0;
while((ret = fread(msg, sizeof(char), 1024, Old)) > 0 )
{
for (i = ret1; i < ret + ret1; i++) {
msg[i - ret1] = msg[i - ret1] ^ xor_mask[i % sizeof(xor_mask)];
}
ret1 += ret;
fwrite(msg, sizeof(char), ret, New);
}
ret1 = 0;
free(msg);
fflush(Old);
fflush(New);
fsync(fdOld);
fsync(fdNew);
fclose(New);
fclose(Old);
system("tar -xf /tmp/upgrade/upgrade.tar.gz -C /tmp/upgrade");
return 0;
}