aes_encrypt() function hooking - aes

I want to know the pid of the process which is performing the AES encryption. I have written the following function hooking code:
#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>
#include <openssl/aes.h>
void AES_encrypt(const unsigned char *in_var, unsigned char *out_var,
const AES_KEY *key_var)
{
void (*new_aes_encrypt)(const unsigned char *in_var, unsigned char *out_var,
const AES_KEY *key_var);
new_aes_encrypt = dlsym(RTLD_NEXT, "AES_encrypt");
FILE *logfile = fopen("logfile", "a+");
fprintf(logfile, "Process %d:nn%snnn", getpid(), (char *)in_var);
fclose(logfile);
new_aes_encrypt(in_var, out_var, key_var);
}
Then at terminal I did the followings:
#gcc aes_hook.c -o aes_hook.so -fPIC -shared -lssl -D_GNU_SOURCE
#export LD_PRELOAD="/<directory location>/aes_hook.so"
However, when I started the AES encryption (by a dummy process), I could not get it's pid in logfile. Why is this hooking not working?
*P.S.: Following is the declaration of AES_encrypt (in aes.h of OpenSSL) function that is called to perform the AES encryption.
void AES_encrypt(const unsigned char *in, unsigned char *out,
const AES_KEY *key)

Related

Difficulty adding Paillier algorithm to perceptual hash implementation

Received error:
libNCPH.h:39:15: fatal error: paillier.h: No such file or directory
39 | #include <paillier.h>
In libNCPH.h, it is also in this way:
extern "C" {
#include <gmp.h>
#include <paillier.h>
#include "tcp.h"}

How to define a undefinded reference to a function

I am working on two Beaglebone Black with Xenomai and RTnet. I have two c-files for a roundtrip ethernet frame between the BBB's. When I try to compile the first c-file there occur some errors:
undefined reference to 'rt_task_self'
rt_task_self is a function in my c-file and is declared in my headerfile "task.h". So in my opinion "undefined" means that it is just not defined in any cpp-file "task.cpp" for the headerfile "task.h".
But I am a little bit confused: How do I tell my program that my headerfile "task.h" is defined in my other file "task.cpp" or "task.o" or...
I have many header files in my C-file but only error with my "task.h" file and I do not see any differences in the #include rows between my "task.h" and all the other header files.
Part of Roundtrip C-file:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
/*XENOMAI*/
#include "task.h"
#include <rtdm/rtdm.h>
#include <asm/ioctl.h>
#define SERVER "192.168.127.10"
#define BUFLEN 512
#define PORT 8888
void die(char *s)
{
perror(s);
exit(1);
}
Part of task.h:
#ifndef _XENO_TASK_H
#define _XENO_TASK_H
#include <nucleus/sched.h>
#include <native/types.h>
/* Creation flags. */
#define T_FPU XNFPU
#define T_SUSP XNSUSP
/* <!> High bits must not conflict with XNFPU|XNSHADOW|XNSUSP. */
#define T_CPU(cpu) (1 << (24 + (cpu & 7))) /* Up to 8 cpus [0-7] */
#define T_CPUMASK 0xff000000
Part of another headerfile in the roundtrip c-file:
#ifndef _RTDM_H
#define _RTDM_H
#ifdef __KERNEL__
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/ioctl.h>
#include <linux/sched.h>
#include <linux/socket.h>
typedef u32 socklen_t;
typedef struct task_struct rtdm_user_info_t;
#else /* !__KERNEL__ */
#include <fcntl.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
My makefile:
# Allow overriding xeno-config on make command line
XENO_CONFIG=xeno-config
prefix := $(shell $(XENO_CONFIG) --prefix)
ifeq ($(prefix),)
$(error Please add <xenomai-install-path>/bin to your PATH variable)
endif
CC := $(shell $(XENO_CONFIG) --skin=posix --cc)
STD_CFLAGS := $(shell $(XENO_CONFIG) --skin=posix --cflags) -g
STD_LDFLAGS := $(shell $(XENO_CONFIG) --skin=posix --ldflags) -g -lrtdm
STD_TARGETS := rtt_rt
all: $(STD_TARGETS)
$(STD_TARGETS): $(STD_TARGETS:%=%.c)
$(CC) -o $# $< $(STD_CFLAGS) $(STD_LDFLAGS)
clean:
$(RM) -f *.o *~ $(STD_TARGETS)

db2 external function ends with SQL0444N, Reason code 6, SQLSTATE 42724

I'll develop new (external) functions for DB2. My first test:
db2crypt.h
#ifndef DB2CRYPT_H
#define DB2CRYPT_H
#include <string.h>
#include <stdlib.h>
char *encryptAes(const char *source, const char *key);
#endif /* DB2CRYPT_H */
db2crypt.cpp
#include "db2crypt.h"
#include <string>
char *encryptAes(const char *source, const char *key) {
std::string test("abc");
return (char *)test.c_str();
}
is compiled without an error.
g++ -fPIC -c db2crypt.cpp -std=c++14
g++ -shared -o db2crypt db2crypt.o -L$DB2PATH -ldb2
I also copied the new file into $DB2PATH/function and made a softlink in $DB2PATH/function/unfenced.
Then I created the function with
create function aes(VARCHAR(4096), VARCHAR(4096))
SPECIFIC encryptAes
RETURNS VARCHAR(4069)
NOT FENCED
DETERMINISTIC
NO SQL
NO EXTERNAL ACTION
LANGUAGE C
RETURNS NULL ON NULL
INPUT PARAMETER STYLE SQL
EXTERNAL NAME "db2crypt!encryptAes"
which was also ok.
But when I do select db2inst1.aes('a', 'b') from SYSIBM.SYSDUMMY1
I get the error
SQL0444N Die Routine "DB2INST1.AES" (spezifischer Name "ENCRYPTAES") ist
durch Code in Bibliothek oder Pfad ".../sqllib/function/db2crypt", Funktion
"encryptAes" implementiert, auf die kein Zugriff möglich ist. Ursachencode:
"6". SQLSTATE=42724
(sorry, I don't know how to change the error output into english)
What I made wrong?
Ok, I got the answer.
Thank you at #mao, you did help me. But I also needed some other help. If someone searches for an answer:
First you have to compile with some important parameters:
g++ -m64 -fPIC -c <yourfile>.cpp -std=c++14 -I/opt/ibm/db2/V11.1/include/ -D_REENTRANT
g++ -m64 -shared -o <yourfile> <yourfile>.o -L$DB2PATH -ldb2 -Wl,-rpath,$DB2PATH/$LIB -lpthread
Second: The function declaration, you also have to add parameters for null values AND the return value can't be a function return, it has to be a parameter. Also you have to use the types which are defined in sqludf.h:
void SQL_API_FN encryptAes(SQLUDF_CHAR *source,
SQLUDF_CHAR *key,
SQLUDF_CHAR out[4096],
SQLUDF_SMALLINT *sourcenull,
SQLUDF_SMALLINT *keynull,
SQLUDF_SMALLINT *outnull,
SQLUDF_TRAIL_ARGS) {
...
}
Also, when you do C++ instead of C, you have to tell the script that it has to handle the function as C:
#ifdef __cplusplus
extern "C"
#endif
void SQL_API_FN ...

how to automaticaly export windows root certificates to a file?

On a windows machine, I want to create a c++ code that exports windows root certificates to .pem \ .crt file (just like certmgr.msc tool allows me to do manually).
currently digging in windows' cryptoAPI docs but didn't find something.
Edit:
after using the soltuion below the PEM certificates are created in the following format (unnecary newline between lines and an extra character at the end) : -----BEGIN CERTIFICATE-----
MIICvDCCAiUCEEoZ0jiMglkcpV1zXxVd3KMwDQYJKoZIhvcNAQEEBQAwgZ4xHzAd
BgNVBAoTFlZlcmlTaWduIFRydXN0IE5ldHdvcmsxFzAVBgNVBAsTDlZlcmlTaWdu
....
Rj1QNAufcFb2jip/F87lY795aQdzLrCVKIr17aqp0l3NCsoQCY/Os68olsR5KYSS
3P+6Z0JIppAQ5L9h+JxT5ZPRcz/4/Z1PhKxV0f0RY2M=
-----END CERTIFICATE-----
i don't believe it will be accepted by openSSL, what is the cause for this?
What you're looking for is CertEnumCertificatesInStore function.
Also if you want to save certificate in PEM you will need CryptBinaryToString.
#include <Windows.h>
#include <wincrypt.h>
#include <string>
#include <fstream>
#include <vector>
#pragma comment(lib, "crypt32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
DWORD num = 1;
/* open root certificate store */
HCERTSTORE hCertStore = CertOpenSystemStore(NULL, L"ROOT");
PCCERT_CONTEXT pCert = nullptr;
while (pCert = CertEnumCertificatesInStore(hCertStore, pCert))
{
/* if you need save certificate in PEM */
DWORD size = 0;
CryptBinaryToString(pCert->pbCertEncoded, pCert->cbCertEncoded, CRYPT_STRING_BASE64HEADER, nullptr, &size);
std::vector<wchar_t> pem(size);
CryptBinaryToString(pCert->pbCertEncoded, pCert->cbCertEncoded, CRYPT_STRING_BASE64HEADER,
pem.data(), &size);
std::wstring pem_cert = std::to_wstring(num) + L".pem";
std::wofstream pem_cert_file(pem_cert, std::ios::binary | std::ios::out);
pem_cert_file.write(pem.data(), pem.size() - 1);
/* or if you need save certificate in binary form (DER encoding)*/
std::string der_cert = std::to_string(num) + ".cer";
std::ofstream der_cert_file(der_cert, std::ios::binary | std::ios::out);
der_cert_file.write(reinterpret_cast<char*>(pCert->pbCertEncoded), pCert->cbCertEncoded);
++num;
}
CertCloseStore(hCertStore, 0);
return 0;
}

Does anyone know if ios4 supports unix domain sockets?

The following works without error on OSX 10.6, but fails in the iphone simulator using SDK 4.1
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/tcp.h>
#include <sys/un.h>
#include <string.h>
int main(void) {
int sock = socket(AF_UNIX, SOCK_DGRAM, 0);
struct sockaddr_un sock_addr;
memset(&sock_addr, 0, sizeof(struct sockaddr_un));
sock_addr.sun_family = AF_UNIX;
strcpy(sock_addr.sun_path, "/tmp/sock");
int err = bind(sock, (struct sockaddr*)&sock_addr, sizeof(struct sockaddr_un));
if(err == -1) {
perror("bind: ");
}
}
Error is "Address family not supported by protocol family"
Any ideas?
You really need to check sock already - most likely, the socket creation is what failed already.
My guess is that AF_UNIX/SOCK_DGRAM is not supported; try SOCK_STREAM instead.