[Master C++ Pointer #1]Retrieve private and protected stuffs in C++

[You may only do this practice on Linux..Coz windows behaves weirdly allocating memory for stack variables~ :( ]

 

This practice is made by Parthas Menethil(Luke.J.Sun). All rights reserved.

C++ is such a flexible language to have fun. You can break the common rule an do fantastic things. In this post, I am going to work with you to hack private information out from classes using only pointers!

I have three classes declared and instantiated one instance of each class in main. Some of the values inside the class are inaccessible by normal methods. We are going to pull them out from the memory.

NOTE: This program should be complied targeting 32bit operating system. The preferred compiler would be GCC 4.6.3

/*	How to access values in a class abnormally
 *
 * */
#include <iostream>
using namespace std; // Not a good habit, just for testing
class A{
private:
	int apples;
	double weight;
	unsigned int bugs;
public:
	A() : apples(100), weight(99.9989), bugs(123456){}
};
class B{
private:
	double exam_grade;
protected:
	double test_grade;
public:
	double quiz_grade;
	B(double exam, double test, double quiz){
		exam_grade = exam;
		test_grade = test;
		quiz_grade = quiz;
	}
	virtual void placeholder(){ /* Nothing to show */ }
};
struct Date{
private:
	int day;
	int month;
	int year;
public:
	Date(int d, int m, int y){
		day = d;
		month = m;
		year = y;
	}
};
class C : public B{
protected:
	Date *d;
public:
	C() : B(55, 50, 60.9){
		d = new Date(20, 9, 1993);
	}
	~C(){
		delete d;
	}
};
int main(){
	A a;
	B b(99,98,97);
	C c;
	cout<<"C++ is flexible!"<<endl<<endl;
	cout<<"Example done by Parthas Menethil(Luke.J.Sun)"<<endl<<endl;
	cout<<"Size of class A: "<<sizeof(a)<<endl;
	cout<<"->int(4) + double(8) + uint(4) = 16"<<endl;
	cout<<"Size of class B: "<<sizeof(b)<<endl;
	cout<<"->virtual table pointer(4) + double(8) * 3 = 28"<<endl<<endl;
	cout<<"Now let's retrieve everything from memory..."<<endl;
	/* Instance a */
	cout<<"In instance a:"<<endl;
	// Take stuffs from the beginning pointer
	cout<<"[Private]apples:"<<*(int*)(&a)<<endl;
	// Change the pointer to a char pointer so offset would moved by 1 per increasement, use sizeof to count the size of variable type
	cout<<"[Private]weight:"<<*(double*)((char*)(&a) + sizeof(int))<<endl;
	cout<<"[Private]bugs:"<<*(unsigned int*)((char*)(&a) + sizeof(int) + sizeof(double))<<endl<<endl;
	/* Instance b */
	cout<<"Ininstance b:"<<endl;
	// Pass the initial virtual table pointer
	cout<<"[Private]exam_grade:"<<*(double*)((char*)&b + 4)<<endl;
	// Move forward to pass the first double and retrieve the second one
	cout<<"[Protected]test_grade:"<<*(double*)((char*)&b + 4 + sizeof(double))<<endl;
	// Move forward to pass the second double and retrive the third one
	// The use of sizeof is not necessary because sometimes we know the variable type length
	// double has the length of 8
	cout<<"[Public]quiz_grade:"<<*(double*)((char*)&b + 4 + sizeof(double) + 8)<<endl<<endl;
	/* Let's try to retrieve the values in the Date object in C class */
	/* Instance c */
	cout<<"Instance c:"<<endl;
	cout<<"[Protected]d->day:"<<"[Your answer?]"<<endl;
	cout<<"[Protected]d->month:"<<"[Your answer?]"<<endl;
	cout<<"[Protected]d->year:"<<"[Your answer?]"<<endl;
	return 0;
}

At the beginning, I counted and explained the sizes as well as structures of class A and B, and then retrieved all values from A and B using only pointers.

To get the variables’ values from class C is tricky, you can try to do it by yourself! I believe that you can find the right answers out!

My solution will be released in the next post about this topic. Before that, try it out!

The right output should be:

C++ is flexible!

Example done by Parthas Menethil(Luke.J.Sun)

Size of class A: 16
->int(4) + double(8) + uint(4) = 16
Size of class B: 28
->virtual table pointer(4) + double(8) * 3 = 28

Now let’s retrive everything from memory…
In instance a:
[Private]apples:100
[Private]weight:99.9989
[Private]bugs:123456

Ininstance b:
[Private]exam_grade:99
[Protected]test_grade:98
[Public]quiz_grade:97

Instance c:
[Protected]d->day:20
[Protected]d->month:9
[Protected]d->year:1993

Try to match my output. There could be many ways to do the samething in c++.

[GoLang]A simple game – Rock Paper Scissors Lizard Spock

Last week, I just noticed that in this semester(2013 Fall), the Intro C Language(IPC144) course’s assignment 1 is a game called Rock Paper Scissors Lizard Spock. The first I encountered this game was when I was learning Python language on a online course. Unfortunately, I quit that course coz I was way too busy, but the game concept was embed into my mind.

Since it has been a long time that I have not actually  on my normal study pace due to the “DEATH” of my deeply loved 4-year-old laptop. Now thanks to my dear sister like friend, I got a temporary laptop and am able to be back on the track of studying Go Language.

Today I am going to make a “Rock Paper Scissors Lizard Spock” game using Go Language. I have seen many people use tons of if statements to judge the result of two players. The judgement part is indeed the tricky programming part of this game, and I am going to think of a clear way to implement it. Finally that part results into four lines of code. I cannot be 100 percent sure that it it right, but anyway hopefully there would be no bug.

My code would be available publicly on GitHub. Please feel free to fork and improve.

In addition, absolutely, I will finish the bonus part of that game which is Human versus Artificial Intelligence. It would be coming soon.

GitHub Repo(The folder is called RPSLS): https://github.com/Parthas-Menethil/GoLangStudy

Parthas.Menethil (Luke.J.Sun)

Basic Math

It took me so long time because that I was doing all of this on an IPAD!
This basic math program takes advantages of the object oriented programming and is built based on git source managing system and all commit histories are stored on GitHub.

GitHub-BasicMath

Although our dear professor H did not cover the arguments in C++ on the last lecture, we, who are good at self studying, worked out the exercise.

This project takes advantages of the following new stuffs covered in OOP344:
Preprocessor macro
GitHub
Arguments
Object Oriented Programming

Fortunately my iPad does compatible with all stuffs needed. XD

Source Code:
Coming Soon. Please check GitHub while it is unavailable.

How integers in VC++ 2012 are stored in memory on windows

This article is written by Luke(Parthas.Menethil.Sun). All rights reserved.

When we assign an integer to a variable, in fact, we store that number in some standard format into our memory somewhere. I have always been curious about how they are stored into our memory. Let’s check it out here.

Based on different languages, compilers, compiling configurations and systems, the answer may be various. My environment is using Visual Studio 2012 with the CL 17.00.51106.1 for x86 complier on Windows 7.

My testing code is simple:

#include
int main(){
	int i = 0x12ABCDEF;   // Assign a hexadecimal number
	printf(&quot;%x&quot;, &amp;i);     // Get the memory address of that variable
	return 0;
}

First I assign a number to fill all bytes of the integer and output its memory address.

While running, we can use hex editors like WinHex to monitor what’s going on in the memory.

We can see at the specific offset, the variable in each byte is stored in the reverse order.

How to change the password on you INT322 website

As you guys who have a zenit account, your website should be:https://zenit.senecac.on.ca/~username/

But when you visit that webpage there’s a prompt pops up asking  username and password and you cannot see the website without providing them.

How to make your own password without removing them?

The htpasswd can add a username for you:

 

htpasswd -b ~/.htpasswd username password

The command will generate a token string based on provided username and password and attach the token string to the .htpasswd file in your home directory.

NOTE: Do NOT overwrite or delete any previous things from that file or your professor cannot access your website.

FAQ for geeks:

1. I found that there are some other information in the .htpasswd file and they seem to be our professor’s username and password. Could we take advantage of that to see my classmates’ web pages?

Technically and ethically NO. Those passwords are generated by some hash arithmetic which is irreversible.

2. There are some online Apache password file generater. Should I use them?

NO. Not every site is using HTTPS protocol to protect your data during the transmission, your username and password can be sniffed by others. Even though they use the safe protocol, your username and password might be recorded by their website which creates potential threats for you.

This article is written by Luke(Parthas.Menethil.Sun). All rights reserved.

解决OPENVPN在Win7下安装的问题

安装OPENVPN的时候,有一步会给系统安装一个TAP驱动。但是在Windows7下,这个驱动因为没有签名从而无法正常使用。

在连接的时候会出现这样的错误:

Fri Nov 05 09:07:24 2004 us=747770 CreateFile failed on TAP device:
\.Global{A3B605BE-B118-49BA-92C3-C6ADFD7A364C}.tap: The system
cannot find the file specified. (errno=2)

解决方案就是:

  1. 如果已经安装OPENVPN,请完全卸载。
    If installed – uninstall the current copy of OpenVPN.
  2. 从这个地址下载OPENVPN安装程序
    Download the installer from one of the following links:
    http://openvpn.net/release/openvpn-2.1_rc19-install.exe
  3. 下载完成之后
    鼠标右键安装程序->属性->兼容性->以Windows Vista兼容模式运行这个程序
    Rt-Click Installer>>Properties>>Compatibility>>X Run this program in compatibility mode for: Windows Vista
    同时勾选以管理员身份运行此程序
    Privilege Level:
    X Run this program as an administrator
    确定
    Then Click Ok
  4. 运行安装程序完成安装
    Now run the installer

小贴士:不要忘记在OpenVPN GUI程序属性的兼容性选项卡中勾选[以管理员身份运行此程序]。

Tip: Set also under properties in the OpenVPN GUI “run this programm as an administrator”

把iCloud玩瘫了

添加事件的时候,我总是爱写24小时制,而iCloud上只能输入12小时制(Default),一开始还可以自动改正,后来就直接这样了…

https://blog.matrixdoge.com/wp-content/uploads/2011/11/20111101-173533.jpg

Windows Phone 7!

前几天和朋友LL聊天偶然提到了Windows Phone 7,想一想既然没有money去买苹果iOS的开发KEY,倒不如直接在WP7平台开发练练手,正好是我熟悉的.NET。

之前已有Visual Studio 2010,SDK加上一堆乱七八糟的东西也就几百MB,很快就装完了。

运行之后,创建一个新工程,发现竟然没有我最熟练的VB.NET,记得之前我看过一个视频说支持VB.NET啊…不知道怎么回事。。。不过没有关系,对于我来说VB.NET和C#.NET是简体中文和繁体中文的关系,可以做到完美翻译,凑合用吧,正好练练C语言语法。

我很快就用Silverlight写出了第一个WP7 Hello World:

Continue reading “Windows Phone 7!”

Parthas’云计算模型总结

最早接触云是从Rising的云安全开始,当初我对那东西并没有什么好感,不过我发现云这个词在互联网上越来越流行,我才意识到我不能不承认这是一个未来的机会。为了不输在起跑线上,我计划在明年3月份之前拿出一份像样的云计算模型。

自从10月13日云计算的第一个版本Alpha-1出世以来,我进行了一系列的修改,到了如今11月3日最新测试的Alpha-3版,已经可以体现出云计算的优势所在了。

我的云计算主要以分布式计算质数为主。

根据今天的测试情况来看,云计算的优势已经体现出来了。

今天测试的是计算80000以内的质数

单线程执行 14秒

8线程执行 9秒

2台机器(一个开了3线程+服务器,一个开了5线程)8秒

时间测定均为发出指令开始计时到最后一个结果到达为止。

目前的改进方向就是总体的自动调度与资源的分配。

测试历史:

Alpha-1                                                   10月13日

第一个云计算版本。因数据通讯过于频繁,造成带宽阻塞,通讯受阻。服务器压力过大,仅仅可以承载一个云端。

Alpha-2                                                   10月17日

减少了通讯周期,数据分组进行计算,已经可以负载多个云端进行正常云计算。但是因为多线程并行工作造成数据收集出现未知错误,导致丢失结果,虽然说一般不超过10个,但是一样是不允许的。

Alpha-3                                                   11月3日

本版本在Alpha-2版本的基础上将数据收集端与调度服务器分开,避免多线程操作数组产生冲突导致丢失数据。

上一个版本中发现一个BUG,因为每次接受数据量只有100字节,造成循环次数过多,每次循环式1秒钟,造成了数据接受过慢。于是更改了数据接收量,使效率进一步提高。