init linux pic loader

master
JoYo 2016-01-08 19:47:59 +00:00
commit 83972b17cf
5 changed files with 520 additions and 0 deletions

71
Vagrantfile vendored Normal file
View File

@ -0,0 +1,71 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "ubuntu/wily64"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.
# Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
# such as FTP and Heroku are also available. See the documentation at
# https://docs.vagrantup.com/v2/push/atlas.html for more information.
# config.push.define "atlas" do |push|
# push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
# end
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# sudo apt-get update
# sudo apt-get install -y apache2
# SHELL
end

109
pic-linux.c Normal file
View File

@ -0,0 +1,109 @@
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <string.h>
#include <errno.h>
#include <sysexits.h>
#include <openssl/sha.h>
char *picProto(void *picAddr, size_t picSize, void *clonePtr) {
char *(*cloneFunc)(void *, size_t) = clonePtr;
return cloneFunc(picAddr, picSize);
}
char *clone(void *picAddr, size_t picSize) {
auto retVal = EX_SOFTWARE;
srand((unsigned int)time(NULL));
unsigned int picOffset = (rand() % (picSize + 1));
((char *)picAddr)[picOffset] = ((char *)picAddr)[picOffset] & (rand() % 1);
unsigned char *digest = NULL;
SHA_CTX sha;
SHA1_Init(&sha);
SHA1_Update(&sha, picAddr, picSize);
SHA1_Final(digest, &sha);
char *fileOutPath = NULL;
sprintf(fileOutPath, "./%s_%x.bin", digest, rand());
FILE *fileOutHandle = fopen(fileOutPath, "wb");
if (NULL == fileOutHandle) {
retVal = EX_SOFTWARE;
goto CLONE_CLEANUP;
}
retVal = fwrite(picAddr, 1, picSize, fileOutHandle);
if (retVal != picSize) {
retVal = EX_SOFTWARE;
goto CLONE_CLEANUP;
}
retVal = EX_OK;
CLONE_CLEANUP:
if (fileOutHandle) {
fclose(fileOutHandle);
}
return fileOutPath;
}
int main(int argc, const char **argv) {
auto retVal = EX_SOFTWARE;
char *fileInPath = argv[1];
FILE *fileInHandle = fopen(fileInPath, "rb");
if (NULL == fileInHandle) {
retVal = EX_SOFTWARE;
goto MAIN_CLEANUP;
}
fseek(fileInHandle, 0L, SEEK_END);
size_t picBuffer_len = ftell(fileInHandle);
fseek(fileInHandle, 0L, SEEK_SET);
if (0 >= picBuffer_len) {
retVal = EX_SOFTWARE;
goto MAIN_CLEANUP;
}
void *picBuffer = malloc(picBuffer_len);
if (NULL == picBuffer) {
retVal = EX_SOFTWARE;
goto MAIN_CLEANUP;
}
memset_s(&picBuffer, picBuffer_len, 0, picBuffer_len);
retVal = mprotect(picBuffer, picBuffer_len, PROT_EXEC);
if (0 == retVal) {
retVal = EX_SOFTWARE;
goto MAIN_CLEANUP;
}
retVal = fread(picBuffer, 1, picBuffer_len, fileInHandle);
if (retVal != picBuffer_len) {
retVal = EX_SOFTWARE;
goto MAIN_CLEANUP;
}
if (fileInHandle) {
fclose(fileInHandle);
}
char *(*cloneFunc)(void *, size_t) = clone;
void *(*picFunc)(void *, size_t, void *) = picBuffer;
char *childPath = picFunc(picBuffer, picBuffer_len, cloneFunc);
retVal = EX_OK;
MAIN_CLEANUP:
if (fileInHandle) {
fclose(fileInHandle);
}
if (picBuffer) {
free(picBuffer);
}
return retVal;
}

154
pic-windows.c Normal file
View File

@ -0,0 +1,154 @@
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <assert.h>
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <malloc.h>
#include <fcntl.h>
#include <intrin.h>
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "Mswsock.lib")
#pragma comment(lib, "AdvApi32.lib")
#define PORT 999
#define PORT_STR "999"
typedef auto (*FUNCPTR)();
int __cdecl main(int argc, const char **argv)
{
auto retVal = SOCKET_ERROR;
LPVOID picBuffer = NULL;
HANDLE fileHandle;
DWORD picBuffer_len = 0;
DWORD oldProtect;
char *fileInPath = argv[0];
char *fileOutPath = argv[0];
FUNCPTR func = NULL;
SecureZeroMemory(&fileHandle, sizeof(fileHandle));
fileHandle = CreateFile(fileInPath, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
if (INVALID_HANDLE_VALUE == fileHandle)
{
retVal = GetLastError();
goto CLEAN_UP;
}
picBuffer_len = GetFileSize(fileHandle, &picBuffer_len);
if (-1 == picBuffer_len)
{
retVal = GetLastError();
goto CLEAN_UP;
}
picBuffer = malloc(picBuffer_len);
if (NULL == picBuffer)
{
retVal = GetLastError();
goto CLEAN_UP;
}
SecureZeroMemory(picBuffer, sizeof(picBuffer_len));
retVal = VirtualProtect(picBuffer, picBuffer_len, PAGE_EXECUTE_READWRITE,
&oldProtect);
if (0 == retVal)
{
retVal = GetLastError();
goto CLEAN_UP;
}
SecureZeroMemory(&overlapped, sizeof(overlapped));
retVal =
ReadFile(fileHandle, picBuffer, picBuffer_len, &overlapped, NULL);
if (FALSE == retVal)
{
retVal = GetLastError();
goto CLEAN_UP;
}
if (fileHandle)
{
CloseHandle(fileHandle);
}
func = (FUNCPTR)picBuffer;
SecureZeroMemory(&wsaData, sizeof(wsaData));
WSAStartup(MAKEWORD(2, 2), &wsaData);
/* create sockets */
OVERLAPPED overlapped;
unsigned int listenSocket = INVALID_SOCKET;
unsigned int clientSocket = INVALID_SOCKET;
struct addrinfo *result = NULL;
struct addrinfo hints;
SOCKET socketReuse = INVALID_SOCKET;
WSABUF messageBuffer;
WSAOVERLAPPED sendOverlapped;
WSADATA wsaData;
SecureZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
retVal = getaddrinfo(NULL, PORT_STR, &hints, &result);
if (SOCKET_ERROR == retVal)
{
retVal = WSAGetLastError();
goto CLEAN_UP;
}
listenSocket = INVALID_SOCKET;
listenSocket =
socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (INVALID_SOCKET == listenSocket)
{
retVal = WSAGetLastError();
goto CLEAN_UP;
}
retVal = bind(listenSocket, result->ai_addr, (int)result->ai_addrlen);
if (SOCKET_ERROR == retVal)
{
retVal = WSAGetLastError();
goto CLEAN_UP;
}
freeaddrinfo(result);
retVal = listen(listenSocket, SOMAXCONN);
if (SOCKET_ERROR == retVal)
{
retVal = WSAGetLastError();
goto CLEAN_UP;
}
clientSocket = accept(listenSocket, NULL, NULL);
if (INVALID_SOCKET == clientSocket)
{
retVal = WSAGetLastError();
goto CLEAN_UP;
}
socketReuse = func(); /** PIC test here **/
retVal = 0;
CLEAN_UP:
if (fileHandle)
{
CloseHandle(fileHandle);
}
WSACleanup();
return retVal;
}

170
waf-1.8.18 Executable file

File diff suppressed because one or more lines are too long

16
wscript Normal file
View File

@ -0,0 +1,16 @@
import sys
def options(opt):
opt.load('asm compiler_c')
def configure(conf):
conf.load('asm compiler_c')
def build(bld):
bld.program(
source='pic-linux.c',
target='pic-linux')
# bld(features='nasm',
# source='scrap.asm',
# target='scrap')