Skip to content

Commit

Permalink
Code commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jo3bingham committed Oct 3, 2016
1 parent 4783dee commit 98dc2e4
Show file tree
Hide file tree
Showing 14 changed files with 1,008 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Tibia IP Changer.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tibia IP Changer", "Tibia IP Changer\Tibia IP Changer.csproj", "{8500F172-B5EF-4B06-90B0-CCA0CD101F5C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8500F172-B5EF-4B06-90B0-CCA0CD101F5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8500F172-B5EF-4B06-90B0-CCA0CD101F5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8500F172-B5EF-4B06-90B0-CCA0CD101F5C}.Debug|x86.ActiveCfg = Debug|x86
{8500F172-B5EF-4B06-90B0-CCA0CD101F5C}.Debug|x86.Build.0 = Debug|x86
{8500F172-B5EF-4B06-90B0-CCA0CD101F5C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8500F172-B5EF-4B06-90B0-CCA0CD101F5C}.Release|Any CPU.Build.0 = Release|Any CPU
{8500F172-B5EF-4B06-90B0-CCA0CD101F5C}.Release|x86.ActiveCfg = Release|x86
{8500F172-B5EF-4B06-90B0-CCA0CD101F5C}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
9 changes: 9 additions & 0 deletions Tibia IP Changer/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="Tibia_IP_Changer.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Tibia_IP_Changer"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
16 changes: 16 additions & 0 deletions Tibia IP Changer/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

namespace Tibia_IP_Changer
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
29 changes: 29 additions & 0 deletions Tibia IP Changer/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Window x:Class="Tibia.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Tibia"
mc:Ignorable="d"
Title="Tibia IP Changer" SizeToContent="Height" Width="250" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize">
<Grid>
<DockPanel>
<DockPanel DockPanel.Dock="Top">
<Label Content="Client Path" DockPanel.Dock="Left" Margin="5,5,0,5" />
<Button Content="..." DockPanel.Dock="Right" Margin="0,5,5,5" Name="uxBrowse" Click="uxBrowse_Click" />
<TextBox Margin="0,5,5,5" MaxWidth="147" Name="uxClientPath" />
</DockPanel>
<DockPanel DockPanel.Dock="Top">
<DockPanel DockPanel.Dock="Right">
<Label Content="Port" DockPanel.Dock="Left" Margin="0,5" />
<TextBox DockPanel.Dock="Right" Margin="0,5,5,5" MaxWidth="36" Name="uxPort" Text="7171" />
</DockPanel>
<DockPanel DockPanel.Dock="Left">
<Label Content="IP" DockPanel.Dock="Left" Margin="5,5,0,5" />
<TextBox DockPanel.Dock="Right" Margin="0,5,5,5" Name="uxIP" Text="127.0.0.1" />
</DockPanel>
</DockPanel>
<Button Content="Apply" Margin="5" Name="uxApply" Click="uxApply_Click" />
</DockPanel>
</Grid>
</Window>
217 changes: 217 additions & 0 deletions Tibia IP Changer/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
using System;
using System.Diagnostics;
using System.Net;
using System.Threading;
using System.Windows;
using Tibia.Utilities;

namespace Tibia
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

uxClientPath.Text = Constants.DefaultClientPath;
}

private void uxBrowse_Click(object sender, RoutedEventArgs e)
{
var openFileDialog = new System.Windows.Forms.OpenFileDialog() { Filter = "Tibia Client (*.exe)|*.exe" };
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);

if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
uxClientPath.Text = openFileDialog.FileName;
}
}

private void uxApply_Click(object sender, RoutedEventArgs e)
{
try
{
if (uxClientPath.Text == string.Empty || !uxClientPath.Text.Contains(".exe"))
{
MessageBox.Show("Client path is not valid.");
return;
}

if (uxIP.Text.Equals(string.Empty))
{
MessageBox.Show("IP is not valid.");
return;
}

ushort port;
if (uxPort.Text.Equals(string.Empty) || !ushort.TryParse(uxPort.Text, out port))
{
MessageBox.Show("Port is not valid.");
return;
}

Environment.CurrentDirectory = uxClientPath.Text.Replace("Tibia.exe", "");
Process process = Process.Start(uxClientPath.Text);

process.WaitForInputIdle();
while (process.MainWindowHandle == IntPtr.Zero)
{
process.Refresh();
Thread.Sleep(5);
}

var version = FileVersionInfo.GetVersionInfo(uxClientPath.Text).FileVersion;
if (version == "1, 0, 0, 1")
{
version = "7.0.0";
}

var versionNumber = int.Parse(version.Replace(".", ""));
var baseAddress = (uint)process.MainModule.BaseAddress.ToInt32();
var processHandle = WinAPI.OpenProcess((WinAPI.PROCESS_VM_READ | WinAPI.PROCESS_VM_WRITE | WinAPI.PROCESS_VM_OPERATION), 0, (uint)process.Id);
var buffer = Memory.ReadBytes(processHandle, baseAddress, (uint)process.MainModule.ModuleMemorySize);

uint loginServerStart = 0;
if (versionNumber >= 10110)
{
loginServerStart = Memory.ScanBytes(buffer, Constants.LoginServerArrayCurrent) + baseAddress;
loginServerStart = Memory.ReadUInt32(processHandle, loginServerStart - Constants.LoginServerStartOffsetCurrent) - baseAddress;
}
else if (versionNumber >= 800)
{
loginServerStart = Memory.ScanBytes(buffer, Constants.LoginServerArrayPre10110) + baseAddress;
loginServerStart = Memory.ReadUInt32(processHandle, loginServerStart + Constants.LoginServerStartOffsetPre10110) - baseAddress;
}
else
{
loginServerStart = Memory.ScanBytes(buffer, Constants.LoginServerArrayPre8000) + baseAddress;
loginServerStart = Memory.ReadUInt32(processHandle, loginServerStart + Constants.LoginServerStartOffsetPre8000) - baseAddress;
}

uint loginServerEnd = 0;
uint loginServerStep = 0;
uint loginServerHostnamePtrOffset = 0;
uint loginServerIpPtrOffset = 0;
uint loginServerPortOffset = 0;
uint loginServerCount = 0;
if (versionNumber >= 10500)
{
loginServerEnd = loginServerStart + 0x04;
loginServerStep = 0x30;
loginServerHostnamePtrOffset = 0x04;
loginServerIpPtrOffset = 0x1C;
loginServerPortOffset = 0x28;
}
else if (versionNumber >= 10110)
{
loginServerEnd = loginServerStart + 0x04;
loginServerStep = 0x38;
loginServerHostnamePtrOffset = 0x04;
loginServerIpPtrOffset = 0x20;
loginServerPortOffset = 0x30;
loginServerCount = 0x0A;
}
else if (versionNumber >= 800)
{
loginServerStep = 0x70;
loginServerPortOffset = 0x64;
loginServerCount = 0x0A;
}
else
{
loginServerStep = 0x70;
loginServerPortOffset = 0x64;
loginServerCount = 0x04;
}

uint rsaKey = 0;
if (versionNumber >= 861)
{
rsaKey = Memory.ScanString(buffer, Constants.RsaKeyCurrent);
}
else if (versionNumber >= 772)
{
rsaKey = Memory.ScanString(buffer, Constants.RsaKeyOld);
}

process.Kill();

var pi = new WinAPI.PROCESS_INFORMATION();
var si = new WinAPI.STARTUPINFO();

if (!WinAPI.CreateProcess(uxClientPath.Text, " ", IntPtr.Zero, IntPtr.Zero, false, WinAPI.CREATE_SUSPENDED, IntPtr.Zero, System.IO.Path.GetDirectoryName(uxClientPath.Text), ref si, out pi))
{
return;
}

processHandle = pi.hProcess;
process = Process.GetProcessById(Convert.ToInt32(pi.dwProcessId));
baseAddress = (uint)WinAPI.GetBaseAddress(processHandle).ToInt32();

WinAPI.ResumeThread(pi.hThread);
process.WaitForInputIdle();
WinAPI.CloseHandle(pi.hThread);

while (process.MainWindowHandle == IntPtr.Zero)
{
process.Refresh();
Thread.Sleep(5);
}

var resolvedIp = uxIP.Text;
IPAddress ipa;
if (!IPAddress.TryParse(uxIP.Text, out ipa))
{
IPAddress[] addressList = Dns.GetHostEntry(uxIP.Text).AddressList;

if (addressList.Length > 0)
{
resolvedIp = addressList[0].ToString();
}
}

if (versionNumber >= 10110)
{
uint loginStart = Memory.ReadUInt32(processHandle, loginServerStart + baseAddress);
uint loginEnd = Memory.ReadUInt32(processHandle, loginServerEnd + baseAddress);

for (uint loginServer = loginStart; loginServer < loginEnd; loginServer += loginServerStep)
{
uint ipAddress = Memory.ReadUInt32(processHandle, loginServer + loginServerIpPtrOffset);
Memory.WriteInt32(processHandle, ipAddress, 0);

uint hostAddress = Memory.ReadUInt32(processHandle, loginServer + loginServerHostnamePtrOffset);
Memory.WriteString(processHandle, hostAddress, resolvedIp);

uint portAddress = loginServer + loginServerPortOffset;
var portValue = BitConverter.ToUInt16(Memory.ReadBytes(processHandle, portAddress, 2), 0);
Memory.WriteUInt16(processHandle, portAddress, port);
}
}
else
{
uint loginServer = loginServerStart + baseAddress;

for (int i = 0; i < loginServerCount; ++i)
{
Memory.WriteString(processHandle, loginServer, resolvedIp);
Memory.WriteUInt16(processHandle, loginServer + loginServerPortOffset, port);
loginServer += loginServerStep;
}
}

if (versionNumber >= 772)
{
Memory.WriteRsa(processHandle, (rsaKey + baseAddress), Constants.RsaKeyOpenTibia);
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}
55 changes: 55 additions & 0 deletions Tibia IP Changer/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Tibia IP Changer")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Tibia IP Changer")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

//In order to begin building localizable applications, set
//<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
//inside a <PropertyGroup>. For example, if you are using US english
//in your source files, set the <UICulture> to en-US. Then uncomment
//the NeutralResourceLanguage attribute below. Update the "en-US" in
//the line below to match the UICulture setting in the project file.

//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]


[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]


// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading

0 comments on commit 98dc2e4

Please sign in to comment.