Why ELF files keep the actual names of global variables? rather than just keeping their addresses. Here is an example file:
int oren; int moish;
int main(int argc, char **argv)
{
if (argc>3)
{
oren=2;
moish=5;
return oren+moish;
}
return 8;
}
I compiled it and looked for moish with
$ gcc -O3 main.c -o main
$ objdump -D ./main | grep "moish"
I was a bit surprised to find the actual name moish inside (since I'm not sure
what it is needed for):
504: c7 05 06 0b 20 00 05 movl $0x5,0x200b06(%rip) # 201014 <moish>
0000000000201014 <moish>:
Any reason to keep it?
I was a bit surprised to find the actual name moish inside
UNIX binaries traditionally keep the symbol table in linked binary (executable or DSO) to assist in debugging. The symbol table is not used for anything else, and you can remove it from the binary using strip command, or linking with -Wl,-s flag.
After strip the disassembly looks like this:
105a: c7 05 c8 2f 00 00 05 movl $0x5,0x2fc8(%rip) # 402c <__cxa_finalize#plt+0x2ffc>
Related
I am building my Ada/SPARK project using GNAT and I am using a linker script. Here is an excerpt:
SECTIONS
{
.code :
{
. = ALIGN(0x4);
*(.text.section1)
_end_of_section1 = .;
*(.text.section2)
...
}
}
The symbol _end_of_section1 is the address between the two sections. I'd like to be able to access this in my Ada code. I know it's possible in C using extern char _end_of_section1[];. Is it possible to do something like this in Ada? If not, is there some other way to get this address in the code?
You can import a linker symbol by using the Importand Link_Name aspects (see also RM B.1):
main.adb (updated on 25-jan)
with System.Storage_Elements;
with System.Address_Image;
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
package SSE renames System.Storage_Elements;
package Storage_Element_IO is
new Ada.Text_IO.Modular_IO (SSE.Storage_Element);
use Storage_Element_IO;
Start_Symbol : aliased SSE.Storage_Element
with Import, Link_Name => "_start";
Start_Symbol_Addr : constant System.Address := Start_Symbol'Address;
begin
Put ("Address : ");
Put (System.Address_Image (Start_Symbol_Addr));
New_Line;
Put ("Value : ");
Put (Start_Symbol, Base => 16);
New_Line;
end Main;
output
$ ./obj/main
Address : 0000000000403300
Value : 16#F3#
output (objdump)
$ objdump -d -M intel ./obj/main | grep -A5 "<_start>"
0000000000403300 <_start>:
403300: f3 0f 1e fa endbr64
403304: 31 ed xor ebp,ebp
403306: 49 89 d1 mov r9,rdx
403309: 5e pop rsi
40330a: 48 89 e2 mov rdx,rsp
...
Can someone explain why the following code yields different results on the second printf if I comment the first printf line or not, in 64 bits?
/* gcc -O0 -o test test.c */
#include <stdio.h>
#include <stdlib.h>
int main() {
char a[20] = {0};
char b = 'a';
int count=-1;
// printf("%.16llx %.16llx\n", a, &b);
printf("%x\n", *(a+count));
return 0;
}
I get the following results for the second printf:
commented: 0
uncommented: 61
Thanks in advance!
iansus
Can someone explain why the following code yields different results on the second printf if I comment the first printf line or not
Your program uses a[-1], and thus exhibits undefined behavior. Anything can happen, and figuring out exactly why one or the other thing happenes is pointless.
The precise reason is that you are reading memory that gets written to by the first printf (when commented in).
I get a different result (which is expected with undefined behavior):
// with first `printf` commented out:
ffffffff
// with it commented in:
00007fffffffdd20 00007fffffffdd1b
ffffffff
You could see where that memory is written to by setting a GDB watchpoint on it:
(gdb) p a[-1]
$1 = 0 '\000'
(gdb) p &a[-1]
$2 = 0x7fffffffdd1f ""
(gdb) watch *(int*)0x7fffffffdd1f
Hardware watchpoint 4: *(int*)0x7fffffffdd1f
(gdb) c
Continuing.
Hardware watchpoint 4: *(int*)0x7fffffffdd1f
Old value = 0
New value = 255
main () at t.c:12
12 printf("%.16llx %.16llx\n", a, &b);
It my case above, the value is written as part of initializing count=-1. That is, with my version of gcc, count is located just before a[0]. But this may depend on compiler version, exactly how this compiler was built, etc. etc.
hi i have my UNIX file date in particular format 2017-02-01 i want to convert it in integer value like 20170201. What can i do get this output.My UNIX box is(SunOS 5.10).I tried to check below command to see what they output.But i am not getting anything.Can anyone help?
bash-3.2$ date +'%s'
%s
bash-3.2$ date +"%s"
%s
bash-3.2$ date +%s
%s
When i try date -d "Filename" +%Y%m%d option it error out saying:-
date: bad conversion
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
You can get the date you like with this line of code (bash):
date +"%Y%m%d"
you can use it as a filename like this:
_now=$(date +"%Y%m%d")
_file="/tmp/$_now.ext"
then use $_file for your filename
SunOS 5.10 (Solaris 10) is pretty old. There are some things it doesn't do.
In particular, I note that its date command relies on strftime() for formatting, and according to the man page, Solaris 10's strftime does not support %s. Which explains the results in the test you did in your question.
If you really just want a YYYYMMDD format, the following should work:
$ date '+%Y%m%d'
If you're looking for an epoch second, then you might have to use some other tool. Nawk, for example, should be installed on your system:
$ nawk 'BEGIN{print srand}'
You can man nawk and search for srand to see why this works.
Alternately, you could use Perl, if you've installed it:
$ perl -e 'print time . "\n";'
These are strategies to get the current epoch second, but your initial date command is the correct way to get the date formatted the way you suggested.
Based on other comments, it also appears you're looking to get the timestamp of certain files. That's not something the date command will do. In other operating systems, like Linux or FreeBSD, you'd use the stat command, which does not appear as a separate shell command in Solaris 10.
But you can do it yourself in a pretty short C program that uses the fstat(2) system call. While it may be beyond the scope required for this question, you can probably compile this using /usr/sfw/bin/gcc:
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h>
int main(int argc, char **argv)
{
struct tm info;
char buf[80];
struct stat fileStat;
time_t epoch;
if(argc != 2)
return 1;
int file=0;
if((file=open(argv[1],O_RDONLY)) < -1)
return 1;
if(fstat(file,&fileStat) < 0)
return 1;
info = *localtime( &fileStat.st_mtime );
epoch = mktime(&info);
printf("%s:%ld\n", argv[1], (long) epoch );
return 0;
}
For example:
$ gcc -o filedate filedate.c
$ ./filedate /bin/ls
/bin/ls:1309814136
$ perl -e 'use POSIX; print POSIX::strftime("%F %T\n", gmtime(1309814136));'
2011-07-04 21:15:36
$ ls -l /bin/ls
-r-xr-xr-x 1 root bin 18700 Jul 4 2011 /bin/ls
$
SOLVED IT WITH THIS COMMAND-:
browserDate="2016-11-21"
dateConversion="${browserDate//'-'}"
How to hash a string using SHA-1 and base64 encode? I have package openssl and I can use sha1 function to has a string but I don't know how to combine sha-1 and base64 encode to my string.
For testing purpose, my string is:
HY8&V5EDJO8NYT9C2011-12-13T00:44:02ZTest123
I need to SHA-1 and base64 encode to output:
nuoiyUX6m+irC5rvB4QUSycfHGc=
Please could someone kindly advice?
As you are using the openssl package, use the following function
openssl::base64_encode(openssl::sha1(charToRaw("HY8&V5EDJO8NYT9C2011-12-13T00:44:02ZTest123")))
openssl generally operates on a file,basically stdin in a byte stream.
I am passing "HY8&V5EDJO8NYT9C2011-12-13T00:44:02ZTest123" in a form of printf, In a sequence of non-0 bytes (which may or may not form valid characters in UTF-8 or other character set/encoding).
Since the input is passed from the terminal, on a keyboard. The terminal will send corresponding characters (as written on the key label) encoded in its configured character set.
Usually, set in the current locale. You can find that by using a command "locale charmap".
We have to make sure the input is first convierted to UTF-8 and then passed to openssl. that the reason i have used icovn to convert bytes to the corresponding encoding in UTF-8.
So below command can be used to convert our string in sha1 and then base64 using openssl.
printf %s "HY8&V5EDJO8NYT9C2011-12-13T00:44:02ZTest123" | iconv -t utf-8 | openssl dgst -sha1 -binary | openssl enc -base64
$ echo nuoiyUX6m+irC5rvB4QUSycfHGc= | base64 -d | xxd -g 1
00000000: 9e ea 22 c9 45 fa 9b e8 ab 0b 9a ef 07 84 14 4b ..".E..........K
00000010: 27 1f 1c 67
And:
$ echo -n "HY8&V5EDJO8NYT9C2011-12-13T00:44:02ZTest123" | openssl sha1
(stdin)= 9eea22c945fa9be8ab0b9aef0784144b271f1c67
And:
$ cat test.c
#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
int main()
{
unsigned char ibuf[] = "HY8&V5EDJO8NYT9C2011-12-13T00:44:02ZTest123";
unsigned char obuf[20];
SHA1(ibuf, strlen(ibuf), obuf);
int i;
for (i = 0; i < 20; i++) {
printf("%02x ", obuf[i]);
}
printf("\n");
return 0;
}
And then:
$ gcc -I /usr/local/include/ test.c /usr/local/lib/libcrypto.a -o test.exe
$ ./test.exe
9e ea 22 c9 45 fa 9b e8 ab 0b 9a ef 07 84 14 4b 27 1f 1c 67
Maybe a little bit late, but try
$ echo -n "HY8&V5EDJO8NYT9C2011-12-13T00:44:02ZTest123" | openssl dgst -binary -sha1 | openssl enc -base64
This will:
Echo the string to the pipe
openssl will digest the string using SHA1 algorithm and pipe it
openssl will encode the resulting digested string from step 2 into a new string using base64 algorithm
And it will be echoed back to you.
This is not typically a question where to find a step-by-step guide, but rather the guide itself.
My intention with this post is to give others a hint, who have the same problems in compiling the driver-plugin as I just had recently.
How to build the Qt-SQL-driver-plugin 'QSQLCIPHER' for SQLite-DB with SQLCipher-extension using the Windows/MinGW-platform?
How to build the Qt-SQL-driver-plugin 'QSQLCIPHER' for SQLite-DB with SQLCipher-extension using the Windows/MinGW-platform:
Qt 5.4.0 for Windows/MinGW
Download Qt
Install including the sources e.g to C:\Qt\Qt5.4.0
OpenSSL for Windows
Download Win32 OpenSSL v1.0.2a
Download Visual C++ 2008 Redistributable
Install Visual C++ 2008 Redistributable by executing 'vcredist_x86.exe'
Install OpenSSL v1.0.2a by executing 'Win32OpenSSL-1_0_2.exe'
Target directory e.g. C:\OpenSSL-Win32
During installation choose the option to install the libraries to the Windows system directory (C:\Windows\SysWOW64)
MinGW - Minimalist GNU for Windows
Download and install 'mingw-get-setup.exe'
Start of MinGW Installer
Installation of MSYS Base System
Selection: All Packages -> MSYS -> MSYS Base System
Select msys-base (Class 'bin') for installation
Menu: installation -> apply changes
Installation of files by default to directory C:\MinGW
Installation of Tcl/Tk
Selection: All Packages -> MinGW -> MinGW Contributed
Select 'mingw32-tcl' and 'mingw32-tk' (Class 'bin') for installation
Menu: installation -> apply changes
Installation of files by default to directory C:\MinGW
Copy content of C:\MinGW to the Qt-MinGW-directory C:\Qt\Qt5.4.0\Tools\mingw491_32
Create file 'fstab' in C:\Qt\Qt5.4.0\Tools\mingw491_32\msys\1.0\etc
Insert content as follows:
#Win32_Path Mount_Point
C:/Qt/Qt5.4.0/Tools/mingw491_32 /mingw
C:/Qt/Qt5.4.0/5.4 /qt
C:/ /c
zlib-Library
Download zlib-dll-Binaries
Extract and copy file 'zlib1.dll' to the Qt-MinGW-directory C:\Qt\Qt5.4.0\Tools\mingw491_32\msys\1.0\bin
SQLCipher
Download the SQLCipher-zip-file
Extract the zip-file e.g. to C:\temp\sqlcipher-master
Copy OpenSSL-Win32-libraries
Copy C:\OpenSSL-Win32\bin\libeay32.dll to C:\temp\sqlcipher-master
Copy C:\OpenSSL-Win32\lib\libeay32.lib to C:\temp\sqlcipher-master
Build SQLCipher.exe
Execute MSYS: C:\Qt\Qt5.4.0\Tools\mingw491_32\msys\1.0\msys.bat
$ cd /c/temp/sqlcipher-master
$ ./configure --prefix=$(pwd)/dist --with-crypto-lib=none --disable-tcl CFLAGS="-DSQLITE_HAS_CODEC -DSQLCIPHER_CRYPTO_OPENSSL -I/c/openssl-win32/include /c/temp/sqlcipher-master/libeay32.dll -L/c/temp/sqlcipher-master/ -static-libgcc" LDFLAGS="-leay32"
$ make clean
$ make sqlite3.c
$ make
$ make dll
$ make install
Save the executable SQLite/SQLCipher-database e.g. to C:\sqlcipher
Copy C:\temp\sqlcipher-master\dist\bin\sqlcipher.exe to C:\sqlcipher.
The file 'sqlcipher.exe' is the crypting equivalent to the non-crypting original command line interface 'sqlite3.exe'.
Copy C:\temp\sqlcipher-master\sqlite3.dll to C:\sqlcipher.
This file is the SQLite-library extended by the encryption.
The SQLite-database with SQLCipher-extension is now ready for work.
Build Qt-QSQLCIPHER-driver-plugin
Create directory:
C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\plugins\sqldrivers\sqlcipher
Create the following three files within the new directory:
File 1: smain.cpp:
#include <qsqldriverplugin.h>
#include <qstringlist.h>
#include "../../../../src/sql/drivers/sqlite/qsql_sqlite_p.h" // There was a missing " at the end of this line
QT_BEGIN_NAMESPACE
class QSQLcipherDriverPlugin : public QSqlDriverPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QSqlDriverFactoryInterface" FILE "sqlcipher.json")
public:
QSQLcipherDriverPlugin();
QSqlDriver* create(const QString &);
};
QSQLcipherDriverPlugin::QSQLcipherDriverPlugin()
: QSqlDriverPlugin()
{
}
QSqlDriver* QSQLcipherDriverPlugin::create(const QString &name)
{
if (name == QLatin1String("QSQLCIPHER")) {
QSQLiteDriver* driver = new QSQLiteDriver();
return driver;
}
return 0;
}
QT_END_NAMESPACE
#include "smain.moc"
File 2: sqlcipher.pro
TARGET = qsqlcipher
SOURCES = smain.cpp
OTHER_FILES += sqlcipher.json
include(../../../sql/drivers/sqlcipher/qsql_sqlite.pri)
wince*: DEFINES += HAVE_LOCALTIME_S=0
PLUGIN_CLASS_NAME = QSQLcipherDriverPlugin
include(../qsqldriverbase.pri)
File 3: sqlcipher.json
{
"Keys": [ "QSQLCIPHER" ]
}
Copy directory C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\sql\drivers\sqlite
to C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\sql\drivers\sqlcipher
Customize file C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\sql\drivers\sqlcipher\qsql_sqlite.pri
The content of the file shall be like:
HEADERS += $$PWD/qsql_sqlite_p.h
SOURCES += $$PWD/qsql_sqlite.cpp
!system-sqlite:!contains(LIBS, .*sqlite3.*) {
include($$PWD/../../../3rdparty/sqlcipher.pri) #<-- change path of sqlite.pri to sqlcipher.pri here !
} else {
LIBS += $$QT_LFLAGS_SQLITE
QMAKE_CXXFLAGS *= $$QT_CFLAGS_SQLITE
}
The remaining two files in this directory need not to be changed.
Create file 'sqlcipher.pri' in directory C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\3rdparty with following content:
CONFIG(release, debug|release):DEFINES *= NDEBUG
DEFINES += SQLITE_OMIT_LOAD_EXTENSION SQLITE_OMIT_COMPLETE SQLITE_ENABLE_FTS3 SQLITE_ENABLE_FTS3_PARENTHESIS SQLITE_ENABLE_RTREE SQLITE_HAS_CODEC
!contains(CONFIG, largefile):DEFINES += SQLITE_DISABLE_LFS
contains(QT_CONFIG, posix_fallocate):DEFINES += HAVE_POSIX_FALLOCATE=1
winrt: DEFINES += SQLITE_OS_WINRT
winphone: DEFINES += SQLITE_WIN32_FILEMAPPING_API=1
qnx: DEFINES += _QNX_SOURCE
INCLUDEPATH += $$PWD/sqlcipher c:/openssl-win32/include
SOURCES += $$PWD/sqlcipher/sqlite3.c
LIBS += -L$$PWD/sqlcipher/lib -lsqlcipher -leay32 -lsqlite3
TR_EXCLUDE += $$PWD/*
Create and fill C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\3rdparty\sqlcipher
Create the two directories:
C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\3rdparty\sqlcipher
C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\3rdparty\sqlcipher\lib
Copy the following files to C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\3rdparty\sqlcipher:
C:\temp\sqlcipher-master\shell.c
C:\temp\sqlcipher-master\sqlite3.c
C:\temp\sqlcipher-master\sqlite3.h
C:\temp\sqlcipher-master\sqlite3ext.h
Copy the following files/directories to C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\3rdparty\sqlcipher\lib:
C:\temp\sqlcipher-master\dist\lib
C:\temp\sqlcipher-master\sqlite3.dll
C:\OpenSSL-Win32\bin\libeay32.dll
The directory now consists of the following files and directories:
C:\QT\QT5.4.0\5.4\SRC\QTBASE\SRC\3RDPARTY\SQLCIPHER
| shell.c
| sqlite3.c
| sqlite3.h
| sqlite3ext.h
|
\---lib
| libeay32.dll
| libsqlcipher.a
| libsqlcipher.la
| sqlite3.dll
|
\---pkgconfig
sqlcipher.pc
Compile the QSQLCIPHER-driver-plugin for Qt:
Open Qt-command line C:\Windows\System32\cmd.exe /A /Q /K C:\Qt\Qt5.4.0\5.4\mingw491_32\bin\qtenv2.bat
Execute the following commands:
cd C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\pluins\sqldrivers\sqlcipher
qmake
mingw32-make
This builds the QSQLCIPHER-driver-plugin within the following directory:
C:\QT\QT5.4.0\5.4\SRC\QTBASE\PLUGINS\SQLDRIVERS
libqsqlcipher.a
libqsqlcipherd.a
qsqlcipher.dll
qsqlcipherd.dll
Copy 'qsqlcipher.dll' and 'qsqlcipherd.dll' to the SQL-driver-plugin-directory C:\Qt\Qt5.4.0\5.4\mingw491_32\plugins\sqldrivers.
Create a new encrypted SQLite/SQLCipher-database
Create new SQLite-Plaintext-database 'plaintext.db' with a test table and some test data
Change directory to C:\sqlcipher, which contains 'sqlcipher.exe' and 'sqlite3.dll' (see above).
C:\sqlcipher>sqlcpher.exe plaintext.db
SQLCipher version 3.8.6 2014-08-15 11:46:33
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table testtable (id integer, name text);
sqlite> insert into testtable (id,name) values(1,'Bob');
sqlite> insert into testtable (id,name) values(2,'Charlie');
sqlite> insert into testtable (id,name) values(3,'Daphne');
sqlite> select * from testtable;
1|Bob
2|Charlie
3|Daphne
sqlite> .exit
Open C:\sqlcipher\plaintext.db using a standard text-editor:
Database scheme and test data can be read in plaintext.
Encrypting the plaintext-database
This will create the database C:\sqlcipher\encrypted.db using the key 'testkey'.
C:\sqlcipher>sqlcipher.exe plaintext.db
SQLCipher version 3.8.6 2014-08-15 11:46:33
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'testkey';
sqlite> SELECT sqlcipher_export('encrypted');
sqlite> DETACH DATABASE encrypted;
sqlite> .exit
Open C:\sqlcipher\encrypted.db using a standard text-editor:
Data are now encrypted.
For more useful information visit:
https://www.zetetic.net/sqlcipher/sqlcipher-api/
Usage of the SQLite-database with SQLCipher-extension and access via Qt
Create a new Qt-command-line-project e.g. 'qsqlcipher'
Project file
QT += core sql
QT -= gui
TARGET = qsqlcipher
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
Test-program 'main.cpp'
#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QDebug>
#include <QString>
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
qDebug() << QSqlDatabase::drivers();
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLCIPHER");
db.setDatabaseName("C:/sqlcipher/encrypted.db");
db.open();
QSqlQuery q;
q.exec("PRAGMA key = 'testkey';");
q.exec("insert into testtable (id,name) values(4,'dummy')");
q.exec("SELECT id,name anz FROM testtable");
while (q.next()) {
QString id = q.value(0).toString();
QString name = q.value(1).toString();
qDebug() << "id=" << id << ", name=" << name;
}
db.close();
return 0;
}
Compile and execute
("QSQLCIPHER", "QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3", "QPSQL", "QPSQL7")
id= "1" , name= "Bob"
id= "2" , name= "Charlie"
id= "3" , name= "Daphne"
id= "4" , name= "dummy"
When delivering a Qt-program do not forget the Qt-libraries, the platforms-libraries, SQL-driver-plugin 'qsqlcipher.dll' and the OpenSSL-library 'libeay32.dll'.
Example for the test program above:
C:\TEMP\QSQLCIPHER-TEST
| icudt53.dll
| icuin53.dll
| icuuc53.dll
| libeay32.dll
| libgcc_s_dw2-1.dll
| libstdc++-6.dll
| libwinpthread-1.dll
| qsqlcipher.exe
| Qt5Core.dll
| Qt5Sql.dll
|
+---platforms
| qminimal.dll
| qoffscreen.dll
| qwindows.dll
|
\---sqldrivers
qsqlcipher.dll
Caution: The test program contains the key:
...
q.exec("PRAGMA key = 'testkey';");
...
This key string in the binary file of the test program can easiliy be read using a hex-editor, which is, to my opinion, a lack in security:
...
00002C90 70 68 65 72 2F 65 6E 63 72 79 70 74 65 64 2E 64 pher/encrypted.d
00002CA0 62 00 50 52 41 47 4D 41 20 6B 65 79 20 3D 20 27 b.PRAGMA key = '
00002CB0 74 65 73 74 6B 65 79 27 3B 00 00 00 69 6E 73 65 testkey';...inse
00002CC0 72 74 20 69 6E 74 6F 20 74 65 73 74 74 61 62 6C rt into testtabl
...
For approaches how to solve this problem, ask the search engine of your own choice. ;-)
E.g. search for: hide string in executable